当前位置: 动力学知识库 > 问答 > 编程问答 >

ElementNotVisible Exception Selenium Webdriver Java

问题描述:

I am working on a public site:

http://preview.harriscountyfws.org/

The logic shown below encounters the ElementNotVisibleException.

However, if you uncomment out the two lines that start with "//", as well as comment out the two lines above that code, the code works fine (however this is for a different dropdown than the one I want).

So I am puzzled.

I am able to select from "Rainfall in the last" dropdown just fine.

But using exactly the same method, I cannot select from the "Gage by Location" dropdown.

How do I select from the Gage by Location dropdown?

package sdfwe.sdwd.qsde;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

public class Gfgsdrfffde {

public static void main(String[] args) {

System.out.println("Running Tests....");

// Optional, if not specified, WebDriver will search your path for chromedriver.

System.setProperty("webdriver.chrome.driver", "C:\\ChromeDriver\\chromedriver.exe");

WebDriver driver = new ChromeDriver();

driver.get("http://preview.harriscountyfws.org/");

driver.manage().window().maximize();

WebElement we;

String searchText;

WebElement dropdown;

//if you comment out the next two lines after these comments and

// uncomment out the commented lines below, the code works

searchText = "110:A100_110 Clear Creek @45";

dropdown = driver.findElements(By.cssSelector("span.k-widget")).get(3);

// searchText = "24 Hours";

// dropdown = driver.findElements(By.cssSelector("span.k-widget")).get(1);

System.out.println("Attempting Dropdown Click....");

dropdown.click();

System.out.println("Finished running Dropdown Click....");

Sleep(4);

//Select dd2 = new Select(driver.findElement(By.id("ddlTimeSpan")));

//dd2.selectByVisibleText(searchText);

we = driver.findElement(By.xpath("//li[text()='" + searchText + "']"));

if (we != null) {

we.click();

}

System.out.println("Finished Selecting Dropdown....");

//driver.quit();

}

public static void Sleep(int i)

{

try { Thread.sleep(i * 300); }

catch(InterruptedException ie) { System.out.println("Unexpected error in sleep"); }

}

}

网友答案:

I tried your code and you need to make these changes for it to work:

    searchText = "110:A100_110 Clear Creek @ I-45";
    dropdown = driver.findElement(By.cssSelector("span[aria-owns='ddlGage_listbox']"));

Here's the full working code based from your example above.

       public static void main(final String[] args) {
    System.out.println("Running Tests....");
    // Optional, if not specified, WebDriver will search your path for
    // chromedriver.
    System.setProperty("webdriver.chrome.driver", "C:\\ChromeDriver\\chromedriver.exe");
    final WebDriver driver = new ChromeDriver();
    driver.get("http://preview.harriscountyfws.org/");
    driver.manage().window().maximize();
    WebElement we;
    String searchText;
    WebElement dropdown;
    // if you comment out the next two lines after these comments and
    // uncomment out the commented lines below, the code works
    searchText = "110:A100_110 Clear Creek @ I-45";
    dropdown = driver.findElement(By.cssSelector("span[aria-owns='ddlGage_listbox']"));
    // searchText = "24 Hours";
    // dropdown =
    // driver.findElements(By.cssSelector("span.k-widget")).get(1);
    System.out.println("Attempting Dropdown Click....");
    dropdown.click();
    System.out.println("Finished running Dropdown Click....");
    Sleep(4);
    // Select dd2 = new Select(driver.findElement(By.id("ddlTimeSpan")));
    // dd2.selectByVisibleText(searchText);
    we = driver.findElement(By.xpath("//li[text()='" + searchText + "']"));
    if (we != null) {
        we.click();
    }
    System.out.println("Finished Selecting Dropdown....");
    // driver.quit();
} 

Also, for simple selenium scripts, you might want to check https://github.com/codezombies/easytest.

Here's the same script using easytest framework.

public static void main(String[] args) throws Exception {

    try(EasyTest easy = new EasyTest(DriverType.CHROME)) {


        easy.start("http://preview.harriscountyfws.org/");

        easy.newPage(page -> {
           page.click("span[aria-owns='ddlGage_listbox']");

           final String searchText = "110:A100_110 Clear Creek @ I-45";
           page.executeIn("#ddlGage_listbox", container -> {
               page.click("//li[text()='" + searchText + "']");
           });

        });
    }
}
网友答案:

You can try this:

    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("select#ddlGage")));
    Select gageByLocationSelect = new Select(driver.findElement(By.cssSelector("select#ddlGage")));
    gageByLocationSelect.selectByVisibleText("110:A100_110 Clear Creek @ I-45");

Hope this will help you!!

分享给朋友:
您可能感兴趣的文章:
随机阅读: