Forums

Selenium with pythonanywhere

I have a simple python code which will mock events in Firefox browser. Currently I am testing the pythonanywhere environment, I have made a virtual environment and have install all the required Python libraries. I have uploaded the sample code when I run the same code on the console, it runs fine but when I put it in a webapp it is not working. It's giving The browser appears to have exited " 2017-02-23 07:05:41,153 :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. error in the logs. I think its because of the firefox version or anything. Please help!

@app.route('/webapp/<name>/<searchVal>')
def defi(name,searchVal):
if "2" in name:
    display = Display(visible=0, size=(800, 600))
         time.sleep(5)
         display.start()
         try:
              time.sleep(5)
              browser = webdriver.Firefox()
              browser.get('http://www.google.com')
         finally:
              m = {'Site': 'Test site', 'searchVal': browser.title}
              browser.quit()
              display.stop()
         return json.dumps(m)

 if __name__ == "__main__":
     app.run()

do you know which line is throwing that error?

sounds like browser.quit() is failing/erroring.

this means that display.stop isn't being called because an exception is thrown before that- which means that you are opening a lot of displays and not closing them.

I don't think problem is because of opeing too many browsers. The error is coming because browser = webdriver.Firefox() is not creating firefox session i.e. variable browser is not getting any value assigned to it.

If you browser never gets populated and browser.quit raises an error, then the display.stop will never get called, and that could cause xvfb virtual displays to pile up. We may need to improve our suggested code here..

I've adjusted the selenium instructions to suggest the use of a with context manager, which will automatically do the cleanup (I've also manually cleaned up your xvfb processes on the web server for you).

I tired with your code still getting the same error, Also even after the clean up I am still getting the same error i.e.

The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.

When I execute xvfb-run -a python3.5 /home/myusername/myfolder/myscript.py command I get an error that address already in use.

You can view the webApp in my profile to see the code

I just took a look and there was a Firefox and an Xvfb running on the server where your web app runs -- they were started at 03:37 UTC (that is, about 20 minutes before you made your forum post).

Perhaps that was there from before you introduced the cleanup code? I've killed the processes, so if you try running the code again now, perhaps it will work?

Tried with both Display() and Xvbf() still facing the same issue. Display() giving the same old error "... browser exited before we could connect..." and the later one is not allowing the page to load. I have installed Selenium 2.53 , so do you think it is coming because of version mismatch Selenium(2.53) and Firefox(17.0 ancient ver.) ??

Ah! Yes, that could be it. You have to use the old version of Firefox on PythonAnywhere because newer versions don't play nicely with our virtualisation system. And the version of Selenium we have installed is chosen to be the most reliable one to use with that version of Firefox.

Did you ever figure this error out? Having the same exact error. Not exactly sure how to mess with the firefox installation.