How can i select a value from the dropdown using selenium web driver enter image description here
1 Answer 1
Here the drop down is implemented using a ul
and li
, so you can't use WebDriver's Select
class, it will throw UnexpectedTagNameException
if you do so.
What you need to do in this case, write a custom select method like below to help you.
public void select_by_index(int index){
driver.findElement(By.xpath("//ul[@role='listbox']")).click();
WebElement _element = driver.findElement(By.xpath("(//ul[@role='listbox']//li)[" + index+ "]"));
_element.click();
}
So what we are doing here:
- Click the drop down to show all the options, so that they are visible.
- We are dynamically creating xpath for different element in the drop-down list and then clicking on the elements.
- If you pass 2 as argument, it will select 'Change of Mind' from the drop-down and so on.
Try this and let us know if this works in your case. The code is just for illustration purpose.
-
The code in your example does not seem to be c# code. However OP asks about c#Alexey R.– Alexey R.2018年07月16日 10:44:19 +00:00Commented Jul 16, 2018 at 10:44
-
My intention was to give an idea how to solve the problem. The code is written in Java, however the same can be translated to C#. Hope that's not a problem.Kshetra Mohan Prusty– Kshetra Mohan Prusty2018年07月16日 11:33:35 +00:00Commented Jul 16, 2018 at 11:33