3

I am trying access a page which requires me to select an option from a drop down menu.

When i run my code atm, I get an error where it says it was unable to locate the drop down element by id. I do not know how to remedy this situation, as I am copying and pasting the elements id.

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 selecting by name, but that also proved fruitless. Once I select the dropdown I am attempting to select the option S2 2015.

Thank you for your help!

Edit: I put in the time.sleep because I thought perhaps the website wasn't fully loaded by the time is was trying to select the drop down.

1
  • .select_by_value() returns an element right? You're not capturing that value Commented Jun 26, 2015 at 0:34

2 Answers 2

1

The select element is inside an 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')
Sign up to request clarification or add additional context in comments.

3 Comments

whats the time.sleep for?
@nilesh what time.sleep? ;)
This worked thanks! If you don't mind, could you elaborate on exactly what frame is and why I have to switch to it?
0

You may be interested in using an action chain. From the docs:

ActionChains are a way to automate low level interactions such as mouse movements, mouse button actions, key press, 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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.