【问题标题】:How to locate the web element via sibling in python?如何通过python中的兄弟定位web元素?
【发布时间】:2018-01-14 17:03:57
【问题描述】:

如何在selenium中找到“check”,对应我需要点击的按钮?

我无法直接找到它。我必须使用 id='animal'。如果我在下面尝试没有错误,但没有响应。

driver.find_element_by_xpath('//div[@id="animal"]//following-sibling::div[contains(@style,"height:10px")]').find_element_by_xpath('//input[@value="check"]')

【问题讨论】:

    标签: python selenium xpath


    【解决方案1】:

    你可以使用 XPath

    //div[@id="animal"]/following-sibling::div/input[@value="check"]
    

    或者修改你的代码为

    driver.find_element_by_xpath('//div[@id="animal"]/following-sibling::div[contains(@style,"height:10px")]').find_element_by_xpath('./input[@value="check"]')
    

    请注意,我用./input 替换了//input,这意味着页面某处的输入节点(返回在DOM 中找到的第一个输入),这意味着输入节点是当前的子节点节点(返回在目标 div 中找到的第一个输入)

    【讨论】:

    • 你的方法有效。似乎/following-sibling//following-sibling 无关紧要。
    • 是的。 /following-sibling//following-sibling 在这种情况下并不重要
    • 为什么不进行一次搜索?通过链接它们,您将点击页面两次。
    • @Jeff 我的解决方案是通过单个 XPath 表达式获取所需的 input。第二个(附加)解决方案只是解释为什么 OP 的初始代码失败
    • 你是对的。我看到了第一行,但没有注意到它是一个组合定位器。我专注于第二行。
    猜你喜欢
    • 2018-09-08
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 2023-03-19
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多