2

I have python script which run absolutly fine when we execute it manually. However when we schedule it to run from cronjob it fails with

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. 

Below is my script

#!/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()

I even set geckodriver in PATH but it is not working. Is there any way to fix it?

Note: I am running this script and cronjob inside docker container. I have placed geckodriver in /usr/local/bin/

Jaydeep Chaudhari
  • 420
  • 1
  • 9
  • 21
  • just put your python code and gecko driver at same location and just write "geckodriver". No need to give full path. Try this. – Hiten Dec 29 '17 at 07:14
  • Possible duplicate of [selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH error with chrome](https://stackoverflow.com/questions/46085270/chrome-headless-browswer-selenium-python/46089751#46089751) – undetected Selenium Dec 29 '17 at 07:19
  • @Hiten, where do you suggest exactly to write geckodriver – Jaydeep Chaudhari Dec 29 '17 at 07:27
  • 1
    put your gecko driver where the project is locate and then not require to give full path just gave the name "geckodriver". – Hiten Dec 29 '17 at 07:28
  • @Hiten, Oh, I get it, I tried that too, didn't worked. My script is working if run script manually it only doesn't work when run from cronjob. – Jaydeep Chaudhari Dec 29 '17 at 07:31
  • Okay. So where you put your gecko driver on server ? may be there is a issue of path. It didn't found your driver on server that's why you are getting error. – Hiten Dec 29 '17 at 07:40
  • At present my I have put geckodriver at `/usr/loca/bin` also set path for it – Jaydeep Chaudhari Dec 29 '17 at 08:35
  • Possible duplicate of [How to get CRON to call in the correct PATHs](https://stackoverflow.com/questions/2388087/how-to-get-cron-to-call-in-the-correct-paths) – SiKing May 01 '19 at 17:07

0 Answers0