scorpion
scorpion

Reputation: 247

Selenium: Element is not visible when trying to select option in drop-down list?

How can I select an item Option 3 in drop down as below?

<span class="k-widget k-dropdown k-header form-control required" style="padding: 0px;" unselectable="on" role="listbox" aria-haspopup="true" aria-expanded="true" tabindex="0" aria-owns="assignee_listbox" aria-disabled="false" aria-readonly="false" aria-busy="false" aria-activedescendant="assignee_option_selected">
    <span class="k-dropdown-wrap k-state-default" unselectable="on">
	<select id="assignee" class="form-control required" style="padding: 0px; display: none;" name="assignedUserId" data-role="dropdownlist" title="">
		<option value="28941">Option 1</option>
		<option value="28938">Option 2</option>
		<option value="28940">Option 3</option>
		<option value="28942">Option 4</option>
		<option value="28943" selected="selected">Option 5</option>
		<option value="28939">Option 6</option>
	</select>
    </span>
</span>

I tried to select the option 3 on drop-down list, below is my code:

public Page selectAsignee(String asignee){
        try{
            WebElement dropdownAsignee = connector.waitForControl(SBConstant.XPATH,dropdownAssignee,3);
            // My xPath is //select[@id='assignee']
            Select select = new Select(dropdownAsignee);
            select.selectByVisibleText("Option 3");
            return this;

        }catch (StaleElementReferenceException s){
            s.toString();
        }
        return this;
    }

But it's unable to select option 3 although web driver can detect the select with id ="assignee". After run this code, it throws the error like this:

org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with

I'm hoping someone can point out an error on my part that will make this all better.

Upvotes: 3

Views: 18929

Answers (4)

drkthng
drkthng

Reputation: 6909

You're already doing a good job,

BUT

The problem lies with your style-display attribute set to "none"

style="padding: 0px; display: none;"

Nothing displayed means nothing is "visible" to Selenium, hence you get the ElementNotVisibleException.

What you can try

1)

Telling from the classes of your span elements (k-dropdown, k-dropdown-wrap etc.) there is a possibility, that the dropdown is "operated" by other elements. You could inspect your site if you find a div or list elements that also contain information about your options. Something like:

<ul class="someClass" someOtherAttributes>
    <li>
        <a href="someHypertextRef">Option 1</a>
    </li>
    <li>
        <a href="someHypertextRef">Option 2</a>
    </li>
    <li>
        <a href="someHypertextRef">Option 3</a>
    </li>
</ul>

Then you would need to work with these other elements. I saw this kind of select elements that never change their display attribute several times already.

2)

Is there a "button" near the dropdown? Or can you click the dropdown itself? If yes do it, and inspect if the display attribute changes. If it does change to "block" or sth similar, you just need to click the dropdown element before you try to find the option.

Upvotes: 2

Juhi Saxena
Juhi Saxena

Reputation: 1217

Replace your code with this , I hope it will work for you . Updated : Either please wait for element visiblity , if any click or event makes it visible or make it visible by below code

 //Use JavascriptExecutor to make the element visible  
((JavascriptExecutor)wd).executeScript("jQuery('#assignee').css('display','block')");
     Select select = new Select(wd.findElement(By.xpath(".//select[@id='assignee']")));
     select.selectByVisibleText("Option 3");

Upvotes: 1

Ketan Pardeshi
Ketan Pardeshi

Reputation: 1

SelectElement select1 = new SelectElement(driver.FindElement(By.TagName("select"))); select1.SelectByText("28940");

try this

Upvotes: 0

Saritha G
Saritha G

Reputation: 2608

Try like this:

  WebElement element = driver.findElement(By.xpath("//span[@class='kdropdown-wrap k-state-default']/select"));
  Select select = new Select(element);
  select.selectByVisibleText("Option 3");

Upvotes: 0

Related Questions