python - 尝试使用 selenium 运行脚本时出现错误
问题描述
每当我尝试运行此代码时
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Chrome()
browser.get("https://kahoot.it/")
time.sleep(10)
code = browser.find_element_by_id("inputSession")
code.send_keys("273976")
login_attempt = browser.find_element_by_xpath("//*[@type='submit']")
login_attempt.submit()
我得到错误
Traceback (most recent call last):
File "C:\Users\jiney\AppData\Local\Programs\Python\Python37-
32\lib\site-packages\selenium\webdriver\common\service.py", line 76, in
start
stdin=PIPE)
File "C:\Users\jiney\AppData\Local\Programs\Python\Python37-
32\lib\subprocess.py", line 756, in __init__
restore_signals, start_new_session)
File "C:\Users\jiney\AppData\Local\Programs\Python\Python37-
32\lib\subprocess.py", line 1155, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/jiney/AppData/Local/Programs/Python/Python37-
32/kahootthing.py", line 4, in <module>
browser = webdriver.Chrome()
File "C:\Users\jiney\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
self.service.start()
File "C:\Users\jiney\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\selenium\webdriver\common\service.py", line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver'
executable needs to be in PATH. Please see
https://sites.google.com/a/chromium.org/chromedriver/home
如果有任何帮助,将不胜感激,因为它非常令人困惑,我认为这与模块有关,但我已尝试卸载并重新安装两次。
谢谢!
解决方案
如错误所示:
可执行文件需要在 PATH 中
您应该添加路径:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Chrome(executable_path=r'C:\the\path\to\chromedriver.exe')
browser.get("https://kahoot.it/")
time.sleep(10)
code = browser.find_element_by_id("inputSession")
code.send_keys("273976")
login_attempt = browser.find_element_by_xpath("//*[@type='submit']")
login_attempt.submit()
希望这对你有帮助!
推荐阅读
- postgresql - 如何在路径中使用通配符为 postgres 搜索 jsonb 值?
- apache - 生成 CSR 时无法在配置中找到“distinguished_name”
- java - 如何批量更新弹性搜索多个id?
- c++ - 创建一个重复字符 n 次的编译时间字符串
- laravel - 如果用户成功登录并在 laravel-8 中返回该数据,如何从迁移表中获取特定列数据(角色列)?
- c# - 正则表达式无法检测到,(逗号)
- pandas - 查找哪些列包含/匹配另一列的智能/pythonic方法
- c# - 扫描 DHL 标签时出现问题 Identcode C# XAMARIN EMDK
- macos - macOS11(虚拟机或docker容器)启动时如何设置自动登录
- php - 将下划线 URL 的 PHP 查询转换为破折号/连字符 URL