Jinxi
Jinxi

Reputation: 311

Selenium webdriver: Dropdown element selection throws Element not visible exception

Im using Selenium and Java Webdriver and I'm new to Selenium. I have a similar problem as in this Thread and I tried several approaches. I just want to get the first element of this dropdown (which will get longer soon) and select it to test the input mask with junit. Here is a snippet of the website:

<md-option ng-repeat="customer in settingsCtrl.customers" value="1" tabindex="0" class="ng-scope md-ink-ripple" role="option" aria-selected="true" id="select_option_4" style="" selected="selected">
<div class="md-text ng-binding">FirstCustomer</div>
<div class="md-ripple-container" style=""></div>
</md-option>

I tried following:

WebDriverWait wait = new WebDriverWait(driver, 300);

    WebElement customer = driver.findElement(By.id("select_option_4"));
    //customer.click();

    //wait.wait();

    List <WebElement> rows = customer.findElements(By.tagName("div"));
    System.out.println("row size: " + rows.size());
    // Debug text
    Iterator<WebElement> i  = rows.iterator();
    while(i.hasNext()){
        WebElement row = i.next();
        System.out.println("row text: " + row.getText() );

    }
    rows.get(0).click();

The error msg is as in the thread: ElementNotVisibleException

Any suggestions?

Upvotes: 0

Views: 1365

Answers (3)

Tokci
Tokci

Reputation: 1280

Use below function to verify if element is visible on page or not.

isDisplayed()

Upvotes: 1

Bill Hileman
Bill Hileman

Reputation: 2838

Have you tried instead to treat the dropdown as a Select webelement?

Select custDrop = new Select(driver.findElement(By.id("select_option_4")));

List rows = custDrop.getAllSelectedOptions();

Then all the dropdown values are in the rows string array.

If you merely want to select the first option, regardless:

custDrop..selectByIndex(0);

You can also select by value or by visible text if you know ahead of time what they are.

Upvotes: 1

Linh Nguyen
Linh Nguyen

Reputation: 1138

I think after rows.get(0).click(), the dropdown will be closed, that's why you get the error.

If you want to get all texts from the dropdown, do it before the line of code I mentioned above

Upvotes: 1

Related Questions