3

I need to find a particular element in some webpages which are basically identical in structure, using the following xpath:

//*[@id="detailPCTtableHeader"]/tbody/tr[10]/td[2]/div/span/span[1]/text()

The problem is that in some pages tr[10] is tr[11].

Is there a way to tell Selenium to search for tr[10] or tr[11]?

2 Answers 2

2

You could use the position() to evaluate the index:

element = driver.find_element_by_xpath("id('detailPCTtableHeader')/tbody/tr[position()=10 or position()=11]/td[2]/div/span/span[1]")
Sign up to request clarification or add additional context in comments.

Comments

2

"Is there a way to tell Selenium to search for tr[10] or tr[11]?"

Use position() :

tr[position() = 10 or position() = 11]

Alternatively, if only one of tr[10] and tr[11] ever exists at a time, meaning it is always the last tr, you can simply use last() :

tr[last()]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.