问题
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?
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
My HTML code is as below
Below is the code am using
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)
Now am using xpatha and everything works fine. When i use find element by name it fails with not interactable error
回答1:
This error message...
ElementNotInteractableException: Message: element not interactable
...implies that the WebElement with whom you are trying to interact isn't interactable (isn't in interactable state) at that moment.
The two(2) main reasons for this error are:
- 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.
Analysis and Solution
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:
Using
ID
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "userNameTrucks"))).send_keys("SrinivasVenkataram")
Using
CSS_SELECTOR
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#userNameTrucks"))).send_keys("SrinivasVenkataram")
Using
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
References
You can find a detailed discussion on ElementNotInteractableException in:
- ElementNotInteractableException: Message: element not interactable error sending text in search field using Selenium Python
来源:https://stackoverflow.com/questions/62992942/element-not-interactable-when-using-the-selenium-function-find-element-by-name-b