如何在Ubuntu上正确使用selenium与geckodriver和firefox与python?

his*_*der 5 python ubuntu firefox selenium-webdriver geckodriver

我正在尝试在我的 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")

Run Code Online (Sandbox Code Playgroud)

尽管如此,我还是收到以下错误:

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.
Run Code Online (Sandbox Code Playgroud)

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

有谁知道我该如何解决这个问题?

Lit*_*jan 6

第一步:安装Selenium

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

$pip install selenium
Run Code Online (Sandbox Code Playgroud)

第二步:下载Geckodriver

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

从以下页面下载 Gecko 驱动程序:

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

第三步:安装Gecko驱动

适用于 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 驱动程序:解压 zip 文件并将 geckodiver.exe 可执行文件移动到 Path 变量中已有的任何位置(例如,您可以将其移动到 Python 路径位置)

除非将“geckodriver.exe”的路径添加到 Path 变量中

为 Ubuntu 设置 Gecko 驱动程序:打开终端

Ctrl+Alt+T
Run Code Online (Sandbox Code Playgroud)

将目录移动到下载 tar 文件的位置通常它会在“下载”中。所以输入 $ cd 下载

解压 tar 文件,例如:

$sudo tar -xvf filename.tar.gz
Run Code Online (Sandbox Code Playgroud)

就我而言,它是:

$sudo tar -xvf geckodriver-v0.26.0-linux64.tar.gz
Run Code Online (Sandbox Code Playgroud)

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

将目录移动到“/usr/local/bin/”

$cd /usr/local/bin/
Run Code Online (Sandbox Code Playgroud)

现在为“geckodriver”可执行文件授予可执行权限

$sudo chmod +x geckodriver
Run Code Online (Sandbox Code Playgroud)

现在在终端中输入“geckodriver”

geckodriver
Run Code Online (Sandbox Code Playgroud)

如果 Gecko 驱动程序仍然无法工作,请添加其路径

$export PATH=$PATH:/usr/local/bin/geckodriver
Run Code Online (Sandbox Code Playgroud)

现在它已经准备好与 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)
Run Code Online (Sandbox Code Playgroud)


小智 0

如果您想将 Firefox 与 Selenium 一起使用,您需要导入 Firefox 配置文件。您可以通过以下步骤使用您自己的个人资料:

  1. 找到 Firefox 配置文件目录
  2. 当您启动 .firefox 时,您必须指定 Firefox 配置文件目录的绝对路径webdriver

    from selenium import webdriver
    profile = webdriver.FirefoxProfile(*path to your profile*)
    driver = webdriver.Firefox(firefox_profile=profile)
    
    Run Code Online (Sandbox Code Playgroud)