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 valueCommentedJun 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') 
3
  • whats the time.sleep for?
    – nilesh
    CommentedJun 25, 2015 at 22:35
  • @nilesh what time.sleep? ;)
    – alecxe
    CommentedJun 25, 2015 at 22:46
  • This worked thanks! If you don't mind, could you elaborate on exactly what frame is and why I have to switch to it?CommentedJun 26, 2015 at 4:26
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() 

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.