3

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.

1
  • The problem lies with your style-display attribute set to "none", you need to do some further investigations on how you can "display" your options -> see my answer Commented Jul 20, 2015 at 9:23

4 Answers 4

2

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.

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

2 Comments

Yes, thank you for your explaination. After click dropdown by itself, I can use drop_down list properly.
If you like, you can mark it as the correct answer then ;-)
1

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");

5 Comments

What's the difference between your code and mine? I try, but still unable to selectVisibleText.
Ohh sorry , I did'nt notice the css , you are applying style display none , this is the problem.
No he is not "applying" the display style. But indeed this is the problem (see my answer). He has to investigate the site further to find a way to make either the current options elements visible or find other elements that will be used when the user uses the dropdown.
@JuhiSaxena injecting the desired state/behavior, as you propose, is definitely an approach. Although personally I always try to be as close as a user can be using the browser manually, but that's because I mostly automate for e2e-testing purposes
@drkthng I completely agree with you , but sometimes we do somethings thats for only our purpose , so if it is the case then he can use my approach but if its not he should automate it from the end user flow.
0

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");

2 Comments

Sorry, even I use your way, driver can detect the Select, but cannot select by Visible Text.
You can use select.selectByIndex(index) by passing specified index. or use select.selectByValue("28941");
0

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

try this

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.