【问题标题】:Can't click the button - element not visible无法单击按钮 - 元素不可见
【发布时间】:2017-10-15 10:58:32
【问题描述】:

我正在使用 selenium 和 chrome 驱动器,但无法单击按钮, 我尝试了不同的方法,但没有:

link = driver.findElement(By.xpath("//button[@class='btn buttonContainer arrow']"));

link = driver.findElement(By.xpath("//input[@class='btn buttonContainer arrow']"));

link = driver.findElement(By.cssSelector("input[type='button']"));

link = driver.findElement(By.cssSelector("input[type='button'][value='btn buttonContainer arrow']"));

错误消息:

INFO: Detected dialect: OSS
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: element not visible
  (Session info: chrome=58.0.3029.96)
  (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 10.0.10240 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 15 milliseconds

代码:

<a class="ng-scope" ng-if="!loginCtrl.pageObject.isDebugMode" ng-click="rootCtrl.redirectToRegistration()">
      <button class="btn buttonContainer arrow">
         <span>Enter</span>
      </button>
</a>

感谢阿米尔

【问题讨论】:

  • 将网址粘贴到此按钮,以便您尝试
  • 你想点击哪个按钮?
  • 输入按钮
  • 检查我的答案并更新我。谢谢
  • 没什么意思?得到任何错误?或点击无效

标签: java html selenium-webdriver


【解决方案1】:

看起来 DOM 中有多个具有相同 xpath 的 web 元素,第一个元素不可见,尽管 Firebug 会突出显示可见元素(这通常发生在响应式网站上)

您可以尝试以下操作:

public WebElement getDisplayedElement( WebDriver driver ) {
                    try {   
                        WebElement visibleElement = null;
                        List<WebElement> elements = driver.findElements(locator);
                        for (WebElement element : elements) {
                            boolean isDisplayed = false;
                            boolean isEnabled = false;
                            System.out.println("Count of element(s) from locator " +locator+ " is " +elements.size());
                            isDisplayed = element.isDisplayed();
                            isEnabled = element.isEnabled();
                            if(isDisplayed && isEnabled ) {
                                System.out.println("Found enabled and displayed web element");
                                visibleElement = element;
                                break;
                            }
                        }           
                        return visibleElement;    
                    }

【讨论】:

    【解决方案2】:

    您正在自动化使用 AngularJS 构建的基于 Web 的应用程序。

    在这种情况下,您的click() 事件应该定位到具有ng-click 属性的网络元素。

    尝试点击锚链接而不是按钮。

    link = driver.findElement(By.xpath("//button[@class='btn buttonContainer arrow']/..")); 链接.click();

    【讨论】:

      【解决方案3】:

      在你的脚本中使用一些 implicit wait 默认值,在抛出异常之前通过 selenium 定位元素

      喜欢

      driver.get("your _site_url");               
      driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
      

      使用显式等待,直到您的元素可见,然后执行点击

      WebDriverWait wait = new WebDriverWait(driver, 60);
      wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[@class='btn buttonContainer arrow']")));
      
      driver.findElement(By.xpath("//button[@class='btn buttonContainer arrow']")).click();
      

      【讨论】:

        【解决方案4】:

        我没有复制它,但你应该可以让它像这样工作:

        firefoxDriver.findElement(By.className("buttonContainer")).click();
        

        这种方法适用于我使用 Selenium V2.52.0

        【讨论】:

          【解决方案5】:

          如果你想点击Enter按钮试试:driver.findElement(By.xpath("//button[@class='btn buttonContainer arrow']/span"));

          【讨论】:

          • 谢谢,但我仍然收到错误“元素不可见”
          • Ok :) 至少我们现在能够找到不可见的元素。但这是一个单独的问题。在尝试查找元素之前,请引入一些 wait。你能接受这个答案吗?谢谢
          猜你喜欢
          • 2016-10-15
          • 2022-06-10
          • 1970-01-01
          • 2021-09-22
          • 2016-08-01
          • 2017-10-21
          • 2014-06-13
          • 2020-08-10
          • 1970-01-01
          相关资源
          最近更新 更多