raise exception_class(message, screen, stacktrace) selenium.common.exceptions.ElementNotInteractable

问题描述:

使用selenium点击某个元素时发生报错:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=101.0.4951.67)

报错原因:

ElementNotInteractableException异常通常表示元素无法与之交互,也就是无法被点击、输入等。
这可能是由于以下原因之一:

元素被其他元素遮挡。
元素处于不可交互状态,例如元素处于禁用状态或隐藏状态。
元素没有被完全加载或渲染。
点击的位置不正确,例如元素位于屏幕的边缘或滚动区域之外。

解决方法:

以下是一些可能的解决方案:

确保元素没有被其他元素遮挡。可以使用execute_script()方法将元素滚动到视野范围内,或者使用ActionChains类模拟鼠标操作。
确保元素处于可交互状态。可以使用WebDriverWait类等待元素变为可交互状态,或者通过JavaScript来修改元素状态。
确保元素已经被完全加载或渲染。可以使用WebDriverWait类等待元素的加载或渲染完成。
确保点击的位置正确。可以使用location_once_scrolled_into_view属性将元素滚动到视野范围内,或者使用ActionChains类模拟鼠标操作。
以下是一个示例代码,演示如何使用ActionChains类模拟鼠标操作:

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

# 创建WebDriver对象,此处使用Chrome浏览器
driver = webdriver.Chrome()

# 打开目标网页
driver.get("https://www.example.com")

# 等待目标元素加载完成
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "target_element")))

# 创建ActionChains对象,模拟鼠标操作
actions = ActionChains(driver)
actions.move_to_element(element).click().perform()

# 关闭浏览器
driver.quit()

在这个例子中,我们使用WebDriverWait类等待目标元素加载完成,并使用ActionChains类模拟鼠标移动和点击操作。如果元素无法被点击,就会抛出ElementNotInteractableException异常。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值