我正在用 Java 编写 Selenium 测试来测试一个简单的 PHP Web 应用程序。其中大部分涉及填写字段。
如果我尝试连续运行测试两次(或更多次),则会收到以下错误:
org.openqa.selenium.ElementNotVisibleException:
Session ID: ef10680c-429b-4312-9d69-41496e7dce6a
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:127)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:93)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:42)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:274)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)
at com.midamcorp.insurance.TesterNGTest.test(TesterNGTest.java:83)
该声明是正确的,元素不可见 - 问题是它不可见的原因。一开始这个元素被 Javascript 隐藏。然而,显示它就像单击链接元素一样简单。我也在测试中执行了此“点击”操作。
@BeforeClass
public static void setUpClass() throws Exception {
System.setProperty("webdriver.gecko.driver","C://geckodriver/geckodriver.exe");
driver = new FirefoxDriver();
driver.get(BASE);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void test() throws Exception {
// login
driver.findElement(By.id("ssn")).clear();
driver.findElement(By.id("ssn")).sendKeys("user");
driver.findElement(By.id("user")).clear();
driver.findElement(By.id("user")).sendKeys("pass");
driver.findElement(By.name("login")).click();
// insurance screen
driver.findElement(By.name("phone")).clear();
driver.findElement(By.id("phone")).sendKeys("phone");
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys("email");
driver.findElement(By.id("empSSN")).sendKeys("ssn");
driver.findElement(By.id("address")).clear();
driver.findElement(By.id("address")).sendKeys("address");
driver.findElement(By.id("city")).clear();
driver.findElement(By.id("city")).sendKeys("city");
new Select(driver.findElement(By.id("state"))).selectByVisibleText("Missouri");
driver.findElement(By.id("zip")).sendKeys("63780");
driver.findElement(By.id("genderFemale")).click();
if(!(driver.findElement(By.cssSelector("input[type='checkbox")).isSelected())) { driver.findElement(By.cssSelector("input[type='checkbox")).click(); }
driver.findElement(By.cssSelector("input[value='1']")).click();
new Select(driver.findElement(By.name("employmentLocation"))).selectByVisibleText("Central Office");
driver.findElement(By.cssSelector("input.btn.btn-primary")).click();
// enrollment
// the code below is supposed to see if the container for the radio buttons is hidden, if so, click the link to display them
if( !(driver.findElement(By.id("healthOnly")).isDisplayed()) ) {
driver.findElement(By.cssSelector("a[href='#healthOnly']")).click();
}
// the line below is the one that throws the error; the link noted above is not being clicked and thus the container with the radio buttons remains hidden
List<WebElement> insuranceOptions = driver.findElements(By.cssSelector("#healthOnly input[type='radio']"));
insuranceOptions.get(rand.nextInt(insuranceOptions.size() - 1)).click();
insuranceOptions = driver.findElements(By.cssSelector("input[name='visionID']"));
insuranceOptions.get(rand.nextInt(insuranceOptions.size() - 1)).click();
我的简单 Javascript/jQuery 函数用于显示和隐藏元素。当我手动单击它时,它按预期工作。
$(".insuranceOptionsToggle").click(function(e){
e.preventDefault();
$($(this).attr("href")).toggle();
$(this).toggleClass("active");
});
最佳答案
您可能只需要短暂的等待即可让元素有机会变得可见。
顺便说一句,您不想使用该 jQuery 函数来取消隐藏元素,因为没有用户会使用该函数来取消隐藏元素。仅自动化用户可以执行和愿意执行的操作。
关于javascript - Selenium - 元素不可见错误 - 周期性发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41153238/