Marc Adler Marc Adler - 7 months ago 21
Python Question

Python / Selenium - using a conditional in an xpath

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]
?

Answer

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]")