2

I'm having some problem when deploying an application to Heroku but the application works fine on my machine. The driver enters a page and closes but when it tries to reopen it crashes giving me the following message:

2019-07-18T02:45:46.825924+00:00 heroku[worker.1]: Starting process with command `python script.py`
2019-07-18T02:45:47.454789+00:00 heroku[worker.1]: State changed from starting to up
2019-07-18T02:46:00.826330+00:00 app[worker.1]: https://www.google.com/ Viewed!
2019-07-18T02:46:02.300095+00:00 app[worker.1]: Traceback (most recent call last):
2019-07-18T02:46:02.300192+00:00 app[worker.1]:   File "script.py", line 37, in <module>
2019-07-18T02:46:02.300596+00:00 app[worker.1]:     execution()
2019-07-18T02:46:02.300704+00:00 app[worker.1]:   File "script.py", line 25, in botting
2019-07-18T02:46:02.300950+00:00 app[worker.1]:     driver = webdriver.Chrome(executable_path='/app/.chromedriver/bin/chromedriver', chrome_options=options) #/app/chrome_file/chromedriver
2019-07-18T02:46:02.301008+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
2019-07-18T02:46:02.301309+00:00 app[worker.1]:     desired_capabilities=desired_capabilities)
2019-07-18T02:46:02.301367+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
2019-07-18T02:46:02.301692+00:00 app[worker.1]:     self.start_session(capabilities, browser_profile)
2019-07-18T02:46:02.301776+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
2019-07-18T02:46:02.302193+00:00 app[worker.1]:     response = self.execute(Command.NEW_SESSION, parameters)
2019-07-18T02:46:02.302235+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
2019-07-18T02:46:02.302727+00:00 app[worker.1]:     self.error_handler.check_response(response)
2019-07-18T02:46:02.302769+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
2019-07-18T02:46:02.303239+00:00 app[worker.1]:     raise exception_class(message, screen, stacktrace)
2019-07-18T02:46:02.303329+00:00 app[worker.1]: selenium.common.exceptions.SessionNotCreatedException: Message: session not created
2019-07-18T02:46:02.303332+00:00 app[worker.1]: from tab crashed
2019-07-18T02:46:02.303335+00:00 app[worker.1]:   (Session info: headless chrome=75.0.3770.142)
2019-07-18T02:46:02.303395+00:00 app[worker.1]: 

And here's the script in question:

from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException, ElementNotVisibleException, WebDriverException
from selenium import webdriver
from time import sleep
import json

with open("proxies.txt", 'r') as filein:
    proxylist = filein.read()

with open("links.json", 'r') as linksin:
    linklist = json.load(linksin)

def execution():
    for proxies in proxylist:
        options = webdriver.ChromeOptions()
        options.binary_location = '/app/.apt/usr/bin/google-chrome'
        options.add_argument("--example-flag")
        options.add_argument('--no-sandbox')
        options.add_argument('--disable-gpu')
        options.add_argument('--headless')
        #options.add_argument('--disable-dev-shm-usage')
        options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
        options.add_argument('--proxy-server=%s' % proxies)
        driver = webdriver.Chrome(executable_path='/app/.chromedriver/bin/chromedriver', chrome_options=options) #/app/chrome_file/chromedriver
        for links in linklist:
            try:
                driver.get(links)
                sleep(10)
                print('{} Viewed!'.format(links))
                driver.close()
            except(WebDriverException, ConnectionError, ConnectionAbortedError, ConnectionRefusedError, ConnectionResetError):
                pass

if __name__ == "__main__":
    execution()

But the thing is, I tried almost every "solution" I found and the result was always the same, any help is appreciated.

Tried some stuff from this answer and other but to no success, here's the stuff I tried that didn't worked:

The chrome_options.add_argument('--no-sandbox') is already in the script and it results in the same error.

The chrome_options.add_argument('--disable-dev-shm-usage') is already on the script but it freezes the Heroku and it stays frozen as app[worker.1]: https://www.google.com/ Viewed! and don't go anywhere (waited for it to respond for 30m)

Regarding the:

 sudo mount -t tmpfs -o rw,nosuid,nodev,noexec,relatime,size=512M tmpfs /dev/shm, 

I can't mess with this stuff in the Heroku env so it's a no-no. And from tab crashed, the Chrome is updated (75.0.3770.142) and chromedriver is updated (75.0.3770.140) too. Regarding other fixes I've tried the options.add_argument("--example-flag"), and had the same result.

ZF007
  • 3,708
  • 8
  • 29
  • 48
arlon
  • 21
  • 3
  • you are closing the browser in your linklist loop... the browser opens when you call "driver = webdriver.Chrome..." You close the browser and then there's nothing for the driver to work with. – pcalkins Jul 18 '19 at 18:24
  • I know but I need it to actually reopen so it can use another proxy to visit the site, the frist time it actually visits but after closing it simply crashes and besides, it works ok when running locally – arlon Jul 18 '19 at 18:27
  • loops are nested wrong then? You are opening a new driver for each proxy, and then each driver instance is going to all links. – pcalkins Jul 18 '19 at 18:28
  • The exception you are getting seems like it'd be coming from cleanup during the closing browser instance... it may be trying to launch a new driver instance during that cleanup and Chrome is crashing. (Chrome creates some temp folders during selenium runs for profile stuff) – pcalkins Jul 18 '19 at 18:44

0 Answers0