如何在Ubuntu上使用Python、Geckodriver和Firefox正确地使用Selenium?

4

我正在尝试在我的Ubuntu机器上使用geckodriver、firefox和selenium。这是我目前的代码:

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.keys import Keys
from selenium import webdriver


#path where browser is installed
binary = '/usr/bin/firefox'
options = webdriver.FirefoxOptions()
options.binary = binary
options.add_argument('start-maximized')
options.add_argument('--headless')


cap = DesiredCapabilities().FIREFOX
cap["marionette"] = False


path_to_driver = "/home/andrea/geckodriver"

# run firefox webdriver from executable path 
driver = webdriver.Firefox(firefox_options=options, capabilities=cap, executable_path = path_to_driver)
#driver = webdriver.Firefox(capabilities=cap, executable_path = path_to_driver)


driver.get("https://www.amboss.com/us/account/login")


尽管我遇到以下错误:
selenium.common.exceptions.WebDriverException: Message: Can't load the profile. 
Possible firefox version mismatch. You must use GeckoDriver instead for Firefox 48+. Profile Dir: /tmp/tmpuigrk9f7 If you specified a log_file in the FirefoxBinary constructor, check it for details.

我使用的Firefox版本是:Mozilla Firefox 68.0.2

有人知道如何解决这个问题吗?


这个下载的驱动程序是否位于此页面中的"/home/andrea/geckodriver"?它是可执行文件(chmod +x)吗? - ipaleka
@ipaleka 是的,它甚至可以打开Mozilla Firefox,但然后它会关闭并且控制台显示我上面写的错误消息。 - hispaniccoder
你尝试过使用这个全新的驱动程序吗?https://github.com/mozilla/geckodriver/releases? - ipaleka
4个回答

6

步骤1:安装Selenium

在终端(Ubuntu)或命令提示符(Windows)中输入以下命令:

$pip install selenium

步骤2:下载Geckodriver

为了使用Selenium,需要安装一个名为“Gecko Driver”的可执行文件。

从以下网页下载Gecko Driver:

https://github.com/mozilla/geckodriver/releases

步骤3:安装Gecko Driver

Windows最新版本:

https://github.com/mozilla/geckodriver/releases/download/v0.26.0/geckodriver-v0.26.0-win64.zip

Ubuntu最新版本:

https://github.com/mozilla/geckodriver/releases/download/v0.26.0/geckodriver-v0.26.0-linux64.tar.gz

设置Windows的Gecko Driver:

解压缩zip文件并将geckodiver.exe可执行文件移动到已经在Path变量中的任何位置(例如,您可以将其移动到Python路径位置)。

否则,将'geckodriver.exe'的路径添加到Path变量中。

设置Ubuntu的Gecko Driver:

打开终端。

Ctrl+Alt+T

将目录移动到tar文件下载的位置。通常情况下,它会在“下载”文件夹中。因此,请键入$ cd Downloads

解压缩tar文件 例如:

$sudo tar -xvf filename.tar.gz

在我的情况下,它是:

$sudo tar -xvf geckodriver-v0.26.0-linux64.tar.gz

将 geckodriver 可执行文件移动到 '/usr/local/bin' 位置。 $sudo mv geckodriver /usr/local/bin/

将目录移动到 '/usr/local/bin/'。

$cd /usr/local/bin/

现在为 'geckodriver' 可执行文件添加可执行权限。
$sudo chmod +x geckodriver

现在在终端中输入“geckodriver”。
geckodriver

如果Gecko Driver仍然无法正常工作,则需要添加其路径。
$export PATH=$PATH:/usr/local/bin/geckodriver

现在它已准备好与selenium一起使用。

示例代码

这里有一些示例代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import ui

driver = webdriver.Firefox()
driver.get('https://www.google.com/')
page_url=driver.find_elements_by_xpath("//a[@class='content']")
all_title = driver.find_elements_by_class_name("title")
title = [title.text for title in all_title]
print(title)

1
这个错误信息...
selenium.common.exceptions.WebDriverException: Message: Can't load the profile. 
Possible firefox version mismatch. You must use GeckoDriver instead for Firefox 48+. Profile Dir: /tmp/tmpuigrk9f7 If you specified a log_file in the FirefoxBinary constructor, check it for details.

这段文字意味着在启动/生成新的Firefox浏览器会话时,GeckoDriverFirefox版本不匹配。您主要的问题是以下二进制文件版本之间的不兼容性:
- 您正在使用Mozilla Firefox v68.0.2 - 您的Selenium Client版本我们无法确定 - 您的GeckoDriver版本我们也无法确定
然而,由于您正在使用Mozilla Firefox v68.0.2,因此使用GeckoDriver强制性的,并且在使用GeckoDriver时,您不能将capability marionette设置为False

您可以在如何在Firefox 53上使用Python Selenium 3运行Geckodriver / Firefox而不使用Marionette?中找到详细讨论。


解决方案
  • Selenium升级到当前版本 Version 3.141.59
  • GeckoDriver升级到当前 GeckoDriver v0.24.0 版本。
  • GeckoDriver位于指定位置。
  • GeckoDriver对非root用户具有可执行权限。
  • Firefox版本升级到Firefox v68.0.2
  • 通过IDE清理项目工作区并仅使用所需的依赖项重建项目。
  • 如果您的基本Web客户端版本太旧,则通过Revo Uninstaller卸载它并安装最新的GA和发布版本的Web客户端
  • 进行系统重新启动
  • 以非root用户身份执行Test
  • 始终在tearDown(){}方法中调用driver.quit()以优雅地关闭和销毁WebDriverWeb客户端实例。

结语

GeckoDriverSeleniumFirefox浏览器兼容性表

supported_platforms_geckodriver_24


0
如果你想要使用Selenium和Firefox,你需要导入Firefox配置文件。你可以通过以下步骤使用自己的配置文件:
  1. 找到Firefox配置文件目录
  2. 在启动webdriver时,必须指定Firefox配置文件目录的绝对路径。

    from selenium import webdriver
    profile = webdriver.FirefoxProfile(*您的配置文件路径*)
    driver = webdriver.Firefox(firefox_profile=profile)
    

找到了,但是这个方法不起作用!它还是给我同样的错误。 - hispaniccoder

0

步骤1:在终端中安装Selenium(在Ubuntu中输入)

$ pip install selenium

步骤2:下载Geckodriver

https://pypi.org/project/geckodriver-autoinstaller/

步骤3:在你的Python代码文件中

from selenium import webdriver                  # Import selenium into your program
from selenium.webdriver.common.keys import Keys # Import keys of selenium web driver
import geckodriver_autoinstaller                # import Geckodriver into your program
geckodriver_autoinstaller.install()             # Get the latest version every day on 1st excution of your program
driver = webdriver.Firefox()                    # initiate the firefox driver 
driver.get("Your Website URL")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接