python - Selenium Webdriver wait.until is not working -


so starting use selenium webdriver , encountering problem until method selenium.webdriver.support.wait. below code:

from selenium import webdriver import time selenium.webdriver.support.select import select selenium.webdriver.support.wait import webdriverwait   url = "http://shop.uclastore.com/courselistbuilder.aspx"  driver = webdriver.firefox() driver.get(url) time.sleep(1)  departments = select(driver.find_element_by_id('cldeptselectbox')) department in departments.options:     # select department     departments.select_by_value(department.get_attribute('value'))       elem = driver.find_element_by_xpath("//select[@id='clcourseselectbox']//option")     print elem     print elem.get_attribute('value')      wait = webdriverwait(driver, 10, 1.0)     wait.until(lambda driver: driver.find_element_by_xpath("//select[@id='clcourseselectbox']//option"))      # time.sleep(1)      elem = driver.find_element_by_xpath("//select[@id='clcourseselectbox']//option")     print elem     print elem.get_attribute('value')     print 

the problem when print out statements before , after get:

<selenium.webdriver.remote.webelement.webelement object @ 0x108a4af50> 0 <selenium.webdriver.remote.webelement.webelement object @ 0x108a4af90> 0  <selenium.webdriver.remote.webelement.webelement object @ 0x108a4afd0> 0 <selenium.webdriver.remote.webelement.webelement object @ 0x108a4af10> 0 

where when comment out wait.until code , uncomment time.sleep, following:

<selenium.webdriver.remote.webelement.webelement object @ 0x10378de90> 0 <selenium.webdriver.remote.webelement.webelement object @ 0x10378de50> 84082  <selenium.webdriver.remote.webelement.webelement object @ 0x10378df90> 0 <selenium.webdriver.remote.webelement.webelement object @ 0x103767110> 87846 

so far, 1 possibility thinking wait found elem imply should not 0 value second print statement. however, not case. not know happening , need figure out.

problem

your wait waiting until driver.find_element_by_xpath("//select[@id='clcourseselectbox']//option") returns element. wrong thing test for.

the way page written, <select> elements "campus" , "term" populated <option> elements , have 1 <option> preselected. <select> "department" populated <option> has nothing preselected. <select> elements "course" , "section" not populated.

the <select> element "course" becomes populated when "department" chosen. however, before <select> populated, does contain placeholder <option> element contains text "loading...". whenever //select[@id='clcourseselectbox']//option, hit. in version of code wait.until, find placeholder <option> exists before javascript code on page has had chance replace real options. in version of code time.sleep(1) give time javascript work. however, note the wait.until call have asked do.

solution

you wait until value of option not 0:

wait.until(lambda driver: driver.find_element_by_xpath(     "//select[@id='clcourseselectbox']//option").get_attribute("value") != "0") 

note get_attribute requires round-trip between script , browser in addition find_element... round-trip. following code same thing uses 1 round-trip:

def cond(driver):     return driver.execute_script("""     var option = document.queryselector("select#clcourseselectbox>option");     return option.value !== "0";     """)  wait.until(cond) 

the code above has been tested.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

jquery - Keeping Kendo Datepicker in min/max range -