Contains expression in Selenium using Java Language

0 votes

I have used two paths :

 driver.findElement(By.xpath("//tr[@id='"+variable+"']/td[5]/span")).click();

 driver.findElement(By.xpath("//tr[@id='"+variable+"']/td[5]")).click();

How can I use both using contains expression so that the first or second version will be searched.

May 4, 2018 in Selenium by Martin
• 4,220 points
48 views

1 answer to this question.

0 votes

Do something like this:

WebElement thingie = driver.findElement(By.xpath("//tr[contains(@id,'" + variable + "')]/td[5]"))
if (thingie.findElements(By.tagName("span")).size() != 0) {

    thingie.findElement(By.tagName("span")).click();

} else {

    thingie.click();

}

Make a starting point WebElement.

Check to see if the <span> exists. If so, click on the span version, otherwise, click the td directly.

answered May 4, 2018 by Samarpit
• 5,130 points

Related Questions In Selenium

0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,110 points
2,288 views
0 votes
1 answer

How to scroll the Page up or down in Selenium WebDriver? (using java)

Its pretty simple. Check out the below ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,110 points
8,429 views
0 votes
1 answer

How to handle Pop-up in Selenium WebDriver using Java

Actually, its pretty simple. Use this code ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,110 points
4,979 views
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,110 points
965 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
1,127 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
868 views
0 votes
1 answer

Identifying the page iframe in Selenium using Java Code

Open the page in a browser and ...READ MORE

answered May 24, 2018 in Selenium by Samarpit
• 5,130 points
144 views
0 votes
1 answer

Not able to login using selenium webdriver using Java Language

Try to send text to the username ...READ MORE

answered Jul 17, 2018 in Selenium by Samarpit
• 5,130 points
362 views