Sometimes when I'm using selenium to click on a particular link on a page, the click goes through but the website does not respond to the click. For example, here is the situation when I try to navigate between dates on the statistics page on nba.com.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
import datetime
import time
def go_to_next_day(driver, next_date):
for 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 to navigate to the next day')
# wait 30 seconds until the 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 to 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 to next day!!!'
end_date = datetime.datetime.now()
current_date = datetime.datetime.strptime('2014-10-03', '%Y-%m-%d')
# after page loads, just click right until you get to current date
while current_date <= end_date:
# do something interesting on the page, modeled as a 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 to day {}'.format(current_date)
driver.quit()
print 'Done'
Why is it that the script always clicks, but the website only changes its page sometimes? Is it something to do with angular? I doubt it has anything to do with the OS, but I'm on a Mac OS X.
I'm not sure and would really like to figure out how to avoid the click failing, especially because I think I click and wait in the selenium way.