Reputation: 125
Is there a way to find an element next to another element as shown in the example below? I'm trying to retrieve the word "Daily" by locating the words "Frequency of Update" first. I think an XPath would be the only way but I'm not sure how to get to it since its not a child element.
image: (https://i.sstatic.net/ELjbl.png)
Upvotes: 0
Views: 4023
Reputation: 4424
Try this xpath:
//*[contains(text(),'Frequency of Update')]/ancestor::td/following-sibling::td
This will first locate the element having innerHTML/text 'Frequency of Update' in the DOM, then locate 'td' element from all its ancestors, and then locate the 'td' element that is its sibling element, which has the 'Daily' text
Upvotes: 1
Reputation: 1042
You can find elements like this:
element = driver.find_element_by_xpath('//td[//*[contains(text(),"Frequency of Update")]]')
next_element = element.find_element_by_xpath('./following-sibling::td')
If you want to find another element by located element,
Xpath should start with ' . '
Upvotes: 1
Reputation: 319
If it's the element's sibling you should use the following xpath:
/following-sibling::td
element = <the element you retrived by locating "Frequency of Update">
element.find_element_by_xpath('/following-sibling::td')
Upvotes: 0