如何解决 ElementClickInterceptedException 错误?

乔西

我有以下代码:

from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select, WebDriverWait
from selenium.common.exceptions import TimeoutException

driver = webdriver.Firefox()
driver.get('https://www.flashscore.co.uk/tennis/atp-singles/australian-open-2021/results')
wait = WebDriverWait(driver, 5)
try:
    wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@id='onetrust-accept-btn-handler']"))).click()
except TimeoutException:
    pass
exit_loop = False
while not exit_loop:
    try:
        wait.until(EC.element_to_be_clickable((By.XPATH, f"//a[@class='event__more event__more--static']"))).click()
    except TimeoutException:
        exit_loop = True

第一个 try/except 块是摆脱导致ElementClickInterceptedException 错误的“隐私”横幅,它可以正常工作。但是,在 while 循环中的下一个 try/except 块中,我仍然遇到以下两个类似错误之一:

Exception has occurred: ElementClickInterceptedException
Message: Element <a class="event__more event__more--static" href="#"> is not clickable at point (623,976) because another element <span class="loadingOverlay"> obscures it

或者:

Exception has occurred: ElementClickInterceptedException
Message: Element <a class="event__more event__more--static" href="#"> is not clickable at point (623,976) because another element <div id="onetrust-banner-sdk" class="otFlat ot-iab-2 bottom vertical-align-content ot-buttons-fw"> obscures it

我尝试为第二个错误添加另一个 try/except 块:

try:
    wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@id='onetrust-banner-sdk']"))).click()
except TimeoutException:
    pass

然而这行不通。我怀疑我无法点击这个元素,所以我走错了路......

由于错误中的元素似乎都不是我可以单击的按钮 - 我应该如何处理它们?


编辑:

我已经更新了代码以包含滚动:

driver = webdriver.Firefox()
driver.maximize_window()
driver.get('https://www.flashscore.co.uk/tennis/atp-singles/australian-open-2021/results')
wait = WebDriverWait(driver, 5)
try:
    wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@id='onetrust-accept-btn-handler']"))).click()
except TimeoutException:
    pass
driver.execute_script("var scrollingElement = (document.scrollingElement || document.body);scrollingElement.scrollTop = scrollingElement.scrollHeight;")
exit_loop = False
while not exit_loop:
    try:
        wait.until(EC.element_to_be_clickable((By.XPATH, "//a[@class='event__more event__more--static']"))).click()
    except TimeoutException:
        exit_loop = True

但是,我在尝试的两次运行中都遇到了错误。

游轮潘迪

乔西,

在单击之前,您需要向下滚动到页面末尾 show more matches

向下滚动的代码:

driver.execute_script("var scrollingElement = (document.scrollingElement || document.body);scrollingElement.scrollTop = scrollingElement.scrollHeight;")

然后你可以有这个代码:

wait.until(EC.element_to_be_clickable((By.XPATH, f"//a[@class='event__more event__more--static']"))).click()

您还应该全屏启动浏览器:

driver.maximize_window()

在加载 URL 之前。

更新 1:

driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.flashscore.co.uk/tennis/atp-singles/australian-open-2021/results")
wait = WebDriverWait(driver, 10)
try:
    wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@id='onetrust-accept-btn-handler']"))).click()
except TimeoutException:
    pass
exit_loop = False
while not exit_loop:
    try:
        driver.execute_script("var scrollingElement = (document.scrollingElement || document.body);scrollingElement.scrollTop = scrollingElement.scrollHeight;")
        sleep(1)
        wait.until(EC.element_to_be_clickable((By.XPATH, "//a[@class='event__more event__more--static']"))).click()
    except TimeoutException:
        exit_loop = True

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章