我正在尝试在我的 Ubuntu 机器上使用带有 firefox 和 selenium 的 geckodriver。这是我到目前为止的代码:
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
有人知道我该如何解决这个问题吗?
最佳答案
第 1 步:安装 Selenium
输入终端(在 Ubuntu 中)或命令提示符(在 Windows 中)
$pip install selenium
第二步:下载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
将目录移动到下载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)
关于python - 如何在 Ubuntu 上正确使用带有 geckodriver 的 selenium 和带有 python 的 firefox?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57870311/