无法单击按钮 - 元素不可见

时间:2017-05-16 11:23:23

标签: java html selenium-webdriver

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

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>

感谢Amir

5 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

在脚本中使用一些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();

答案 3 :(得分:0)

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

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

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

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

答案 4 :(得分:0)

看起来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;    
                    }
相关问题