How to use such xpath to find web elements

0 votes

Consider this xpath which should always return one element.

//div[@id='MyDiv123']/div[contains(@class, 'super')]

Assume that we won't add anymore divs whose class is super. Given that info, I don't think that it is a good idea to use /div[contains(@class, 'super')]because the xpath will break

if div[contains(@class, 'super')] is placed inside another element.

Shouldn't we be using //div[contains(@class, 'super')] instead ?

Jun 14, 2018 in Selenium by Martin
• 4,220 points
243 views

1 answer to this question.

0 votes

It's much simpler to use:

#MyDiv123 > div.super

or use

div.super

if it's unique on the page.

XPath contains() is a string match. All the elements below will match your XPath locator but none of them will match the CSS selectors above.

<div class="super-duper" ...>

<div class="superior" ...>

<div class="abcsuperdef" ...>

answered Jun 14, 2018 by Samarpit
• 5,130 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
+1 vote
1 answer
0 votes
1 answer

How to use link as a locator to find element in selenium?

Hello @Jino, its amazing to explore ways ...READ MORE

answered Dec 21, 2018 in Selenium by Nabarupa
66 views
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,050 points
249 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
621 views
0 votes
1 answer

Installing Selenium Webdriver with Python package

Hey Hemant, for installing Selenium Webdriver with ...READ MORE

answered May 8 in Selenium by Anvi
• 9,990 points
83 views
0 votes
1 answer

How to find Elements by their Attribute in Python Selenium WebDriver

To find all the <td> tags which have attribute data-sortable-id you ...READ MORE

answered Jul 19, 2018 in Selenium by Samarpit
• 5,130 points
1,373 views
0 votes
1 answer

How to get next sibling element using XPath and Selenium for Java?

Below code will help you: Try following-sibling axis : WebElement ...READ MORE

answered May 15, 2018 in Selenium by Samarpit
• 5,130 points
4,575 views