Reputation: 1
The webpage I am searching has multiple instances of the keyword, but I am interested in finding the instance within a particular column. I need to search using multiple terms in order to find the keyword and the column identifier. The entire table on the page is included in the 'body' so there is no header. Once I find the appropriate instance of the keyword, I need to move two columns over in order to get the correct download.
row = driver.find_element(By.XPATH, "//*[contains(text(), 'keyword')]")
tree = lxml.html.fromstring(row.parent.page_source)
element = tree.xpath("//*[contains(text(), 'keyword')]")[0]
root = tree.getroottree()
xpth = root.getpath(element)
pdf_xpath = xpth.replace('td[4]', 'td[6]')
The keyword appears in td[5] before appearing in td[4], and find_element won't give more than the first instance.
'/html/body/div[2]/div[3]/div/div[4]/div[3]/div[2]/div/table/tbody/tr[62]/td[5]'
'/html/body/div[2]/div[3]/div/div[4]/div[3]/div[2]/div/table/tbody/tr[83]/td[4]'
The first xpath is what is being flagged currently, and the second is what I want to flag, both have the same text.
Upvotes: 0
Views: 568
Reputation: 287
A hacky way to achieve this could be using find_elements
instead of find_element
and then accessing the element you'd prefer.
rows = driver.find_elements(By.XPATH, "//*[contains(text(), 'keyword')]")
row_i_want = rows[1]
Upvotes: 1