1. 程式人生 > 實用技巧 >python-selenium元素操作,定位到元素報錯selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

python-selenium元素操作,定位到元素報錯selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

用selenium可以定位到一個元素,但是click()報錯,在介面可以點選該元素。程式碼報錯為:selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

排查問題思路

1.確定能定位到元素

2.判斷元素是否可以點選

#判斷元素是否可以點選
#利用顯示等待
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()

driver.get("http://somedomain/url_that_delays_loading")

#判斷元素是否可以點選
def isclickable(xpath):
    try:
        WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, xpath)))
        return True
    except :
        return False

  結果返回是false

解決辦法:

改變定位的元素。

報錯時定位的是svg元素,改為定位button元素後不報錯