open-wa / wa-automate-python

💬 🤖 The most advanced Python whatsapp library for chatbots with advanced features. Be sure to ⭐ this repository for updates!
Other
330 stars 71 forks source link

Runs great locally but get stuck when running on Heroku #82

Closed fire17 closed 2 years ago

fire17 commented 2 years ago

Hi There! maybe you can help ..

I suddenly get stuck while waiting for login... Not sure what changed but I get timeout error rasing the timeout doesnt help, still stuck forever then crashes the same

works fine on my machine, all the profile files and other paths are set correctly on server env

if you have any suggestions i would love to try stuff out... for now ill try rolling untill i find something that worked in the past and merge it somefuckinghow (lol)

THX :)

 RRRRRRRRRRRRRRRRRRRRRRRRRRREADY [False]
2021-08-10T22:54:40.045441+00:00 app[web.1]: ::: waiting for login :::
2021-08-10T22:54:48.000000+00:00 app[api]: Build succeeded
2021-08-10T22:56:10.352419+00:00 app[web.1]: Exception in thread Thread-2:
2021-08-10T22:56:10.352430+00:00 app[web.1]: Traceback (most recent call last):
2021-08-10T22:56:10.352453+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/threading.py", line 973, in _bootstrap_inner
2021-08-10T22:56:10.353260+00:00 app[web.1]: self.run()
2021-08-10T22:56:10.356408+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/threading.py", line 910, in run
2021-08-10T22:56:10.357027+00:00 app[web.1]: self._target(*self._args, **self._kwargs)
2021-08-10T22:56:10.357058+00:00 app[web.1]: File "/app/app.py", line 316, in initAsync
2021-08-10T22:56:10.357437+00:00 app[web.1]: driver.wait_for_login()
2021-08-10T22:56:10.357466+00:00 app[web.1]: File "/app/openwapy/src/__init__.py", line 640, in wait_for_login
2021-08-10T22:56:10.359588+00:00 app[web.1]: WebDriverWait(self.driver, timeout).until(EC.visibility_of_element_located((By.CSS_SELECTOR, self._SELECTORS['mainPage'] + ',' + self._SELECTORS['qrCode'])))
2021-08-10T22:56:10.359620+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/selenium/webdriver/support/wait.py", line 80, in until
2021-08-10T22:56:10.359866+00:00 app[web.1]: raise TimeoutException(message, screen, stacktrace)
2021-08-10T22:56:10.360016+00:00 app[web.1]: selenium.common.exceptions.TimeoutException: Message:
2021-08-10T22:56:10.360039+00:00 app[web.1]: 
mrodal commented 2 years ago

Hi, sorry I have no experience on heroku, but did you google something like selenium in heroku? I found this, may be of help https://stackoverflow.com/questions/42860703/how-to-use-selenium-webdriver-on-heroku

darkash commented 2 years ago

never used heroku, but it seems that the front page does not load. Perhaps try screenshot the page when the exception occured just to see how it's rendered?

not sure if this script works

from selenium.common.exceptions import TimeoutException
from openwa import WhatsAPIDriver

driver = WhatsAPIDriver(client='firefox') # replace this with your setup
try:
    print("Waiting for QR")
    driver.wait_for_login()
    print("Bot started")
except TimeoutException as e:
    driver.driver.screenshot('screenshot.png') # save screenshot file to storage
    raise e
mrodal commented 2 years ago

Regarding https://github.com/open-wa/wa-automate-python/issues/83#issuecomment-899063248 which browser are you using, could you provide your heroku setup? could you execute driver.execute_script("return navigator.userAgent") to see the user agent?

fire17 commented 2 years ago

SEEMINGLY same as before but I dont think that my issue now was the same as it was before, Pretty sure what solved it the first time was adding

      chrome_options.add_argument("--disable-dev-shm-usage")
      chrome_options.add_argument("--no-sandbox")
      chrome_options.add_argument('--disable-gpu')

I have followed @darkash 's advice and save a screenshot, which said (on a whatsappweb style page) (here's how)

# (on server)
      print("!!!!!!!!!!!!!! userAgent", driver.driver.execute_script("return navigator.userAgent"))
      print(''' ::: waiting for login ::: ''')
      try:
          driver.wait_for_login()
      except:
          driver.screenshot('loginFailed.png') # save screenshot file to storage
          print(" ::: ERROR - waiting for login - screenshot saved to loginFailed.png ::: ")#,"\n",e,e.args,"\n")
          traceback.print_exc()

# (on local)
heroku ps:copy loginFailed.png -a app-example-prod --output=~/loginFailed_from_heroku.png
Browser Not Supported
we recomend using one of the following : Chrome, Firefox, Edge, Opera, Safari

I've checked the chromedriver installed on heroku through the buildpacks, it said 92.0.4515.107 so now i'm trying it out with chrome_options.add_argument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36") hope it works

(just read what you wrote, will try to not set user-agent and print driver.execute_script("return navigator.userAgent") )

fire17 commented 2 years ago

got !!!!!!!!!!!!!! userAgent Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/92.0.4515.131 Safari/537.36

im guessing the HeadlessChrome is the problem, again tried to set the user agent again (didnt work) now got !!!!!!!!!!!!!! userAgent Mozilla/5.0 so i will try chrome_options.add_argument("--user-agent=Chrome/92.0.4515.107")

fire17 commented 2 years ago

Now im getting !!!!!!!!!!!!!! userAgent Chrome/92.0.4515.131 and DeepinScreenshot_select-area_20210816095043

Is it possible that the buildpacks for chrome and chromedriver are TOO NEW ? any idea on how to downgrade chrome and chromedriver ? id like to still use some buildpacks to hopefully not fuckup anything else in my setup

Also tried chrome_options.add_argument("--user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36") and chrome_options.add_argument("--user-agent=Chrome/89.0.4389.90") did not work any suggestions ??? thx

darkash commented 2 years ago

You sure the chrome in heroku is updated? Not the chromedriver but rather the browser executable

I don't think spoofing user-agent will work

fire17 commented 2 years ago

I get both chrome and chromedriver there from buildpacks so both should be latest and Ideally the same . . . :/

fire17 commented 2 years ago

Update: Closing this issue for now, as I just don't use Heroku anymore. For anyone wondering, it is possible, but not the most stable (if you're using buildpacks, you just cant know when one of them will update)

Maybe I or someone will take a look at this in the future and give better answers, probably to install a specific version of chrome that's compatible with a specific version of chromedriver