Selenium Webdriver: Element Not Visible Exception

二次信任 提交于 2019-11-29 21:02:16

问题


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


回答1:


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

One is under <div class="loginPopup">

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

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

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



回答2:


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:

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



回答3:


There are 3 occurrences of id="_loginButton".

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

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



回答4:


Webdriver may throw an ElementNotVisible exception in-case there are multiple elements with the same locator and if Webdriver has already operated upon one of the element matching the locator.

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



回答5:


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


        }
    }
}



回答6:


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

This worked for me: (I use c#)

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



回答7:


You could try:

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

Or

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


来源:https://stackoverflow.com/questions/28820820/selenium-webdriver-element-not-visible-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!