Forums

a problem about selenium.webdriver

i want to save screenshots of a webpage, and i find the code

display = Display(visible=0, size=(800, 600))
try:
    display.start()
    browser = webdriver.Firefox()
    try:
        browser.get(some_url)
        browser.save_screenshot(filename)
    finally:
        browser.quit()
finally:
    display.stop()

runs well like said in https://www.pythonanywhere.com/forums/topic/1192/#id_post_8360

but things went wrong when i just added @app.route(some_route) in front of the function with an exception

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.

could anyone tell me what happened? the python version is 2.7

Just to make sure I understand -- you're saying that the code works when you run it from a console, but not when you run it in a web app's code. Is that right?

that it is!

Hmm. Normally with an error like that I'd expect that you had too many processes running on the web server (so your Firefox instance wouldn't be able to start), but I've checked and you don't have anything beyond the processes necessary to run your website at the moment. Are you trying to start up multiple Firefox instances, perhaps?

I guess i am not...

it's the only place where a firefox instance could be started up in my code, and in case the web app does that without letting me know, i put one line open("result", "a").write("webdriver.Firefox\n") before the line of browser = webdriver.Firefox() where the exception was caught and get only one line per https get request in the file "result" which i think means there's only one instance started up.

Were you in the tarpit when you got this error. If you're in the tarpit, the firefox startup will take longer and that may mean that the connection times out.

Hmm... I think I was not. In fact, I'm not familiar with tarpit and have never used it...

https://www.pythonanywhere.com/tarpit/

Thanks a lot! that's really helpful!

however here comes another problem...

i found that browser.get(url) in a flask app gets nothing as the title is a empty string and the function save_screenshot gives a blank image while the same codes running in a console works.

plus, browser.get(url) encounters "Connection refused" or "BadStatusLine" sometimes while sometimes it gets no error with the same url.

I mean, the browser either raises an error or just gets nothing while it always works in a console.

can it be still because I am in the tarpit? Or do you have some other ideas? thanks a lot.

Our general advice is not to use selenium directly from a web app, it's too slow. more info on our selenium help page,

Thanks a lot!!!