【问题标题】:Java Code is opening Chrome URL through selenium However not able to Click on buttonJava代码正在通过硒打开Chrome URL但是无法单击按钮
【发布时间】:2019-02-25 15:17:17
【问题描述】:

以下是我编写的用于在 chrome 浏览器中打开 URL 并单击其上的按钮的代码。然而,尽管尝试通过 xpath、类和 CSS 选择器单击按钮,但它没有得到点击。任何建议代码有什么问题? P.S:我是这个社区和 java 的新手。

  public void testUntitledTestCase() throws Exception 
  {
    for (int i = 1; i < 364 ; i++) 
            {
                driver.get("URL+i+");  
                driver.findElement(By.xpath("(.//*[normalize-space(text()) and normalize-space(.)='Docs'])[1]/following::div[12]")).click();
                driver.findElement(By.xpath("//button[@value='graphiql-container']")).click();
                driver.findElement(By.className("execute-button")).click();
                driver.findElement(By.xpath("//button[contains(@class='execute-button')]")).click();
                driver.findElement(By.xpath("//button[@class='execute-button']")).click();
            }
  }

以下是 HTML 部分: CSS 选择器:

graphiql-container > div.editorWrap > div.topBarWrap > div > div.execute-button-wrap > 按钮

X 路径: //*[@id="graphiql-container"]/div[1]/div[1]/div/div[2]/button

类: "button class="execute-button" title="执行查询(Ctrl-Enter)"

【问题讨论】:

  • 有异常跟踪吗?
  • 用相关的 HTML 更新问题
  • 是的,它显示空指针异常,java 错误 64
  • 你能给出一个示例 url,或者它发生的示例 html 吗?另外,哪一行抛出空指针异常?疯狂猜测:检查按钮是否在框架中。如果是,您必须在与按钮交互之前切换到该框架。

标签: javascript java selenium


【解决方案1】:

您可以尝试直接打开 URL 而不放入 for 循环。提供完整的 URL 并检查您是否能够单击该按钮。

driver.get("URL");

【讨论】:

  • 没有循环它会打开 URL,但是在单击按钮时再次出现空指针异常
【解决方案2】:

几个月前,我曾在一个项目中使用 Selenium (Java),并且遇到了一些问题需要处理 - 特别是在测试基于 ajax 的网站时。我最终使用了 Arquillian Graphene——一堆 Selenium 扩展,它们添加了功能并且已经处理了其中的一些问题。你绝对应该看看它:

http://arquillian.org/arquillian-graphene/

除此之外,我还可以分享一些在当时有所帮助的书签:

http://www.testautomationguru.com/selenium-webdriver-how-to-wait-for-expected-conditions-using-awaitility/ http://www.naveenautomationlabs.com/2018/04/mystery-of-staleelementreferenceexcepti.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-06
    • 2019-03-18
    • 2020-12-05
    • 1970-01-01
    • 2021-01-26
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多