0

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?

2
  • where is the error getting thrown? Commented Jan 12, 2017 at 14:14
  • the rows.get(0).click(); Commented Jan 12, 2017 at 15:38

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

I removed the customer.click() that caused some error, but you are completly right :) still the getText() appears empty in my console... the row size is 1
For debugging, instead of printing text, maybe you can print outerHtml of each row to see what you get: row.getAttribute("outerHTML")
Also check outerHTML of customer
ahh good tip thanks :) seems to be at the correct position also for customer <div class="md-text ng-binding"> FirstCustomer </div>
hmm.. if you get the correct html for customer (with 2 div:s inside?), I cannot think of why row size could be 1.
|
1

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.

2 Comments

Yes I tried that before, but I get an UnexpectedTagNameException: Element should have been "select" but was "md-option"
That's my bad, I didn't catch that in the original HTML code. I have no other idea except perhaps to try using sendKeys to drop-down the listbox instead of click? I'm not sure, but perhaps sending it a down-arrow, since your ultimate goal is just to always select the first available option.
1

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

isDisplayed()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.