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异常。

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
乘风的头像乘风管理团队
上一篇 2023年8月23日
下一篇 2023年8月23日

相关推荐