使用 selenium 函数按名称查找元素时元素不可交互,但在通过 xpath 方法使用查找元素时有效 [英] Element not Interactable when using the selenium function find element by name but works when using find element by xpath method

查看:16
本文介绍了使用 selenium 函数按名称查找元素时元素不可交互,但在通过 xpath 方法使用查找元素时有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,我正在尝试登录它.当我使用按名称查找元素的方法来查找元素时,我得到元素不可交互错误,但是当我通过 xpath 使用查找元素时,它工作正常并且没有错误.谁能解释一下为什么通过 name 方法找到它时无法与元素交互?

I have webpage where I am trying to login to it. When I use find element by name method to find the elements I get element not interactable error but when i use find element by xpath it works fine and no error. Can anyone explain me why it is not able to interact with element when found by name method?

对于用户 ID、密码和登录元素也观察到同样的问题.我敢肯定,即使按名称使用方法网站已加载并可以使用.下面是网页登录截图

Same issue is observed for User ID, Password and Login elements. I am sure even when using by name method website is loaded and is ready for use. Below is the screenshot of the Webpage login

我的 HTML 代码如下

My HTML code is as below

下面是我使用的代码

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_win32chromedriver.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,一切正常.当我使用按名称查找元素时,它失败并出现不可交互错误

Now am using xpatha and everything works fine. When i use find element by name it fails with not interactable error

推荐答案

这个错误信息...

ElementNotInteractableException: Message: element not interactable

...暗示 WebElement 您尝试与之交互的对象此时不可交互(不处于可交互状态).

...implies that the WebElement with whom you are trying to interact isn't interactable (isn't in interactable state) at that moment.

此错误的两个 (2) 主要原因是:

The two(2) main reasons for this error are:

  • 您正在尝试与错误/错误的元素进行交互.
  • 或者您在元素变为 clickable/interactable 之前过早地调用 click().
  • Either you are trying to interact with a wrong/mistaken element.
  • Or you are invoking click() too early even before the element turns clickable / interactable.

您需要注意几件事.如果您导航到 Login 页面而不是 find_element_by_id()find_element_by_name() 你必须诱导 WebDriverWait.

There are a couple of things you need to take care. In case of websites like 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 WebDriverWait for the element_to_be_clickable().

例如,要将字符序列发送到 用户名 字段,您可以使用以下任一 定位器策略:

As an example, to send a character sequence to the user name field you can use either of the following Locator Strategies:

  • 使用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")
    

  • 注意:您必须添加以下导入:

  • Note : You have to add the following imports:

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

  • 您可以在 ElementNotInteractableException 在:

    这篇关于使用 selenium 函数按名称查找元素时元素不可交互,但在通过 xpath 方法使用查找元素时有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆