javascript - Selenium - 元素不可见错误 - 周期性发生

标签 javascript java selenium

我正在用 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/

相关文章:

javascript - Prototype.js 类系统的替代品

javascript - 将 AWIS 调用更改为异步函数

java - 将字符串解析为资源名称

java - 将 BufferedWriter.write 延迟到另一个线程

java - 如何比较 selenium webdriver 中的 HTML 弹出窗口中是否存在部分文本?

javascript - 正则表达式的否定

java - Java 中的函数覆盖重载

java - Selenium 更新后 ExpectedConditions 将不起作用

java - 是否可以在一个 Java 类中运行 Java 应用程序和 Junit 测试?

javascript - 数据未从 localStorage PHP 发布。 Angular