为了账号安全,请及时绑定邮箱和手机立即绑定

Webelement内的xpath查找硒

Webelement内的xpath查找硒

喵喵时光机 2021-03-30 13:10:50
我有此页面:<html><head>    <title>Document Example</title></head><body>    <div id="container">        <div class="section">            <h1>Section 1</h1>            <li class="links">                <ul><a href="link.com/1"></a></ul>                <ul><a href="link.com/2"></a></ul>                <ul><a href="link.com/3"></a></ul>            </li>        </div>        <div class="section">            <h1>Section 2</h1>            <li class="links">                <ul><a href="link.com/4"></a></ul>                <ul><a href="link.com/5"></a></ul>                <ul><a href="link.com/6"></a></ul>            </li>        </div>        <div class="section">            <h1>Section 3</h1>            <li class="links">                <ul><a href="link.com/7"></a></ul>                <ul><a href="link.com/8"></a></ul>                <ul><a href="link.com/9"></a></ul>            </li>        </div>    </div></body></html>我想要按部分分组链接:driver.get("mypage.com")sections = driver.find_elements_by_xpath("//div[@class='section']")for section in sections:    section_name = section.find_element_by_xpath("//h1[@class='name']").get_attribute('innerHTML') #This fails    links = section.find_elements_by_xpath("//ul/a") #This find all links in page, not only links in section问题是,我通过xpath找到了所有页面中的元素列表(WebElement),现在我想通过xpath查找元素部分而不是所有页面中的所有特定元素。怎么了?编辑1这样可以解决问题(现在是xpath启动白点。):driver.get(“ mypage.com”)sections = driver.find_elements_by_xpath("//div[@class='section']")for section in sections:    section_name = section.find_element_by_xpath(".//h1[@class='name']").get_attribute('innerHTML') #This fails    links = section.find_elements_by_xpath(".//ul/a") #This find all links in page, not only links in section在这里可以找到以下解释:http : //selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webelement.WebElement.find_element_by_xpath
查看完整描述

2 回答

?
莫回无

TA贡献1865条经验 获得超7个赞

可能会工作


sections = driver.find_elements_by_xpath("//div[@class='section']")

for i, section in enumerate(sections):

    section_name = section.text

    links = section.find_elements_by_xpath("//div[@class='section'][%s]//a" % str(i+1))


查看完整回答
反对 回复 2021-04-13
?
神不在的星期二

TA贡献1963条经验 获得超6个赞

这是你想要的?您可以尝试以下方法:

//*[@id="container"]/div/h1/following-sibling::li/ul


查看完整回答
反对 回复 2021-04-13
  • 2 回答
  • 0 关注
  • 194 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号