ElementNotInteractableException:消息:使用 Selenium Python 在搜索字段中发送文本的元素不可交互错误 [英] ElementNotInteractableException: Message: element not interactable error sending text in search field using Selenium Python

查看:25
本文介绍了ElementNotInteractableException:消息:使用 Selenium Python 在搜索字段中发送文本的元素不可交互错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在网站上使用 send_keys,这给了我元素不可交互的错误.

这是我的代码如下:

导入硒从硒导入网络驱动程序从 selenium.webdriver.chrome.options 导入选项从 selenium.webdriver.common.keys 导入密钥from selenium.webdriver.common.by import By从 selenium.webdriver.support.ui 导入 WebDriverWait从 selenium.webdriver.support 导入 expected_conditions 作为 EC从 selenium.webdriver.support.select 导入选择从 selenium.webdriver.common.action_chains 导入 ActionChains导入时间chrome_optionsme = 选项()chrome_optionsme.add_argument("--incognito")chrome_optionsme.add_argument("--window-size=1920x1080")驱动程序 = webdriver.Chrome(options=chrome_optionsme,可执行文件路径=/Users/chueckingmok/Desktop/html/chromedriver")url='https://thelubricantoracle.castrol.com/industrial/en-DE'driver.get(url)时间.sleep(5)Welcome_sec_one=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,'//*[@id=ctl00_termsPopup_lbConfirm"]')))Welcome_sec_one.click()时间.sleep(5)driver.find_element_by_xpath("//input[@class='search'[@id='txtSearch']").send_keys("示例")

![上传错误图片][1]

这是代码ElementNotInteractableException 回溯(最近一次调用)<ipython-input-2-1415cecd56f9>在<模块>---->1 driver.find_element_by_xpath("//input[@class='search'][@id='txtSearch']").send_keys("示例")/Applications/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py in send_keys(self, *value)477 self._execute(Command.SEND_KEYS_TO_ELEMENT,第478话-->第479话480第481话/Applications/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py 在 _execute(self, command, params)631 参数 = {}第632话-->633返回self._parent.execute(命令,参数)634635 def find_element(self, by=By.ID, value=None):/Applications/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py 在执行(self,driver_command,params)319响应= self.command_executor.execute(驱动程序命令,参数)320 如果响应:-->第321话322 响应['值'] = self._unwrap_value(第323话/Applications/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)第 240 章241 引发异常类(消息,屏幕,堆栈跟踪,警报文本)-->242引发异常类(消息,屏幕,堆栈跟踪)243244 def_value_or_default(self,obj,key,default):ElementNotInteractableException:消息:元素不可交互(会话信息:chrome=83.0.4103.116)

我认为问题在于我最终无法找到该元素.所以,这就是元素不可交互的原因.顺便说一句,网站链接:

I try to use send_keys on a website, which gives me an error of element not interatable.

Here is my code below:

import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
import time

chrome_optionsme = Options()
chrome_optionsme.add_argument("--incognito")
chrome_optionsme.add_argument("--window-size=1920x1080")
driver = webdriver.Chrome(options=chrome_optionsme, 
                          executable_path="/Users/chueckingmok/Desktop/html/chromedriver")

url='https://thelubricantoracle.castrol.com/industrial/en-DE'
driver.get(url)
time.sleep(5)

welcome_sec_one=WebDriverWait(driver,10).until(
    EC.presence_of_element_located((By.XPATH,'//*[@id="ctl00_termsPopup_lbConfirm"]'))
    )
welcome_sec_one.click()

time.sleep(5)



driver.find_element_by_xpath("//input[@class='search'[@id='txtSearch']").send_keys("Example")

![Uploaded the error image][1]

Here is the code 

ElementNotInteractableException           Traceback (most recent call last)
<ipython-input-2-1415cecd56f9> in <module>
----> 1 driver.find_element_by_xpath("//input[@class='search'][@id='txtSearch']").send_keys("Example")

/Applications/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py in send_keys(self, *value)
    477         self._execute(Command.SEND_KEYS_TO_ELEMENT,
    478                       {'text': "".join(keys_to_typing(value)),
--> 479                        'value': keys_to_typing(value)})
    480 
    481     # RenderedWebElement Items

/Applications/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py in _execute(self, command, params)
    631             params = {}
    632         params['id'] = self._id
--> 633         return self._parent.execute(command, params)
    634 
    635     def find_element(self, by=By.ID, value=None):

/Applications/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
    319         response = self.command_executor.execute(driver_command, params)
    320         if response:
--> 321             self.error_handler.check_response(response)
    322             response['value'] = self._unwrap_value(
    323                 response.get('value', None))

/Applications/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=83.0.4103.116)

I think the problem is that I cannot locate the element eventually. So, that's why the element not interactable. btw, the website link: https://thelubricantoracle.castrol.com/industrial/en-DE# I want to use the search button.

Anyone can help?

解决方案

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 point of time.

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.

To send a character sequence with in the search field you you have to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategies:

Code Block:

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

options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:WebDriverschromedriver.exe')
driver.get("https://thelubricantoracle.castrol.com/industrial/en-DE")
WebDriverWait(driver, 20).until(lambda driver: driver.execute_script('return document.readyState') == 'complete')
time.sleep(3)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='button primary' and contains(@id, 'termsPopup_lbConfirm')]"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='search-init']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='search']"))).send_keys("Example")

Browser Snapshot:


Reference

You can find a couple of relevant detailed discussions in:

这篇关于ElementNotInteractableException:消息:使用 Selenium Python 在搜索字段中发送文本的元素不可交互错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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