python - 使用 Python 浏览互联网
问题描述
我正在尝试使用 Python 访问互联网。我写了一个小程序。但是,我遇到了很多对我来说没有意义的错误。
这是程序:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://google.com")
以下是错误:
"C:\Users\PycharmProjects\Python Tutorials\venv\Scripts\python.exe" "C:/Users/PycharmProjects/Python Tutorials/Facebook.py"
Traceback (most recent call last):
File "C:\Users\PycharmProjects\Python Tutorials\venv\lib\site-packages\selenium\webdriver\common\service.py", line 72, in start
self.process = subprocess.Popen(cmd, env=self.env,
File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 1307, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
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/PycharmProjects/Python Tutorials/Facebook.py", line 10, in <module>
driver = webdriver.Firefox()
File "C:\Users\PycharmProjects\Python Tutorials\venv\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 164, in __init__
self.service.start()
File "C:\Users\PycharmProjects\Python Tutorials\venv\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Process finished with exit code 1
现在,我使用了你的建议,它对我有用。我想我现在不仅可以打开 google.com 网站,还可以打开 Facebook 网站以及我的 ID 和密码。
当我无法安装 webdrivermanager 时的主要问题是,我发现有两个包,webdrivermanager 和 webdriver-manager。我一直在尝试安装 webdrivermanager 并且在尝试安装它时它一直给我一个错误,但是当我安装 webdriver-manager 时它允许我成功安装并且它现在对我有用。
请自行检查,这里:
我正在尝试打开 google.com 网站,然后它会自动关闭,然后它会询问我的登录 ID 和密码,然后它会打开另一个 Firefox 网页,在其中显示 Facebook 的主页。
在这里,我也把#号放在上一条命令旁边,尝试运行,运行成功。我想感谢“geeksforgeeks.com”网站,我从那里获得了大部分代码。
解决方案
Message: 'geckodriver' executable needs to be in PATH. this clearly mentioned that the geckodriver path is missing.
当您在 Stackoverflow 上搜索该消息时,您可以找到许多答案。
但是,我更喜欢使用 Weddriver-manger 库,它将下载驱动程序并采取必要的步骤以最小的努力启动浏览器。
试试下面的代码片段。
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
driver.get("https://www.google.com")
driver.quit()
推荐阅读
- html - 为什么某些字符会改变整体行高,如何解决?
- java - Java:如何在程序的方法内部使用用户输入?
- javascript - 使用 Luxon 格式化 ISO 时间
- r - 无法从 OpenSUSE Leap 15.2 安装 arm 和 lme4 软件包
- syntax - Ansible:如何使用另一个变量获取动态服务状态
- javascript - 在 JavaScript 中从数组中创建复选框
- css - 转换在 iOS Safari 上不起作用 - 尝试了所有不同的后缀
- xcode - 切换以在 swiftui 中获得通知
- django - 突然我们无法在 Wagtail 的特定页面中添加更多图像,出现 400 错误请求错误
- python - Pytorch 在第一个训练批次上使用更多内存