Selenium Webdriver:元素不可见的异常

Nik*_*ack 6 java selenium automation qa selenium-webdriver

这是我的代码,点击本网站上的简单登录按钮

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

    }
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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

小智 15

你在这个页面上有两个带有给定xpath的按钮,首先是不可见的,这就是你得到ElementNotVisibleException的原因

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

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

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

By.xpath("//div[@class='page']//div[@id='_loginButton']")
Run Code Online (Sandbox Code Playgroud)