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

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

问题描述

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

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代码如下

下面的代码正在使用

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

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.

您需要注意几件事.如果是类似登录页而不是 find_element_by_id() find_element_by_name()的页面,您必须诱使

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 中找到详细的讨论>在:

    You can find a detailed discussion on ElementNotInteractableException in:

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

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