使用 selenium 函数按名称查找元素时元素不可交互,但在使用 xpath 方法查找元素时有效
Element not Interactable when using the selenium function find element by name but works when using find element by xpath method
我有一个要登录的网页。当我使用按名称查找元素的方法查找元素时,出现元素不可交互错误,但是当我使用按 xpath 查找元素时,它工作正常并且没有错误。谁能解释一下为什么在通过名称方法找到时无法与元素交互?
用户 ID、密码和登录元素也存在同样的问题。
我确信即使按名称方法使用网站也已加载并可以使用。
下面是网页登录的截图
我的HTML代码如下
下面是我正在使用的代码
import xlrd
import openpyxl
import requests
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import ChromeOptions, Chrome
opts = webdriver.ChromeOptions()
opts.add_experimental_option("detach", True)
def login_to_Portal(mBrowser):
mBrowser.find_element_by_id("userNameTrucks")
mBrowser.find_element_by_id("userNameTrucks").clear()
mBrowser.find_element_by_id("userNameTrucks").send_keys("xxxxx")
mBrowser.find_element_by_xpath("/html/body/div/router-view/section/div[2]/ul/li[4]/input")
#mBrowser.find_element_by_name("password").clear()
mBrowser.find_element_by_xpath("/html/body/div/router-view/section/div[2]/ul/li[4]/input").send_keys("xxxxx")
mBrowser.find_element_by_xpath("/html/body/div/router-view/section/div[2]/ul/li[5]/button").click()
#mBrowser.find_element_by_class_name("au-target").click()
#mBrowser.find_element_by_name("target")
#mBrowser.find_element_by_name("target").click()
mBrowser = webdriver.Chrome(executable_path = r'C:\chromedriver_win32\chromedriver.exe', options = opts )
mBrowser.get("https://grouptrucksportal.volvo.com/gpp/index.html")
time.sleep(10)
mBrowser.find_element_by_xpath("/html/body/div/compose[1]/section/div[1]/map/area[1]")
mBrowser.find_element_by_xpath("/html/body/div/compose[1]/section/div[1]/map/area[1]").click()
time.sleep(3)
mBrowser.find_element_by_xpath("/html/body/div/compose[3]/section/div[2]/div/ul/li[4]/a")
mBrowser.find_element_by_xpath("/html/body/div/compose[3]/section/div[2]/div/ul/li[4]/a").click()
time.sleep(5)
login_to_Portal(mBrowser)
现在我正在使用 xpatha,一切正常。
当我按名称使用查找元素时,它失败并显示不可交互错误
这个错误信息...
ElementNotInteractableException: Message: element not interactable
...表示您尝试与之互动的 在那一刻不可互动(不处于可互动状态)。
此错误的两 (2) 个主要原因是:
- 您正在尝试与 wrong/mistaken 元素进行交互。
- 或者您甚至在元素变为 可点击 / 可交互.
之前就过早地调用了 click()
分析与解决
您需要注意几件事。对于 element_to_be_clickable()
.
这样的网站,如 Truck Dealer Online once you navigate to the Login page instead of find_element_by_id()
or find_element_by_name()
you have to induce
例如,要将字符序列发送到 用户名 字段,您可以使用以下任一项 :
使用ID
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "userNameTrucks"))).send_keys("SrinivasVenkataram")
使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#userNameTrucks"))).send_keys("SrinivasVenkataram")
使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='userNameTrucks']"))).send_keys("SrinivasVenkataram")
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
参考资料
您可以在以下位置找到关于 ElementNotInteractableException 的详细讨论:
我有一个要登录的网页。当我使用按名称查找元素的方法查找元素时,出现元素不可交互错误,但是当我使用按 xpath 查找元素时,它工作正常并且没有错误。谁能解释一下为什么在通过名称方法找到时无法与元素交互?
用户 ID、密码和登录元素也存在同样的问题。 我确信即使按名称方法使用网站也已加载并可以使用。 下面是网页登录的截图
我的HTML代码如下
下面是我正在使用的代码
import xlrd
import openpyxl
import requests
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import ChromeOptions, Chrome
opts = webdriver.ChromeOptions()
opts.add_experimental_option("detach", True)
def login_to_Portal(mBrowser):
mBrowser.find_element_by_id("userNameTrucks")
mBrowser.find_element_by_id("userNameTrucks").clear()
mBrowser.find_element_by_id("userNameTrucks").send_keys("xxxxx")
mBrowser.find_element_by_xpath("/html/body/div/router-view/section/div[2]/ul/li[4]/input")
#mBrowser.find_element_by_name("password").clear()
mBrowser.find_element_by_xpath("/html/body/div/router-view/section/div[2]/ul/li[4]/input").send_keys("xxxxx")
mBrowser.find_element_by_xpath("/html/body/div/router-view/section/div[2]/ul/li[5]/button").click()
#mBrowser.find_element_by_class_name("au-target").click()
#mBrowser.find_element_by_name("target")
#mBrowser.find_element_by_name("target").click()
mBrowser = webdriver.Chrome(executable_path = r'C:\chromedriver_win32\chromedriver.exe', options = opts )
mBrowser.get("https://grouptrucksportal.volvo.com/gpp/index.html")
time.sleep(10)
mBrowser.find_element_by_xpath("/html/body/div/compose[1]/section/div[1]/map/area[1]")
mBrowser.find_element_by_xpath("/html/body/div/compose[1]/section/div[1]/map/area[1]").click()
time.sleep(3)
mBrowser.find_element_by_xpath("/html/body/div/compose[3]/section/div[2]/div/ul/li[4]/a")
mBrowser.find_element_by_xpath("/html/body/div/compose[3]/section/div[2]/div/ul/li[4]/a").click()
time.sleep(5)
login_to_Portal(mBrowser)
现在我正在使用 xpatha,一切正常。 当我按名称使用查找元素时,它失败并显示不可交互错误
这个错误信息...
ElementNotInteractableException: Message: element not interactable
...表示您尝试与之互动的
此错误的两 (2) 个主要原因是:
- 您正在尝试与 wrong/mistaken 元素进行交互。
- 或者您甚至在元素变为 可点击 / 可交互. 之前就过早地调用了
click()
分析与解决
您需要注意几件事。对于 element_to_be_clickable()
.
find_element_by_id()
or find_element_by_name()
you have to induce 例如,要将字符序列发送到 用户名 字段,您可以使用以下任一项
使用
ID
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "userNameTrucks"))).send_keys("SrinivasVenkataram")
使用
CSS_SELECTOR
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#userNameTrucks"))).send_keys("SrinivasVenkataram")
使用
XPATH
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='userNameTrucks']"))).send_keys("SrinivasVenkataram")
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
参考资料
您可以在以下位置找到关于 ElementNotInteractableException 的详细讨论: