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,320 points
7,506 views

2 answers 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,910 points
0 votes

xpath are two types.

1) Absolute XPath:   

/html/body/div[2]/element1

2) Relative Xpath

Xpath=//tagname[@attribute='value']
answered Sep 3, 2020 by Sri
• 3,190 points

Related Questions In Selenium

0 votes
2 answers
0 votes
1 answer
+1 vote
1 answer
0 votes
2 answers

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

driver.findElement(By.linkText("link")).click(); READ MORE

answered Sep 4, 2020 in Selenium by Sri
• 3,190 points
1,199 views
0 votes
2 answers

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,500 points
12,577 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,559 views
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,910 points
6,606 views
0 votes
1 answer

Installing Selenium Webdriver with Python package

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

answered May 8, 2019 in Selenium by Anvi
• 14,150 points
15,122 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,910 points
10,216 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,910 points
20,252 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP