ElementNotInteractableException:消息:使用 Selenium Python 在搜索字段中发送文本时元素不可交互错误

2024-02-29

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

下面是我的代码:

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")

![已上传错误图片][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)

我认为问题是我最终无法找到该元素。 所以,这就是该元素不可交互的原因。 顺便说一句,网站链接:https://thelubricantoracle.castrol.com/industrial/en-DE# https://thelubricantoracle.castrol.com/industrial/en-DE#我想使用搜索按钮。

有人可以帮忙吗?


这个错误信息...

ElementNotInteractableException: Message: element not interactable

...意味着网页元素 https://stackoverflow.com/questions/52782684/what-is-the-difference-between-webdriver-and-webelement-in-selenium/52805139#52805139此时,您尝试与之交互的人不可交互(不处于可交互状态)。

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

  • 要么您正在尝试与错误/错误的元素进行交互。
  • 或者你正在调用click()甚至在元件转动之前就太早了可点击的 / 可互动的.

发送一个字符序列在搜索字段中你必须诱导WebDriver等待 https://stackoverflow.com/questions/49775502/webdriverwait-not-working-as-expected/49775808#49775808为了element_to_be_clickable()你可以使用以下内容定位策略 https://stackoverflow.com/questions/48369043/official-locator-strategies-for-the-webdriver/48376890#48376890:

代码块:

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:\WebDrivers\chromedriver.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")

浏览器快照:


参考

您可以在以下位置找到一些相关的详细讨论:

  • selenium.common.exceptions.ElementNotInteractableException:消息:使用 Selenium Python 单击元素时元素不可交互 https://stackoverflow.com/questions/55235367/selenium-common-exceptions-elementnotinteractableexception-message-element-not/55236397#55236397
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ElementNotInteractableException:消息:使用 Selenium Python 在搜索字段中发送文本时元素不可交互错误 的相关文章

随机推荐