Selenium Webdriver: 元素不可见异常

6
这里是我点击这个网站上简单登录按钮的代码。
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();

    }
}

我遇到了以下错误:

异常情况: "main"线程中发生了org.openqa.selenium.ElementNotVisibleException异常:当前元素不可见,无法进行交互 命令执行持续时间或超时时间为:2.05秒

7个回答

17

这个页面上有两个给定xpath的按钮,第一个不可见,所以你会收到“ElementNotVisibleException”异常。

第一个按钮在<div class="loginPopup">下。

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

因此,请更改你的xpath如下,它将解决你的问题:

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

3

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

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

2

有3个id="_loginButton"出现。

使用css选择器下的class="signIn"来获取页面上确切的按钮,其下使用了id="_loginButton"

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

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


        }
    }
}

我遇到了错误信息 "org.openqa.selenium.ElementNotVisibleException: element not visible",需要帮助。 - Kishore Paul

0

Webdriver 可能会抛出 ElementNotVisible 异常,如果有多个具有相同定位器的元素,并且 Webdriver 已经操作了与定位器匹配的一个元素。

在这种情况下,您可以首先使用以下方法获取元素的大小:

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

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

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

0

请确保远程服务器上的窗口足够大,以便元素不会因为空间限制而被隐藏...

对我有用的方法是:(我使用c#)

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

0

你可以尝试:

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

或者

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

欢迎来到SO并感谢您的回答 - 请尝试在提供答案时添加更多上下文信息 - https://stackoverflow.com/help/how-to-answer - Paul Dawson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接