【问题标题】:Selenium webdriver identify button and clicking on it using JavaSelenium webdriver识别按钮并使用Java单击它
【发布时间】:2023-04-06 14:00:02
【问题描述】:

目前我正在尝试使用 selenium 和 java 自动填写一个相当大的网络表单。我正在使用 Firefox 网络驱动程序。我安装了 firebug 和 xpath 检查器(在 firefox 上)。

我的问题是登录网站后单击按钮。登录部分没问题。它是用户名/密码并提交操作。

我尝试了各种 FindElement 方法,我认为它应该是 Xpath。我只是不知道如何识别按钮,因为它没有名称属性。

xpath 检查器:id('sectiondiv_81')/x:input

<div id="sectiondiv_81" class="ax-boxcontent" style="">
<input class="Button" type="button" onclick="window.location='/Apps/app_editopportunity.jsp?appid=103184&nextlevel=1&companyid=&opportunityid=0&opportunitytypeid=13997&defaultstatusid=12824&providertoken=Sml3L21uJDk!HkITNmAzGDUlHhd2GxoXH2JcU0JFal9AQVxWHwQIUxkbbFFfXhI~'" value="Apply for a scientific research project">
<br>
</div>

它涉及值为“申请科学研究项目”的按钮。

我将不胜感激。

【问题讨论】:

    标签: java selenium webdriver


    【解决方案1】:

    如果这是 内部的唯一按钮,您可能可以通过 XPath 找到它,例如:

    ("//div[contains(@id, 'sectiondiv_81')]/input[contains(@class, 'Button')]")
    

    或

    ("//input[contains(@value, 'scientific research project')]")
    

    【讨论】:

    • 为回复欢呼。我得到这个错误:线程“main”中的异常org.openqa.selenium.NoSuchElementException:无法找到元素:{“method”:“xpath”,“selector”:“//div [contains(@id,'sectiondiv_81' )]/input[contains(@class, 'Button')]"} 我使用的代码: driver.findElement(By.xpath("//div[contains(@id, 'sectiondiv_81')]/input[contains( @class, 'Button')]")).click();
    • 这可能是因为多个元素具有相同的 Xpath。尝试一下价值搜索方法,我已经更新了我的答案..
    【解决方案2】:

    我们可以查看完整的网站进行分析吗?

    我建议你使用 Firebug 和 Firepath 来检查 XPath:

    //div[contains(@id, 'sectiondiv_81')]/input[contains(@class, 'Button')]
    

    您将能够检查网站中与您的 Xpath 相符的“部分”或“部分”

    你也可以使用 Firebug 获取 XPath,右键单击 -> Inspect in FirePath

    【讨论】:

      【解决方案3】:

      如果可能,您应该避免使用数字、索引,因为它会使 xpath 查询对于可能的 ui 更改不灵活。 最好使用任何元素属性,这会给你独特的结果。

      //input[@type='button'][contains(@value, 'Apply')]
      

      【讨论】:

        【解决方案4】:

        &lt;input&gt; 元素是具有 ID 的 &lt;div&gt; 元素的兄弟,因此请尝试使用 XPath following-sibling 命令,如下所示:

        WebElement myButton = driver.findElement(By.xpath("//div[@id='sectiondiv_81']/following-sibling::input"));
        myButton.click();
        

        这来自 W3C XPath spec。

        【讨论】:

          猜你喜欢
          • 2021-06-25
          • 1970-01-01
          • 2021-11-09
          • 2020-09-04
          • 2016-01-30
          • 1970-01-01
          • 2020-03-16
          • 2016-11-14
          • 2012-08-23
          相关资源
          最近更新 更多