硒 - 元素不可见错误 - 定期发生

时间:2016-12-14 22:20:16

标签: 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");
});

1 个答案:

答案 0 :(得分:1)

您可能只需要等待一小段时间让元素有机会变得可见。

顺便说一下,你不想使用那个jQuery函数取消隐藏元素,因为没有用户会使用该函数取消隐藏元素。仅自动化用户可以做和将要做的事情。

相关问题