-2

Please suggest to get the elements present in drop down menu using Selenium WebDriver.

Thank you..

closed as unclear what you're asking by Niels van Reijmersdal, Kate Paulk, Peter M., Lyndon Vrooman, Bruce McLeod Aug 13 '15 at 1:47

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Which language you are using for scripting ? and what is problem your facing in your approach. – QAMember Aug 6 '15 at 7:31
  • Selenium script using java – Kumar Aug 6 '15 at 7:36
  • 1
    You do not give enough context to answer this question. What do you currently have, what problem are you running into? We need info like Testcode, HTML of page and what you tried and researched. – Niels van Reijmersdal Aug 6 '15 at 7:38
  • Thank you for your time and response. I have a drop down down menu in my application where I used Actions class and moveToElement() I'm able to navigate to the sub menus. I'm trying to get all the sub menu items in that drop down menu. Please suggest me how to get those. – Kumar Aug 6 '15 at 7:43
  • can you post html code which you want to fetch the elements so that we can provide an answer.Please update the question with html code – QAMember Aug 6 '15 at 8:14
2

Since drop down contains more than one values, so we need to make selection out of a range of options. So there are three ways of getting or selecting elements from a drop down using the Select class of Selenium webdriver:

  1. Using the index of element in drop down.
  2. Using the Value of element in drop down.
  3. Using the text of element in drop down.

Consider this block of HTML

<html>
<body>
<select id = "designation">
<option value = "MD">MD</option>
<option value = "prog"> Programmer </option>
<option value = "CEO"> CEO </option>
</option>
</select>
<body>
</html>

- Using index of element

You can use the select_by_index(index) method in Python or the equivalent method in Java selectByIndex( ) to get the element by the index position.

In the above block, if I want to select the programmer option then I would achieve this as select_by_index(1).

-Using the value of element

You can use the select_by_value(value) method in Python or the equivalent method in Java selectByValue( )to get the element using the value of the element.

In the above block, if I want to select the programmer option then I would achieve this as select_by_value('prog').

-Using the text of the element

You can use the select_by_visible_text(text_of_option) method in Python or equivalent method in Java selectByVisibleText( ) to get the element using the text with which the drop down element is displayed.

In the above block, if I want to select the programmer option then I would achieve this as select_by_visible_text('Programmer').

If you want to get a list of all the options in the drop down, then it is quite easily achievable using the Python library using the options attribute.

s1=Select(driver.find_element_by_id('designation'))

print(s1.options)

  • This applies for drop down list..using Select method..but I'm trying to work out for drop down menu using Actions class. I'm also able to navigate to any of the drop down menu using moveToElement() but here my concern to get all the dropdown items. pls help.. – Kumar Aug 6 '15 at 8:03
  • 1
    Why are you using Actions class for Drop downs? Any relevant example which you can show?. It's hard to understand from your question. You need to add further description. – demouser123 Aug 6 '15 at 8:04
  • public class Drpdwnmenu { public static void main(String[] args) { WebDriver d = new FirefoxDriver(); d.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); d.get("fatcow.com/"); WebElement menu = d.findElement(By.linkText("Create a Site")); menu.click(); Actions act = new Actions(d); act.moveToElement(menu).perform(); WebElement submenu = d.findElement(By.linkText("Dedicated Servers")); submenu.click(); } } – Kumar Aug 6 '15 at 9:04
  • This is not a drop down. – demouser123 Aug 6 '15 at 9:26
0
Select dropdown = new Select(driver.findElement(By.id("some id")));
dropdown.selectByVisibleText("Some Text");.

This will access control to a dropdown but make sure that you have given enough time using Thread.sleep("3000") command. You can use ByVisibleText(), Byindex(), Byvalue() to get the desired text.

0

https://code.google.com/p/selenium/source/browse/java/client/src/org/openqa/selenium/support/ui/Select.java

here is a class designed for this in the bindigs.

You are looking for the Select class:

https://code.google.com/p/selenium/source/browse/java/client/src/org/openqa/selenium/support/ui/Select.java

You would need to 'find' the actual select element, not the individual options. Find that select element, and let Selenium & the Select class do the rest of the work for you.

You'd be looking for something like (s being the actual select element):

WebElement selectElement = driver.findElement(By.id("s");
Select select = new Select(selectElement);

The Select class has a handy getOptions() method. This will do exactly what you think it does.

List<WebElement> allOptions = select.getOptions();

Now you can do what you want with allOptions.

Source : https://stackoverflow.com/questions/16768318/how-can-i-get-all-elements-from-drop-down-list

If you are looking for a dropdown give the Script enough time to look for the element.

Not the answer you're looking for? Browse other questions tagged or ask your own question.