我正在尝试使用cron运行selenium。
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
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
export Dispaly=:0;
答案 0 :(得分:1)
更新最新的geckodriver
下载最新的firefox tarball并按照此步骤进行操作
https://tecadmin.net/install-firefox-on-linux/
3。安装正式的Firefox Beta PPA
$ sudo apt-add-repository ppa:mozillateam/firefox-next
4。运行apt-get update
$sudo apt-get update
安装firefox和xvfb(X Windows虚拟帧缓冲区)软件包
$ sudo apt-get install firefox xvfb
在后台运行Xvfb并指定显示编号(在我的示例中为10)
$ Xvfb :10 -ac &
将DISPLAY变量设置为您选择的数字
$ export DISPLAY=:10
firefox如果您以tar下载,则有助于避免下载依赖文件
wget http://ftp.mozilla.org/pub/firefox/releases/61.0/linux-x86_64/en-US/firefox-61.0.tar.bz2
请注意,您的Firefox代码应具有无头选项,以避免显示窗口。