Nick Kahn
Nick Kahn

Reputation: 20078

selecting elements in WebDriver

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

Answers (4)

Sheldon
Sheldon

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

eugene.polschikov
eugene.polschikov

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

Vlad Titov
Vlad Titov

Reputation: 676

Try this:

driver.FindElement(By.LinkText("Published DateTime")).Click();

Upvotes: 2

eugene.polschikov
eugene.polschikov

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

Related Questions