How to select/get drop down option in Selenium 2
Take a look at the section about filling in forms using webdriver in the selenium documentation and the javadoc for the Select class.
To select an option based on the label:
Select select = new Select(driver.findElement(By.xpath("//path_to_drop_down")));
select.deselectAll();
select.selectByVisibleText("Value1");
To get the first selected value:
WebElement option = select.getFirstSelectedOption()
How to select a drop-down menu value with Selenium using Python?
Unless your click is firing some kind of ajax call to populate your list, you don't actually need to execute the click.
Just find the element and then enumerate the options, selecting the option(s) you want.
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 in:
https://sqa.stackexchange.com/questions/1355/unable-to-select-an-option-using-seleniums-python-webdriver
Select from dropdown in selenium-webdriver javascript
In selenium-webdriver JavaScript you don't have "Select" class. You just need to simply click on drop-down option by passing correct css/xpath.
let element = driver.wait(until.elementLocated(By.css(cssLocator)));
element.click();
How to select a option from drop down with div tag/class?.selenium
Below code worked for me
WebElement selectMyElement = driver.findElement(this.getObject(By.Id("Id of Your DropDown")));
selectMyElement.click();
Actions keyDown = new Actions(driver);
keyDown.sendKeys(Keys.chord(Keys.DOWN, Keys.DOWN, Keys.ENTER)).perform();
How to select an option from the dropdown menu using Selenium and Python
To get the fares for all combinations of stations in Metro-North using Selenium and python selecting the option ANSONIA from the From Staion drop-down-menu you need to induce WebDriverWait for the element_to_be_clickable()
and you can use the following Locator Strategies:
Using
CSS_SELECTOR
:driver.get("http://as0.mta.info/mnr/schedules/sched_form.cfm")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select#Vorig_station"))).click()
select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select#Vorig_station"))))
print([o.text for o in select.options])
select.select_by_visible_text('ANSONIA')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[value='fares']"))).click()Using
XPATH
:driver.get("http://as0.mta.info/mnr/schedules/sched_form.cfm")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='Vorig_station']"))).click()
select = Select(WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='Vorig_station']"))))
print([o.text for o in select.options])
select.select_by_visible_text('ANSONIA')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@value='fares']"))).click()Console Output:
['ANSONIA', 'APPALACHIAN TRAIL', 'ARDSLEY-ON-HUDSON', 'BEACON', 'BEACON FALLS', 'BEDFORD HILLS', 'BETHEL', 'BOTANICAL GARDEN', 'BRANCHVILLE', 'BREAKNECK RIDGE', 'BREWSTER', 'BRIDGEPORT', 'BRONXVILLE', 'CANNONDALE', 'CHAPPAQUA', 'COLD SPRING', 'CORTLANDT', 'COS COB', 'CRESTWOOD', 'CROTON FALLS', 'CROTON-HARMON', 'DANBURY', 'DARIEN', 'DERBY', 'DOBBS FERRY', 'DOVER PLAINS', 'EAST NORWALK', 'FAIRFIELD', 'FAIRFIELD METRO', 'FLEETWOOD', 'FORDHAM', 'GARRISON', 'GLENBROOK', 'GLENWOOD', 'GOLDENS BRIDGE', 'GRAND CENTRAL', "GREEN'S FARMS", 'GREENWICH', 'GREYSTONE', 'HARLEM - 125TH ST.', 'HARLEM VALLEY-WINGDALE', 'HARRISON', 'HARTSDALE', 'HASTINGS-ON-HUDSON', 'HAWTHORNE', 'IRVINGTON', 'KATONAH', 'LARCHMONT', 'LUDLOW', 'MAMARONECK', 'MANITOU', 'MARBLE HILL', 'MEADOWLANDS SPORTS COMPLEX', 'MELROSE', 'MERRITT 7', 'MILFORD', 'MORRIS HEIGHTS', 'MOUNT KISCO', 'MOUNT PLEASANT', 'MT VERNON EAST ', 'MT VERNON WEST', 'NAUGATUCK', 'NEW CANAAN', 'NEW HAMBURG', 'NEW HAVEN', 'NEW ROCHELLE', 'NH-STATE ST.', 'NOROTON HEIGHTS', 'NORTH WHITE PLAINS', 'OLD GREENWICH', 'OSSINING', 'PATTERSON', 'PAWLING', 'PEEKSKILL', 'PELHAM', 'PHILIPSE MANOR', 'PLEASANTVILLE', 'PORT CHESTER', 'POUGHKEEPSIE', "PURDY'S", 'REDDING', 'RIVERDALE', 'RIVERSIDE', 'ROWAYTON', 'RYE', 'SCARBOROUGH', 'SCARSDALE', 'SEYMOUR', 'SOUTH NORWALK', 'SOUTHEAST', 'SOUTHPORT', 'SPRINGDALE', 'SPUYTEN DUYVIL', 'STAMFORD', 'STRATFORD', 'TALMADGE HILL', 'TARRYTOWN', 'TENMILE RIVER', 'TREMONT', 'TUCKAHOE', 'UNIVERSITY HEIGHTS', 'VALHALLA', 'WAKEFIELD', 'WASSAIC', 'WATERBURY', 'WEST HAVEN', 'WESTPORT', 'WHITE PLAINS', 'WILLIAMS BRIDGE', 'WILTON', 'WOODLAWN', 'YANKEES-E153 ST.', 'YONKERS']
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import SelectBrowser Snapshot:
Is there a way to select an option from dropdown other than using 'Select' class in Selenium?
Yes. The other way is, click on the dropdown menu and select the option from the dropdown using click method as below:
WebElement ele = driver.findElement(By.xpath("<Xpath of dropdown element>")); // To find the dropdown web element
List<WebElement> options = driver.findElements(By.xpath("<Xpath of dropdown Options")); // To find the dropdown options
ele.click(); // To click on the dropdown element
options.get(<index>).click(); //To click on the option..
How to extract the text of the selected option of a Dropdown Menu using Selenium and Python
first_selected_option
first_selected_option()
returns the first selected option in this select tag (or the currently selected option in a normal select).
Seems you were pretty close. To extract the textContent of the default selected <option>
you can use the first_selected_option
property to identify the element and you can extract the option text as per the solution below:
Code Block:
select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[@id='lstOperation_Key' and @name='lstOperation_Key']")))) //selecting tag
element = select.first_selected_option
print(element.text)
# or
print(element.get_attribute("innerHTML"))
How to select an option from drop down using Selenium WebDriver C#?
You must create a select element object from the drop down list.
using OpenQA.Selenium.Support.UI;
// select the drop down list
var education = driver.FindElement(By.Name("education"));
//create select element object
var selectElement = new SelectElement(education);
//select by value
selectElement.SelectByValue("Jr.High");
// select by text
selectElement.SelectByText("HighSchool");
More info here
Related Topics
How to Draw the Same Moving Image Multiple Times
Difference Between Java Se/Ee/Me
In Java, How to Check If a String Contains a Substring (Ignoring Case)
Aggregation Versus Composition
When and Why JPA Entities Should Implement the Serializable Interface
Swingutilities.Invokelater() Why Is It Needed
How to Properly Match Varargs in Mockito
Format Double Value in Scientific Notation
Overriding Private Methods in Java
How to Get the Size of an Array, a Collection, or a String in Java
How to Get Out of While Loop in Java with Scanner Method "Hasnext" as Condition
How to Set the System Time in Java
How to Allow Introducing Only Digits in Jtextfield
Can't Find a Way to Color the Mandelbrot-Set the Way I'm Aiming For