错误:GDK_BACKEND与可用的显示不匹配;使用Crontab运行Selenium [英] Error: GDK_BACKEND does not match available displays; Running Selenium using Crontab

查看:1061
本文介绍了错误:GDK_BACKEND与可用的显示不匹配;使用Crontab运行Selenium的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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

我尝试过的事情:

  1. 尝试了无显示(即虚拟显示)即无头硒的运行中硒
  2. 在调用webdriver.firefox()时尝试二进制参数
  3. 在webdriver.firefox()中尝试了geckodriver excutable(linux)
  4. 在cron cmd中使用了export Dispaly=:0;
  1. tried running selenium without display (pyvirtualdisplay) i.e headless selenium
  2. tried binary parameter while invoking webdriver.firefox()
  3. tried geckodriver excutable(linux) in webdriver.firefox()
  4. used export Dispaly=:0; in the cron cmd

推荐答案

  1. 更新最新的geckodriver

  1. 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

  1. 安装firefox和xvfb(X Windows虚拟帧缓冲区)软件包

  1. 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
    

  • 测试代码 #!/usr/bin/env python 从硒导入webdriver 浏览器= webdriver.Firefox() browser.get(' http://iAisystem.com/') browser.quit()
  • test the code #!/usr/bin/env python from selenium import webdriver browser = webdriver.Firefox() browser.get('http://iAisystem.com/') browser.quit()
  • 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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆