javascript - Selenium click does not trigger event on website (python) -


sometimes when i'm using selenium click on particular link on page, click goes through website not respond click. example, here situation when try navigate between dates on statistics page on nba.com.

from selenium import webdriver selenium.webdriver.common.by import selenium.webdriver.support.ui import webdriverwait selenium.webdriver.support import expected_conditions ec  import datetime import time  def go_to_next_day(driver, next_date):     elem in driver.find_elements_by_css_selector('.date-selector i'):         if 'right' in elem.get_attribute('class'):             print 'found next day!'             elem.click()             break     else:         raise valueerror('unable navigate next day')      # wait 30 seconds until next day loads     webdriverwait(driver, 30).until(         ec.text_to_be_present_in_element((by.css_selector, '.date-selector > span'), next_date.strftime('%m/%d/%y'))     )   if __name__ == '__main__':     # go nba.com     driver = webdriver.firefox()     driver.set_window_size(2560, 1600)     driver.get('http://stats.nba.com/scores/#!/10/03/2014')      print 'nba.com loaded. clicking next day!!!'     end_date = datetime.datetime.now()     current_date = datetime.datetime.strptime('2014-10-03', '%y-%m-%d')      # after page loads, click right until current date     while current_date <= end_date:         # interesting on page, modeled sleep         time.sleep(1)          next_date = current_date + datetime.timedelta(days=1)         go_to_next_day(driver, next_date)         current_date = next_date         print 'went day {}'.format(current_date)      driver.quit()     print 'done' 

why script clicks, website changes page sometimes? something angular? doubt has os, i'm on mac os x.

i'm not sure , figure out how avoid click failing, because think click , wait in selenium way.

the problem click not make go next day if current day's data still loading. in other words, if "loading spinner" visible - clicking > button has no effect.

to solve it: wait invisibility of div.loader element containing spinner:

def go_to_next_day(driver, next_date):     wait = webdriverwait(driver, 10)     actions = actionchains(driver)     try:         next_button = wait.until(ec.element_to_be_clickable((by.css_selector, '.date-selector i[class*=right]')))         actions.move_to_element(next_button).click().perform()     except timeoutexception:         raise valueerror('unable navigate next day')      # key fix     wait.until(ec.invisibility_of_element_located((by.css_selector, "div.loader")))      # wait until next day loads     wait.until(         ec.text_to_be_present_in_element((by.css_selector, '.date-selector > span'), next_date.strftime('%m/%d/%y'))     ) 

i'm operating next button bit differently, feel free continue own approach or switch mine - key fix in waiting "spinner" invisibility. works me.


Comments