脚本从cronjob运行时出现Geckodriver错误

时间:2017-12-29 04:36:58

标签: python python-2.7 selenium selenium-webdriver

我有python脚本,当我们手动执行它时,它运行绝对精确。但是,当我们安排它从cronjob运行时,它失败了

driver = webdriver.Firefox(firefox_profile=profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 148, in __init__
self.service.start()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/common/service.py", line 81, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 

以下是我的剧本

#!/usr/bin/env python
import os
from browsermobproxy import Server
server = Server("/browsermob-proxy-2.1.4/bin/browsermob-proxy")
server.start()
proxy = server.create_proxy()

from pyvirtualdisplay import Display
display = Display(visible=0, size=(1920, 1080))
display.start()

from selenium import webdriver
profile  = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)


proxy.new_har("support")
driver.get('https://mynet.myco.net/')
username = driver.find_element_by_name("user_txt")
password = driver.find_element_by_name("user_pass")
username.send_keys("support@myco.net")
password.send_keys("qaz@1234")
login_attempt = driver.find_element_by_xpath("//*[@type='submit']").click()
proxy.har # returns a HAR JSON blob
data = proxy.har

import json
with open('/json/mynet.json', 'w') as outfile:
    json.dump(data, outfile)

from pprint import pprint

number = 0
with open('/json/mynet.json') as data_file:
    data = json.load(data_file)


total = 0
for j in range(len(data["log"]["entries"])):
    total += (data["log"]["entries"][number]["time"])
    number  += 1       
#print(total)


server.stop()

f=open("/mynet.log", "a+")
f.write("https://mynet.myco.net/: %d\r\n" % total)
driver.quit()
display.stop()

我甚至在PATH中设置了geckodriver,但它无效。 有没有办法解决它?

注意:我在docker容器中运行此脚本和cronjob。 我已将geckodriver放在/ usr / local / bin /

0 个答案:

没有答案