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,340 points
3,359 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,890 points
0 votes

xpath are two types.

1) Absolute XPath:   

/html/body/div[2]/element1

2) Relative Xpath

Xpath=//tagname[@attribute='value']
answered Sep 2 by Sri
• 2,970 points

Related Questions In Selenium

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 3 in Selenium by Sri
• 2,970 points
330 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,520 points
4,037 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

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

answered Aug 31 in Selenium by Sri
• 2,970 points
4,985 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,890 points
2,923 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,110 points
4,050 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,890 points
6,833 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,890 points
13,298 views