selenium 无法修复ElementClickInterceptedException错误

py49o6xq  于 2023-01-13  发布在  其他
关注(0)|答案(1)|浏览(142)

好吧,这有点尴尬,因为我以前在这里问过类似的问题,但我尝试了建议的解决方案,即(等到元素可点击),但它没有工作。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import ElementClickInterceptedException, ElementNotInteractableException, TimeoutException, WebDriverException, NoSuchElementException
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep
import re
import pandas as pd

def get_links(link):

    driver = webdriver.Firefox()
    driver.get(link)
    driver.implicitly_wait(50)
    sleep(5)

    _flag = True
    knt = 0
    while _flag:
        try:
        
            WebDriverWait(driver, 50).until(EC.invisibility_of_element((By.XPATH, "//a[contains(class='ot-buttons-fw')]")))
            WebDriverWait(driver, 50).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='event__more event__more--static']")))
        
            driver.find_element_by_xpath("//*[contains(text(), 'Show more matches')]").click()
            print("works here!")
            print("clicked....................................")
            sleep(5)
            _flag = True
            #tmp = driver.find_elements_by_xpath("//span[contains(text(), 'NBA - Pre-season')]")
            #if len(tmp) > 0:
                #print("Found them!")
                #_flag = False
            if knt > 5: # For testing
                print("Nuff clicked")
                _flag = False
        except(ElementNotInteractableException):
            print("Error!")
            _flag = False
        
    driver.close()
    return None

link = "https://www.flashscore.com/basketball/usa/nba/results/"

_ = get_links(link)

由于某种原因,我一直得到一个ElementClickInterceptedException错误在driver.find_element_by_xpath("//*[contains(text(), 'Show more matches')]").click()行。任何帮助可以做请

jmp7cifd

jmp7cifd1#

您的元素与其他元素重叠,导致出现ElementClickInterceptedException错误。
在执行代码之前,请使用以下代码段关闭cookie弹出窗口:

def get_links(link):

    driver = webdriver.Firefox()
    driver.get(link)
    driver.implicitly_wait(50)
    sleep(5)

    #here, close popup
    if(len(driver.find_elements_by_id('onetrust-accept-btn-handler'))>0):
        driver.find_element_by_id('onetrust-accept-btn-handler').click()

    _flag = True
    knt = 0
    while _flag:
        ....
        ....

并删除以下行:

WebDriverWait(driver, 50).until(EC.invisibility_of_element((By.XPATH, "//a[contains(class='ot-buttons-fw')]")))

这是无效的xpath表达式,不需要,已按if(popup accept)条件处理。

相关问题