How to select dropdown menu option value using Selenium (Python)

I need to select an item from the dropdown menu.

For example:

<select id="fruits01" class="select" name="fruits"> <option value="0">Choose your fruits:</option> <option value="1">Banana</option> <option value="2">Mango</option> </select> 

1) First I have to click on it. I will do it:

 inputElementFruits = driver.find_element_by_xpath("//select[id='fruits']").click() 

2) After that, I need to choose a good item, say, Mango .

I tried to do this with inputElementFruits.send_keys(...) but this did not work.

+136
python selenium web-scraping selenium-webdriver webdriver
Oct 23 '11 at 16:40
source share
10 answers

If your click does not start any ajax call to populate your list, you actually do not need to click.

Just find the item, and then list the options by selecting the options you need.

Here is an example:

 from selenium import webdriver b = webdriver.Firefox() b.find_element_by_xpath("//select[@name='element_name']/option[text()='option_text']").click() 

You can read more:
https://sqa.stackexchange.com/questions/1355/unable-to-select-an-option-using-seleniums-python-webdriver

+89
Nov 01 2018-11-11T00:
source share

Selenium provides a convenient Select class for working with select -> option constructs:

 from selenium import webdriver from selenium.webdriver.support.ui import Select driver = webdriver.Firefox() driver.get('url') select = Select(driver.find_element_by_id('fruits01')) # select by visible text select.select_by_visible_text('Banana') # select by value select.select_by_value('1') 

See also:

+243
Feb 19 '15 at 17:47
source share

First you need to import the Select class, and then you need to instantiate the Select class. After creating an instance of the Select class, you can follow the selection methods for that instance to select options from the drop-down list. Here is the code

 from selenium.webdriver.support.select import Select select_fr = Select(driver.find_element_by_id("fruits01")) select_fr.select_by_index(0) 
+20
Nov 23 '17 at 13:00
source share

I tried a lot of things, but my drop was inside the table, and I could not perform the simple select operation. Only the solution below worked. Here I highlight drop down elem and press the down arrow until the desired value is obtained -

  #identify the drop down element elem = browser.find_element_by_name(objectVal) for option in elem.find_elements_by_tag_name('option'): if option.text == value: break else: ARROW_DOWN = u'\ue015' elem.send_keys(ARROW_DOWN) 
+6
Nov 24 '16 at 6:55
source share

You do not need to click anything. Use xpath search or whatever you want, then use submit keys

For your example: HTML:

 <select id="fruits01" class="select" name="fruits"> <option value="0">Choose your fruits:</option> <option value="1">Banana</option> <option value="2">Mango</option> </select> 

Python:

 fruit_field = browser.find_element_by_xpath("//input[@name='fruits']") fruit_field.send_keys("Mango") 

It.

+3
Nov 20 '18 at 20:50
source share

You can use CSS selector combination well

 driver.find_element_by_css_selector("#fruits01 [value='1']").click() 

Change 1 in the attribute = value css selector to the value corresponding to the desired fruit.

+3
Mar 19 '19 at 8:39
source share
 from selenium.webdriver.support.ui import Select driver = webdriver.Ie(".\\IEDriverServer.exe") driver.get("https://test.com") select = Select(driver.find_element_by_xpath("""//input[@name='n_name']""")) select.select_by_index(2) 

It works great

+2
Apr 12 '17 at 13:08 on
source share

The best way is to use the selenium.webdriver.support.ui.Select class to work with a drop-down list, but for some time it does not work properly due to a development problem or other HTML problems.

In this type of situation, you can also choose an alternative solution using execute_script() , as shown below: -

 option_visible_text = "Banana" select = driver.find_element_by_id("fruits01") #now use this to select option from dropdown by visible text driver.execute_script("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", select, option_visible_text); 
+1
Aug 28 '16 at 13:42 on
source share

Works with parameter value:

 from selenium import webdriver b = webdriver.Firefox() b.find_element_by_xpath("//select[@class='class_name']/option[@value='option_value']").click() 
+1
Jun 16 '19 at 18:34
source share
  1. List item

public class ListBoxMultiple {

 public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe"); WebDriver driver=new ChromeDriver(); driver.get("file:///C:/Users/Amitabh/Desktop/hotel2.html");//open the website driver.manage().window().maximize(); WebElement hotel = driver.findElement(By.id("maarya"));//get the element Select sel=new Select(hotel);//for handling list box //isMultiple if(sel.isMultiple()){ System.out.println("it is multi select list"); } else{ System.out.println("it is single select list"); } //select option sel.selectByIndex(1);// you can select by index values sel.selectByValue("p");//you can select by value sel.selectByVisibleText("Fish");// you can also select by visible text of the options //deselect option but this is possible only in case of multiple lists Thread.sleep(1000); sel.deselectByIndex(1); sel.deselectAll(); //getOptions List<WebElement> options = sel.getOptions(); int count=options.size(); System.out.println("Total options: "+count); for(WebElement opt:options){ // getting text of every elements String text=opt.getText(); System.out.println(text); } //select all options for(int i=0;i<count;i++){ sel.selectByIndex(i); Thread.sleep(1000); } driver.quit(); } 

}

-2
Jan 11 '19 at 10:36
source share



All Articles