[英]How to get first 2 following siblings using Xpath (Webscraping: Python using Selenium)?
[英]How to get sibling and siblings attribute using selenium xpath
根据使用 xpath 找到的“CertainOption”的位置,我怎样才能获得下面显示的兄弟姐妹的 id。 我很欣赏任何提示。
<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>
到目前为止,我编写了以下工作代码:
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');
最后我找到了解决方案:
#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')
不管怎么说,还是要谢谢你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.