首页 > 解决方案 > 使用 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”网站,我从那里获得了大部分代码。

标签: pythonpython-3.xseleniumselenium-webdriver

解决方案


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

确保将Webdriver-manager库添加到项目中。下面是pycharm的截图 在此处输入图像描述


推荐阅读