Home > database >  ElementClickInterceptedException Error Selenium
ElementClickInterceptedException Error Selenium

Time:10-02

I keep getting the ElementClickInterceptedException on this script I'm writing, I'm supposed to click a link that will open a new window, scrape from the new window and close it and move to the next link to scrape, but it just won't work, it gives the error after max 3 link clicks. I saw a similar question here and I tried using wait.until(EC.element_to_be_clickable()) and also maximized my screen but still did not work for me. Here is the site I am scraping from trying to scrape all the games for each day and here is a chunk of the code I'm using

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.common.exceptions import TimeoutException, NoSuchElementException, ElementNotInteractableException, StaleElementReferenceException
from time import sleep

l = "https://www.flashscore.com/"

options = FirefoxOptions()
#options.add_argument("--headless")
driver = webdriver.Firefox(executable_path="geckodriver.exe", 
firefox_options=options)
driver.install_addon('C:\\Windows\\adblock_plus-3.10.1-an fx.xpi')
driver.maximize_window()
driver.get(l)
driver.implicitly_wait(5)
cnt = 0
sleep(5)
wait = WebDriverWait(driver, 20)
a = driver.window_handles[0]
b = driver.window_handles[1]
driver.switch_to.window(a)

# Close Adblock tab
if 'Adblock' in driver.title:
    driver.close()
    driver.switch_to.window(a)
else:
    driver.switch_to.window(b)
    driver.close()
    driver.switch_to.window(a)


var1 = driver.find_elements_by_xpath("//div[@class='leagues--live ']/div/div")

knt = 0
for i in range(len(var1)):
    if (var1[i].get_attribute("id")):
        knt  = 1
        #sleep(2)
        #driver.switch_to.window(driver.window_handles)
    
        var1[i].click()
        sleep(2)
        #var2 = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@classs, 'event__match event__match--last event__match--twoLine')]")))
        print(len(driver.window_handles))
        driver.switch_to.window(driver.window_handles[1])
        try:
            sleep(4)
            driver.close()
            driver.switch_to.window(a)
            #sleep(3)
        except(Exception):
            print("Exception caught")
        #WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.CLASS_NAME, "event__match event__match--last event__match--twoLine")))

sleep(10)
driver.close()

Any ideas to help please.

CodePudding user response:

It looks like the element you are trying to click on is covered by a banner ad or something else like a cookie message.

To fix this you can scroll down to the last element using the following code:

driver.execute_script('\
             let items = document.querySelectorAll(\'div[title="Click for match detail!"]\'); \
             items[items.length - 1].scrollIntoView();'
             )

Add it before clicking on the desired element in the loop. I tried to make a working example for you but it works on chromedriver not gecodriver:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


options = webdriver.ChromeOptions()
# options.add_argument("--headless")
options.add_experimental_option("excludeSwitches", ["enable-automation", "enable-logging"])

service = Service(executable_path='your\path\to\chromedriver.exe')
driver = webdriver.Chrome(service=service, options=options)

wait = WebDriverWait(driver, 5)

url = 'https://www.flashscore.com/'

driver.get(url)
# accept cookies
wait.until(EC.presence_of_element_located((By.ID, 'onetrust-accept-btn-handler'))).click()

matches = driver.find_elements(By.CSS_SELECTOR, 'div[title="Click for match detail!"]')

for match in matches:
    driver.execute_script('\
            let items = document.querySelectorAll(\'div[title="Click for match detail!"]\'); \
            items[items.length - 1].scrollIntoView();'
            )
    match.click()
    driver.switch_to.window(driver.window_handles[1])
    print('get data from open page')
    driver.close()
    driver.switch_to.window(driver.window_handles[0])

driver.quit()

It works in both normal and headless mode

  • Related