Selenium 处理 ElementClickInterceptedException: 按钮被弹窗覆盖

当您使用 Selenium 点击一个按钮时,如果按钮被其他弹窗覆盖,可能会抛出 ElementClickInterceptedException 异常。除了 element.is_enabled() 方法,还可以使用以下方法判断按钮是否被弹窗覆盖:

方法 1: 使用 JavascriptExecutor 执行 JavaScript 代码判断按钮可见性

button = driver.find_element_by_xpath('//button[@class='weui-desktop-btn weui-desktop-btn_default']')
is_visible = driver.execute_script('return arguments[0].offsetParent !== null;', button)
if is_visible:
    button.click()

方法 2: 使用 Expected Conditions 判断覆盖弹窗是否可见

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

button = WebDriverWait(driver, 10).until(EC.invisibility_of_element_located((By.XPATH, '//div[@class='weui-cls-modal_wraper abilities-conf-modal']')))
if button.is_displayed():
    button.click()

这些方法都可以帮助您判断按钮是否被其他弹窗覆盖,从而避免出现 ElementClickInterceptedException 异常。

Selenium 处理 ElementClickInterceptedException: 按钮被弹窗覆盖

原文地址: https://www.cveoy.top/t/topic/qqnW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录