Selenium: Item not showing when selecting an option in a dropdown?
How do I select an item Option 3
in the dropdown as shown 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 option 3 in the dropdown, 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 cannot select option 3, although the web driver can detect a selection with id = "assignee". After running this code, it throws an error like this:
org.openqa.selenium.ElementNotVisibleException: The element is not currently visible and therefore cannot interact with
I hope someone can point out a mistake on my part that will make this all better.
source to share
You are already doing a good job
BUT
The problem is that the style-style attribute is set to "none"
style="padding: 0px; display: none;"
Nothing is displayed because nothing is visible "Selenium", so you get an ElementNotVisibleException.
What you can try
1)
Speaking from the classes of your span elements (k-dropdown, k-dropdown-wrap, etc.), there is a possibility that the dropdown is "controlled" by other elements. You can check your site if you find divs 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 will need to work with these other elements. I've seen selectors like this that don't change their display attribute several times already.
2)
Is there a "button" next to the dropdown? Or can you click on the dropdown menu itself? If so, do so and check if the display attribute changes. If it changes to "block" or sth, you just need to click the dropdown item before trying to find that parameter.
source to share
Replace your code with this, I hope it works for you. Updated: either wait for the element to be visible if any click or event will make it visible or make it visible 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");
source to share