Clicking Selenium doesn't trigger an event on website (python)

Sometimes, when I use selenium to click on a specific link on a page, the click passes, but the website does not respond to the click. For example, here is a situation where I try to move 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 does he always click the script, but the website sometimes changes his page? Is there something to do with angular? I doubt this has anything to do with the OS, but I'm on Mac OS X.

I'm not sure, and I would really like to figure out how to avoid a click failure, especially because I think I click and wait in selenium mode.

+4
1

, , - . , " " - > .

: div.loader, :

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 to navigate to the next day')

    # THIS IS THE KEY FIX
    wait.until(ec.invisibility_of_element_located((By.CSS_SELECTOR, "div.loader")))

    # wait until the next day loads
    wait.until(
        ec.text_to_be_present_in_element((By.CSS_SELECTOR, '.date-selector > span'), next_date.strftime('%m/%d/%Y'))
    )

-, - "". .

+3

Source: https://habr.com/ru/post/1609059/


All Articles