Calling a dropdown menu with selenium and python
I am trying to access a page that requires me to select an option from a dropdown menu.
When I run my atm code, I get an error when it says it couldn't find the drop down item by id. I don't know how to fix this situation as I copy and paste the id elements.
from selenium import webdriver
from selenium.webdriver.support.select import Select
import time
driver = webdriver.Firefox()
driver.get('http://webapp.northampton.edu/coursesearch/default.aspx')
time.sleep(1)
dropdown = driver.find_element_by_id('pg0_V_ddlTerm')
select_box = Select(dropdown)
time.sleep(1)
select_box.select_by_value('2015;S2')
I also tried picking by name, but that also proved fruitless. Once I select the dropdown menu, I try to select the S2 2015 option.
Thanks for your help!
Edit: I entered time.sleep because I thought that maybe the website was not fully loaded by the time it was trying to select the dropdown.
source to share
The select element is inside the iframe, switch to it :
driver.switch_to.frame("cSearch")
dropdown = driver.find_element_by_id('pg0_V_ddlTerm')
select_box = Select(dropdown)
select_box.select_by_value('2015;S2')
source to share
You might be interested in using chaining. From the docs:
ActionChains are a way to automate low-level interactions such as mouse movements, mouse button actions, keystrokes, and context menu interactions.
Example:
dropdown = driver.find_element_by_id('pg0_V_ddlTerm') actions = ActionChains(driver) actions.move_to_element(dropdown) actions.click(dropdown) select_box = Select(dropdown) action.move_to_element(select_box.select_by_value('2015;S2')) action.click(select_box) actions.perform()
source to share