python - 元素不可交互的 Selenium Python
问题描述
我正在尝试将密钥发送到https://www.wolframalpha.com/上的文本框,但如您所见,我遇到了一个错误。我已经尝试过查看这些元素,但如果您能够提供帮助,我将不胜感激。使用python3.7,我的代码:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(options=options, executable_path=r'C:\Program Files (x86)\Chrome\Application\chromedriver.exe')
driver.get('https://www.wolframalpha.com/')
driver.find_element_by_xpath('_9CcbX').send_keys('1+1')
错误
traceback (most recent call last):
File "c:/Users/gabri/Desktop/BOT/IXL Bot - Gabriel.py", line 15, in <module>
driver.find_element_by_xpath('_9CcbX').send_keys('1+1')
File "C:\Users\gabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\gabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Users\gabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\gabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"_9CcbX"}
(Session info: chrome=88.0.4324.146)
解决方案
首先,您会得到NoSuchElementException
,因为您正在通过 xpath 搜索,并带有类名参数。(_9CcbX
是一个类名)。相反,您可以复制元素的正确 xpath:
- 检查
- 右键单击元素
- 复制 -> 复制完整的 xpath
这会给你:
driver.find_element_by_xpath('/html/body/div/div/div/div/div/div[1]/section/form/div/div/input').send_keys('1+1')
其次,您应该使用WebdriverWait
以确保元素已加载并且可交互。
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
xpath = '/html/body/div/div/div/div/div/div[1]/section/form/div/div/input'
element = wait.until(EC.element_to_be_clickable(driver.find_element(By.XPATH, xpath)))
element.send_keys('1+1')
推荐阅读
- macos - 我的系统如何知道在已删除的文件夹中查找二进制文件?
- java - 无法从 firebase 检索 login.java 的数据。gradles 中没有错误,但登录功能不起作用
- javascript - 在 JavaScript 中为所有类型的变量定义一个原型
- java - ă ș ț 使用 PdfWriter 从 html 生成的 pdf 中缺少字符
- android - 即使显示已发送短信,我也没有收到任何短信
- activeadmin - ActiveAdmin 创建/更新控制器
- arduino - 如何在arduino中识别接收到的XBee数据包的源节点?
- java - 如何防止 WebSphere 自动创建 EntitMangerFactory?
- php - 有没有办法使用 exec() 命令显示实时输出?
- wso2 - WSO2 Micro Integrator Validate Mediator 因操作不受支持而失败