当前位置: 首页 > 知识库问答 >
问题:

Python Webdriver无法使用Xpath定位正确的按钮

糜单弓
2023-03-14

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

我尝试了以下Xpaths,这些Xpaths在span类报告下面找到了add按钮:

//button[div[. = 'Add...']]
//div[@class="gwt-HTML" and contains(text(), "Add...")]
e.g. driver.find_element_by_xpath(//button[div[. = 'Add...']])
//span[@class="gwt-InlineLabel" and contains(text(), 'Projects')]
    <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来定位Add...哪一个是在span类项目下面?谢了。

共有1个答案

薛征
2023-03-14

只需在Chrome中打开网站,右键单击按钮,然后Inspect Element。在窗口的下半部分,您现在应该看到页面的源代码。验证所需的行是否高亮显示,并右键单击它,然后选择“Copy XPath”。然后将其粘贴到driver.find_element_by_xpath(“xpath_string_from_chrome”)

 类似资料:
  • 我的AUT有不同的选项卡来测试和验证不同的WebElements,这些选项卡上有几乎相同的属性。我需要验证一个下拉搜索框,并验证下拉列表中的选项。我的脚本找不到这个下拉搜索框,而xpath在firebug中工作得非常好,可以识别搜索框。 显示的错误为: 请告诉我什么可能是错误的,以及为什么Selenium不能使用xpath定位元素,而它在xpath中运行良好。另外,如果我的xpath不正确,请告诉

  • 下面是selenium找不到元素的代码。 但不知何故,它失败了,对我不起作用。它总是给我以下例外情况:过时的元素引用:元素未附加到页面文档 添加了--GetElementStextList的代码 感谢任何帮助。谢谢

  • 嗨,我是一个相当新的硒。可以有人请建议如何定位和元素在iframe asi是得到错误下面。 带有Iframe标记的DOM按钮元素的屏幕截图 以前编写的代码

  • 这个输入在一个模态div中,不显示(display:none)并且只有当我单击模态的触发器时才显示(display:block)。问题是模态变得可见,但它找不到输入元素。 我试过这个 也 不起作用。导致找不到XPath。

  • 正在获取以下异常消息: Selenium 2.47.1不支持边缘驱动程序instace使用xpath定位元素???请您提供解决方案?

  • 这就是我得到的错误