Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
185 views
Welcome To Ask or Share your Answers For Others

1 Answer

I don't like using XPaths for locators that can be written as a CSS selector. I think it's much simpler as

#MyDiv123 > div.super

or just

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" ...>

... you get the idea...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...