错误:GDK_BACKEND与可用的显示不匹配;使用Crontab运行Selenium [英] Error: GDK_BACKEND does not match available displays; Running Selenium using Crontab
问题描述
我正在尝试使用cron运行硒.
I am trying to run selenium using cron.
import os
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from pyvirtualdisplay import Display
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['handleAlerts'] = True
firefox_capabilities['acceptSslCerts'] = True
firefox_capabilities['acceptInsecureCerts'] = True
binary = webdriver.firefox.firefox_binary.FirefoxBinary("/usr/bin/firefox")
now = datetime.datetime.now()
print "* Time of RUN : ",now
display = Display(visible=0, size=(1024, 768))
# display.start()
browser = webdriver.Firefox(firefox_binary=binary,executable_path=os.getcwd()+'/geckodriver')
browser.get('http://www.google.com')
print browser.title
browser.quit()
# display.stop()
以下是geckodriver.log
中显示的错误
Following is the error displayed in geckodriver.log
1525859641163 geckodriver INFO geckodriver 0.20.1
1525859641167 geckodriver INFO Listening on 127.0.0.1:34837
1525859642167 mozrunner::runner INFO Running command: "/usr/bin/firefox" "-marionette" "-profile" "/tmp/rust_mozprofile.1xUlJhxoO4Ar"
Error: GDK_BACKEND does not match available displays
我尝试过的事情:
- 尝试了无显示(即虚拟显示)即无头硒的运行中硒
- 在调用webdriver.firefox()时尝试二进制参数
- 在webdriver.firefox()中尝试了geckodriver excutable(linux)
- 在cron cmd中使用了
export Dispaly=:0;
- tried running selenium without display (pyvirtualdisplay) i.e headless selenium
- tried binary parameter while invoking webdriver.firefox()
- tried geckodriver excutable(linux) in webdriver.firefox()
- used
export Dispaly=:0;
in the cron cmd
推荐答案
-
更新最新的geckodriver
update latest geckodriver
下载最新的firefox tarball并按照此步骤进行操作
download latest firefox tarball and follow this step
https://tecadmin.net/install-firefox-on-linux/
3.安装正式的Firefox Beta PPA
3.Install the official Firefox Beta PPA
$ sudo apt-add-repository ppa:mozillateam/firefox-next
4.运行apt-get update
4.Run apt-get update
$sudo apt-get update
-
安装firefox和xvfb(X Windows虚拟帧缓冲区)软件包
Install firefox and xvfb (the X windows virtual framebuffer) packages
$ sudo apt-get install firefox xvfb
在后台运行Xvfb并指定显示编号(在我的示例中为10)
Run Xvfb in the background and specify a display number (10 in my example)
$ Xvfb :10 -ac &
将DISPLAY变量设置为您选择的数字
Set the DISPLAY variable to the number you chose
$ export DISPLAY=:10
firefox如果您以tar下载,则有助于避免下载依赖文件
firefox if you download as tar it helps to avoid download dependency files
wget http://ftp.mozilla.org/pub/firefox/releases/61.0/linux-x86_64/en-US/firefox-61.0.tar.bz2
请注意,您的Firefox代码应具有无头选项,以避免显示窗口.
Note your firefox code should have headless option inorder to avoid displaying window.
这篇关于错误:GDK_BACKEND与可用的显示不匹配;使用Crontab运行Selenium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!