Reputation: 20078
How do I select only the `Published DateTime webelement and click it?
My HTML code
<tr>
<th scope="col">
...........
</th>
<th scope="col">
...........
</th>
<th scope="col">
...........
</th>
<th scope="col">
...........
</th>
........
<th scope="col">
<a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$AddControl1$gv','Sort$PublishDateTime')">Published DateTime</a>
</th>
</tr>
Upvotes: 0
Views: 1074
Reputation: 39
driver.FindElement(By.LinkText("Published DateTime")).Click();
is a correct answer. In addition, if you are using javascript-selenium-webdriver. Suggest you use https://npmjs.org/package/webdriver-helper for more friendly apis.
driver.link(':contains("Published DateTime")').click()
could help you.
Upvotes: 0
Reputation: 7339
if you want to validate text you can use the following approach: - locating element as I mentioned above:
String cssSelector= "tr>th[scope='col']>a:contains('Published DateTime')"
1st way of getting validating text:
String txt=driver.findElement(By.cssSelector(cssSelector)).getText().trim();
//validating
Assert.assertTrue(txt.equals("blablabla"));
2nd way of getting validating text:
publiv String getTextByJs(String css){
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\""+css+"\");");
stringBuilder.append("return x.text().toString();") ;
String res= (String) js.executeScript(stringBuilder.toString());
}
String txt=getTextByJs(cssSelector);
Assert.assertTrue(txt.equals("blablabla"));
enjoy)
Upvotes: 0
Reputation: 676
Try this:
driver.FindElement(By.LinkText("Published DateTime")).Click();
Upvotes: 2
Reputation: 7339
String cssSelector= "tr>th[scope='col']>a:contains('Published DateTime')"
first way:
driver.findElement(By.css(cssSelector)).click()
second way(using js):
public void jsClick(String css){
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\'"+css+"\');");
stringBuilder.append("x.click();");
js.executeScript(stringBuilder.toString());
}
jsClick(cssSelector)
Hope this works for you)
Upvotes: 0