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

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

java - Selenium multple xpaths how to select a specific xpath

I am using selenium to select an xpath based on the date but there are two separate dates in the html both that start with td. How do I specify which date to select? Do I place a [2] at the end of the search text?

The line looks like this

List<WebElement> li2 = driver.findElements(by.xpath("//td[contains(text(),'" + date " "')]/preceding::*5[]"

so should I do this

List<WebElement> li2 = driver.findElements(by.xpath("//td[contains(text(),'" + date " "'[2])]/preceding::*5[]"
question from:https://stackoverflow.com/questions/65850972/selenium-multple-xpaths-how-to-select-a-specific-xpath

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

1 Answer

0 votes
by (71.8m points)
List<WebElement> li2 = driver.findElements(by.xpath("//td[contains(text(),'" + date " "')]"))

you are using list do you can get the element get (list index starts from 0 )

 WebElement li = li2.get(4)

or

you can get 5th result of xpath by enclosing the xpath locator with brackets and calling [5] ( here index starts from 1 )

 WebElement li = driver.findElements(by.xpath("(//td[contains(text(),'" + date " "')])[5]"))

If you want to get 5th sibling then don't enclose it with bracket , this will say get the 5th td tag that is a sibling and contains the date

    WebElement li = driver.findElements(by.xpath("//td[contains(text(),'" + date " "')][5]"))


 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...