简体   繁体   English

如何使用 selenium xpath 获取兄弟姐妹和兄弟姐妹属性

[英]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.根据使用 xpath 找到的“CertainOption”的位置,我怎样才能获得下面显示的兄弟姐妹的 id。 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不管怎么说,还是要谢谢你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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