【问题标题】:Python Webdriver unable to locate the correct button using XpathPython Webdriver 无法使用 Xpath 找到正确的按钮
【发布时间】:2015-07-01 11:06:46
【问题描述】:

我在为按钮找到正确的 Xpath 语法时遇到了一些麻烦。 页面上有 2 个添加按钮。报告下方有一个添加按钮。 项目下方有一个添加按钮。 我想在 Projects 下面找到那个,但我在 Reports 下面找到那个。

我尝试了以下 Xpaths,这些找到了 span 类 Reports 下方的添加按钮:

//button[div[. = 'Add...']]
//div[@class="gwt-HTML" and contains(text(), "Add...")]
e.g. driver.find_element_by_xpath(//button[div[. = 'Add...']])

我想在跨度类项目下方找到添加...按钮。 我可以使用以下 Xpath 找到 Projects span 类。我不知道如何继续 Xpath 并找到 Add... 按钮。

//span[@class="gwt-InlineLabel" and contains(text(), 'Projects')]

HTML sn-p 如下:

    <div>
    <div style="position: absolute; top: 3px; left: 5px;">
    <span class="gwt-InlineLabel" style="font-weight: bold; color: black; margin-right: 5px; margin-left: 20px;">Reports</span>
    <div class="GAT4PNUMP">
    <button class="gwt-Button" type="button">
        <div style="position: absolute; margin-left: 2px;">
        <div class="gwt-HTML" style="margin-left: 15px;">Add...</div>
    </button>
    <button class="gwt-Button" type="button" disabled="">
    <button class="gwt-Button" type="button" disabled="">
    <div class="GAT4PNUMP">
    <button class="gwt-Button" type="button" title="Run the selected reports" disabled="">
    <button class="gwt-Button" type="button" title="View the selected report" disabled="">
    <button class="gwt-Button workspacebuttontext130" type="button" title="Conditional report" disabled="">Conditional report ▼</button>
    <div class="GAT4PNUMP">
    <select class="gwt-ListBox" style="height: 20px; margin-right: 5px;" disabled="">
</div>  

<div>
    <div style="position: absolute; top: 3px; left: 5px;">
    <span class="gwt-InlineLabel" style="font-weight: bold; color: black; margin-right: 5px; margin-left: 20px;">Projects</span>
    <div class="GAT4PNUMP">
    <button class="gwt-Button" type="button">
        <div style="position: absolute; margin-left: 2px;">
        <div class="gwt-HTML" style="margin-left: 15px;">Add...</div>
    </button>
    <button class="gwt-Button" type="button" disabled="">
    <button class="gwt-Button workspacebuttontext75" type="button" title="Delete" disabled="">Delete </button>
    <div class="GAT4PNUMP">
    <button class="gwt-Button workspacebuttontext60" type="button" title="Import to the selected project" disabled="">Import...</button>
    <button class="gwt-Button workspacebuttontext60" type="button" title="Export from the selected project" disabled="">Export...</button>
    <button class="gwt-Button workspacebuttontext75" type="button" title="Upgrade the selected project" disabled="">Upgrade...</button>
    <button class="gwt-Button workspacebuttontext60" type="button" title="Refresh the selected project" disabled="">Refresh</button>
</div>

我可以使用什么 Xpath 来定位位于跨度类 Projects 下方的 Add...? 谢谢。

【问题讨论】:

  • 如果您使用 Firefox 浏览器,您可以安装 Selenium IDE 插件并简单地获取所需页面元素的唯一选择器(Xpath、css_selector...)
  • 我安装了 IDE 2.9.0。 IDE 中的哪里是唯一选择器的选项?如果我在目标字段中记录点击,我可以在下拉 xpath=(//button[@type='button'])[35] 中看到。这是选择器吗?
  • 嗯,是的。你可以用它来按下你的按钮driver.find_element_by_xpath("(//button[@type='button'])[35]").click()

标签: python selenium xpath selenium-webdriver


【解决方案1】:

只需在 Chrome 中打开网站,右键单击按钮,然后单击 inspect element。在窗口的下半部分,您现在应该可以看到页面的源代码。验证所需的行是否突出显示并右键单击它,然后选择“复制 xpath”。然后粘贴到driver.find_element_by_xpath("xpath_string_from_chrome")

【讨论】:

    【解决方案2】:
    driver.find_element_by_xpath(.//span[text()='Projects' and @class='gwt-InlineLabel]/button[text()='Add...'])
    

    【讨论】:

    • 感谢您的建议。在 html 树的上方还有一个跨度类项目标题。它会去那个。如果我可以使用 //span[@class="gwt-InlineLabel" and contains(text(), 'Projects')] 并在此处添加按钮,那就太好了
    • 再次感谢,我试过了,还是不行。如果我删除 /button[text()='Add...']) 它将找到项目跨度并突出显示它。我不知道为什么我不能到达它下面的按钮。 /button 应该转到 span 类标签下方的标签。不知道为什么它不去。
    • 你能把网页的网址贴出来试试看吗?
    • 它不是面向公众的网址。该网站只能从公司内部访问。我想我现在已经使用跟随兄弟计算出了 Xpath。我只是想试试我的代码,然后我会粘贴答案。
    • 以下 Xpath 有效: //span[@class="gwt-InlineLabel" and contains(text(), 'Projects')]/following-sibling::*/div[@class= "gwt-HTML" 和包含(text(), 'Add...')]
    【解决方案3】:

    我与开发人员交谈,她说该按钮是项目跨度类的兄弟。我应该使用以下兄弟来获得按钮。我现在已经设法让它工作了。 这是我的 XPATH:

    driver.find_element_by_xpath(//span[@class="gwt-InlineLabel" and contains(text(), 'Projects')]/following-sibling::*/div[@class="gwt-HTML" and contains(text(), 'Add...')]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-15
      • 1970-01-01
      相关资源
      最近更新 更多