selenium的异常:selenium.common.exceptions.ElementClickInterceptedException: Message: element click inte

报错信息如下:大致意思就是当前元素是不可以点击,但是确实存在在页面上,有可能是被loading覆盖了

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted:

解决思路:

1.强制等待,多等待几秒能解决问题,但是不灵活

# 可以强制等待
import time
time.sleep()

2.通过selenium调用JS直接操作

js = driver.find_element(By.CSS_SELECTOR, 'xxx')
driver.execute_script("arguments[0].click();", js)

3.显示等待:

里使用的visibility_of_element_located,区别于presence_of_element_located
visibility_of_element_located:找到元素后元素的宽高必须大于0才执行;
presence_of_element_located:找到元素后直接执行,也许元素被蒙层遮住,或者loading遮住会造成无法点击

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

try:
  element = WebDriverWait(driver, 5).until(ec.visibility_of_element_located(loc))
except TimeoutException:
  element.click()



 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
selenium.common.exceptions.ElementClickInterceptedException是一个常见的异常,表示在尝试点击一个元素时,该元素被其他元素所遮挡或拦截。这通常发生在页面上有多个重叠的元素时。 解决这个异常的方法有以下几种: 1. 使用等待机制:在点击元素之前,使用显式等待来确保元素可见并可点击。可以使用`WebDriverWait`类和`expected_conditions`模块来实现等待机制。 ```python from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By # 等待元素可点击 wait = WebDriverWait(driver, 10) element = wait.until(EC.element_to_be_clickable((By.XPATH, "xpath_of_element"))) element.click() ``` 2. 使用JavaScript点击:如果无法通过普通的点击方法点击元素,可以尝试使用JavaScript来模拟点击操作。 ```python element = driver.find_element_by_xpath("xpath_of_element") driver.execute_script("arguments[0].click();", element) ``` 3. 模拟鼠标操作:如果以上方法仍然无法解决问题,可以尝试使用`ActionChains`类来模拟鼠标操作,例如移动到元素上并点击。 ```python from selenium.webdriver.common.action_chains import ActionChains element = driver.find_element_by_xpath("xpath_of_element") actions = ActionChains(driver) actions.move_to_element(element).click().perform() ``` 请注意,以上方法中的`"xpath_of_element"`需要替换为实际元素的XPath表达式。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值