Java Selenium Webdriver:元素不可见异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28820820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:52:43  来源:igfitidea点击:

Selenium Webdriver: Element Not Visible Exception

javaseleniumselenium-webdriverautomationqa

提问by Nik_stack

Here is my code to click a simple login button on this Website

这是我在此网站上单击简单登录按钮的代码

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;    
import org.openqa.selenium.WebDriver;    
import org.openqa.selenium.firefox.FirefoxDriver;    

public class Reports {

    public static void main(String[] args) {

        WebDriver driver = new FirefoxDriver();
        driver.get("https://platform.drawbrid.ge");
        driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
        driver.findElement(By.xpath(".//*[@id='_loginButton']")).click();

    }
}

I am getting following error:

我收到以下错误:

Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 2.05 seconds

线程“main” org.openqa.selenium.ElementNotVisibleException 中的异常:元素当前不可见,因此可能无法与命令持续时间或超时交互:2.05 秒

采纳答案by Dmitry Shyshkin

You have two buttons with given xpath on this page, first is not visible, thats why you are getting ElementNotVisibleException

您在此页面上有两个具有给定 xpath 的按钮,第一个按钮不可见,这就是您收到 ElementNotVisibleException 的原因

One is under <div class="loginPopup">

一个在下 <div class="loginPopup">

Second (the one you need) is under <div class="page">

第二个(你需要的)在 <div class="page">

So change your xpath to look like this, and it will fix your problem:

因此,将您的 xpath 更改为如下所示,它将解决您的问题:

By.xpath("//div[@class='page']//div[@id='_loginButton']")

回答by alecxe

There are even 3 elements with id="_loginButton"on the page, and only one is visible- the one located inside the login form, you can get it by a CSS selector:

id="_loginButton"页面上甚至有 3 个元素,只有一个是可见的- 位于登录表单内的一个,您可以通过CSS 选择器获取它:

By.cssSelector("form#_loginForm div#_loginButton")

回答by Abinaya Veluswamy

There are 3 occurrences of id="_loginButton".

出现 3 次id="_loginButton"

Used the id="_loginButton"under class="signIn"by cssSelector to get the exact button in the page.

通过 cssSelector使用id="_loginButton"underclass="signIn"来获取页面中的确切按钮。

By.cssSelector("div.signIn div#_loginButton")

回答by SelThroughJava

Webdrivermay throw an ElementNotVisibleexception in-case there are multiple elements with the same locator and if Webdriverhas already operated upon one of the element matching the locator.

WebdriverElementNotVisible如果有多个元素具有相同的定位器,Webdriver并且已经对与定位器匹配的元素之一进行了操作,则可能会引发异常。

In such scenarios you can first get the size of the element using

在这种情况下,您可以首先使用

int var_ele_size= driver.findElements(By.xpath("locator")).size();

and then take the first element from the list and click on the element.

然后从列表中取出第一个元素并单击该元素。

driver.findElements(By.xpath("locator")).get(var_ele_size-1).click();

回答by Kishore Paul

public static void Listget (WebDriver driver) throws Exception 

{
    Thread.sleep(5000);
    UtilityMethod.getAppLocaters(driver, "closeicon").click();

    Actions action = new Actions(driver);
    WebElement we = driver.findElement(By.xpath("//li[@class='parent dropdown  aligned-left']"));
    Thread.sleep(5000);
    action.moveToElement(we).build().perform();

    List<WebElement>links = driver.findElements(By.xpath("//span[@class='menu-title']"));
    int total_count = links.size();       
    System.out.println("Total size :=" +total_count);           
     for(int i=0;i<total_count;i++)
        {             
            WebElement  element = links.get(i);
            String text = element.getAttribute("innerHTML");
            System.out.println("linksnameis:="  +text);

            try{
                    File src = new File("D:ReadFile.xlsx");
                    FileInputStream fis = new FileInputStream(src);
                    XSSFWorkbook wb=new XSSFWorkbook(fis);
                    XSSFSheet sh = wb.getSheetAt(0);

                    sh.createRow(i).createCell(1).setCellValue(text);

                    FileOutputStream fos = new FileOutputStream(new File("D:/ReadFile.xlsx"));
                    wb.write(fos);
                    fos.close();
                }
                catch(Exception e)
                {
                    System.out.println(e.getMessage());
                }


        }
    }
}

回答by jacquestheron

Make sure your window on the remote serveris big enough so the elements are not hidden because of space constraints ..

确保你的窗户remote server足够大,这样元素就不会因为空间限制而被隐藏..

This worked for me: (I use c#)

这对我有用:(我使用c#

driver.Manage().Window.Size = new System.Drawing.Size(1928, 1060);

回答by Dor leon

You could try:

你可以试试:

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("your locator value")));

Or

或者

wait.until(ExpectedConditions.ElementIsVisible(By.xpath("your locator value")));