python - ElementNotInteractableException : element not interactable error while trying to search for stock on www. finanzen.net 使用 Selenium 和 Python

标签 python selenium xpath css-selectors webdriverwait

我正在尝试使用 selenium 在 www.finanzen.net 上搜索股票,但总是得到

ElementNotInteractableException:元素不可交互

from selenium import webdriver

import time

chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs", prefs)
chrome_options.add_argument("start-maximized")
driver = webdriver.Chrome(options=chrome_options, executable_path=r'F:\chromedriver.exe')

driver.get('https://www.finanzen.net/')
time.sleep(5)
cookie_banner_button = driver.find_element_by_xpath("//button[@onclick='cookieBannerOverlayClick();']")
cookie_banner_button.click()

search_field = driver.find_element_by_xpath("//input[@class='search-input']")


#search_field.click()
search_field.send_keys('bmw')
search_field.submit()
time.sleep(5)
driver.quit()

HTML:

html-code of element

最佳答案

您使用的 xpath 指向两个元素,并且它所指向的第一个元素不可交互,因此出现异常。
已找到元素的正确 xpath,请引用以下代码:

from selenium import webdriver

import time

chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs", prefs)
chrome_options.add_argument("start-maximized")
driver = webdriver.Chrome(options=chrome_options, 
executable_path=r'F:\chromedriver.exe')

driver.get('https://www.finanzen.net/')
time.sleep(5)
cookie_banner_button = driver.find_element_by_xpath("//button[@onclick='cookieBannerOverlayClick();']")
cookie_banner_button.click()

search_field = driver.find_element_by_xpath("//div[@class='shadow']//input[@class='search-input']")
search_field.send_keys('bmw')
search_field.submit()
time.sleep(5)
driver.quit()

关于python - ElementNotInteractableException : element not interactable error while trying to search for stock on www. finanzen.net 使用 Selenium 和 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60930861/

相关文章:

python - 如何比较明喻列值上的两个不同数据框并将值放入其他数据框

python - 是否可以直接将仿射变换矩阵应用于 Mayavi ImageActor 对象?

python - 如何使用selenium选择单选按钮

python - 在 django/selenium Web 测试中创建预认证 session

java - 使用 dom4j 创建带 key 的 xpath

python - 从url中提取数字

Python - 全局名称未定义

java - Selenide 测试因 org/openqa/selenium/NoSuchSessionException 失败

html - 使用 powershell 和 xpath 问题解析 html

python - NetworkX如何访问作为节点的对象的属性