How can I select a dropdown menu item (value or text) using Selenium and Python? -
How can I select a dropdown menu item (value or text) using Selenium and Python? -
this question has reply here:
selenium - python - drop-down menu alternative value 2 answersfirst time posting!
i'm brand new python , selenium, i'm trying automate basic test , can't find reply problem.
on main ebay.com page, i'm trying take "all categories" dropdown menu , take "dolls & bears" alternative (option value = "237"). when execute script, menu accessed, "dolls & bears" alternative not selected. test not homecoming errors. have tried using select_by_visible_text.
here code. appreciate help!
from selenium import webdriver selenium.webdriver.common.keys import keys driver = webdriver.firefox() driver.get("http://www.ebay.com") assert "electronics" in driver.title elem = driver.find_element_by_id("gh-ac") elem.send_keys("funny bear") driver.find_element_by_id("gh-cat").click()
def select_a_value(select):
select.select_by_value("237").click()
welcome stack overflow!
you close code. "select" class can instantiated, not library of functions. here's working version of script:
from selenium import webdriver selenium.webdriver.support.select import select import time driver = webdriver.firefox() driver.get("http://www.ebay.com") assert "electronics" in driver.title elem = driver.find_element_by_id("gh-ac") elem.send_keys("funny bear") dropdown_web_element = driver.find_element_by_id("gh-cat") select_box = select(dropdown_web_element) time.sleep(1) select_box.select_by_value("237")
notice phone call select passes in value (a webelement in case) instantiate object. can @ selenium source code figure out how it's called (python27/lib/site-packages/selenium/webdriver/support/select.py on windows).
also, added time.sleep(1) in there. have stumbled on 1 of frustrating things selenium. asynchronous loading of info on websites can lead tests failing because run faster humans typically click buttons. info might not there when point in script! right way deal dynamically wait reasonable amount of time until element looking there. bit outside scope of asked, dealing problem enough, i'm sure.
good luck!
python select selenium drop-down-menu
Comments
Post a Comment