简体   繁体   中英

How to get sibling and siblings attribute using selenium xpath

Based on the position of "CertainOption" found using xpath, how can I get the id of the sibling shown below. I appreciate any hint.

 <tr style="background-color:#EFF3FB;"> <td align="center"> <input id="OptionID" type="checkbox" name="OptionName"> </td> <td align="center">CertainOption</td> <td align="left">Description</td> </tr>

So far I wrote following working code:

element = browser.find_element_by_xpath("*//td[.//td[text()='CertainOption']]");
elements_sib = ???
siblings_att =  elements_sib.find_element_by_xpath(".//*[@type='checkbox']").get_attribute('id');

Finally I found the solution:

#First, get element of interest    
element = browser.find_element_by_xpath("//td[text()='CertainOption']")

#Second, get first preceding sibling  
elements_sibl = element.find_element_by_xpath("preceding-sibling::td[1]")

# Third, navigate to subfolder input and get attribute 
siblings_att = elements_sibl.find_element_by_xpath("input").get_attribute('id')

Thanks anyway

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM