简体   繁体   English

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

[英]Can't click the button - element not visible

I'm using selenium and chrome drive and i can't click the button, I tried different approaches but nothing: 我正在使用硒和镀铬驱动器,但我无法单击按钮,我尝试了其他方法,但是没有:

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']"));

error massage: 错误信息:

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

code: 码:

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

thanks Amir 谢谢阿米尔

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

I didnt reproduce it but you should be able to get it to work like this: 我没有重现它,但您应该能够使它像这样工作:

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

This approach is working for me using Selenium V2.52.0 使用Selenium V2.52.0对我来说这种方法有效

Use some implicit wait default in your script to locate the element by selenium before throwing the exception 在引发异常之前,在脚本中使用一些implicit wait默认值来按硒定位元素

Like 喜欢

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

Use explicit wait until your element get visible and then perform click 使用显式等待,直到您的元素可见,然后执行单击

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();

You are automating a web-based application built using AngularJS. 您正在使用AngularJS自动化基于Web的应用程序。

In such a case, your click() events should be targeted to the web elements which have ng-click attribute. 在这种情况下,您的click()事件应针对具有ng-click属性的Web元素。

Try clicking on the anchor link instead of the button. 尝试单击锚链接而不是按钮。

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

Looks like there are multiple web elements in the DOM with same xpath and the first element is not visible although Firebug would highlight visible element (It typically happens with Responsive websites) 看起来DOM中有多个具有相同xpath的Web元素,并且第一个元素不可见,尽管Firebug会突出显示可见元素(通常发生在响应式网站中)

You can try following: 您可以尝试以下操作:

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;    
                    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM