+3 votes
1.8k views
in Programming by (1.6k points)

I have created a selenium script in python and set a cron in server to run that script like this :

30 9 * * * cd '/var/www/html/py/' ; /usr/local/bin/python2.7 test.py > /tmp/Check_bot5.log 2>&1

And getting this error on log file :

Traceback (most recent call last):
  File "Top10FeatureCheck.py", line 48, in <module>
    driver = webdriver.Firefox(firefoxProfile)
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 77, in __init__
    self.binary, timeout),
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/firefox/extension_connection.py", line 49, in __init__
    self.binary.launch_browser(self.profile)
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 68, in launch_browser
    self._wait_until_connectable()
  File "/usr/local/lib/python2.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 98, in _wait_until_connectable
    raise WebDriverException("The browser appears to have exited "
selenium.common.exceptions.WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.

Please help me to resolve this problem or give me some way to execute python script by crontab in server.

 

1 Answer

0 votes
by Expert (5.9k points)
edited by

I was facing the same problem , you just need to follow these steps to resolve this issue :

 

Step 1.  Run these command to install xvfb and pyvirtualdisplay.

    $ sudo apt-get install xvfb python-pip
    $ sudo pip install pyvirtualdisplay

For centOs :

    $ yum install xvfb python-pip
    $ pip install pyvirtualdisplay

What are doing here : we are providing display PyVirtualDisplay so that firefox driver can get a environment to run.

 

Step 2. Now you have to define display in you code like this :

 

display = Display(visible=0, size=(800, 600))
display.start()

And at the end of the code you have to close the display like:

display.stop()

 

So let's have a example of a selenium script including these display command :

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(800, 600))
display.start()

browser = webdriver.Firefox()
browser.get('http://edu.yoursfriends.com')
print browser.title
browser.quit()

display.stop()

After following these 2 steps this problem was resolved for me , I hope it will help you, Enjoy!

Not a Member yet?

Ask to Folks Login

My Account

Your feedback is highly appreciated