1

i am totally new on heroku and i was trying to deploy a simple python script which prints the source code of google. this is my script:

import os
from selenium import webdriver
op = webdriver.ChromeOptions()
op.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
op.add_argument("--headless")
op.add_argument("--no-sandbox")
op.add_argument("--disable-dev-sh-usage")
driver = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"),chrome_options=op)
driver.get('https://google.com')
print(driver.page_source)

and these are my configvars on heroku

CHROMEDRIVER_PATH = /app/.chromedriver/bin/chromedriver

GOOGLE_CHROME_BIN = /app/.apt/usr/bin/google-chrome

and the error that i got on heroku is this

2020-08-10T20:18:21.304180+00:00 heroku[web.1]: State changed from crashed to starting
2020-08-10T20:18:48.000000+00:00 app[api]: Build succeeded
2020-08-10T20:18:49.871354+00:00 heroku[web.1]: Starting process with command `python google.py`
2020-08-10T20:18:53.312155+00:00 heroku[web.1]: Process exited with status 1
2020-08-10T20:18:53.367125+00:00 heroku[web.1]: State changed from starting to crashed
2020-08-10T20:18:53.371240+00:00 heroku[web.1]: State changed from crashed to starting
2020-08-10T20:18:53.206728+00:00 app[web.1]: Traceback (most recent call last):
2020-08-10T20:18:53.206831+00:00 app[web.1]:   File "google.py", line 8, in <module>
2020-08-10T20:18:53.207147+00:00 app[web.1]:     driver = webdriver.Chrome(os.environ.get("CHROMEDRIVER_PATH"),op)
2020-08-10T20:18:53.208271+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
2020-08-10T20:18:53.208582+00:00 app[web.1]:     self.service.start()
2020-08-10T20:18:53.208623+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 71, in start
2020-08-10T20:18:53.208923+00:00 app[web.1]:     cmd.extend(self.command_line_args())
2020-08-10T20:18:53.208958+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/selenium/webdriver/chrome/service.py", line 45, in command_line_args
2020-08-10T20:18:53.209208+00:00 app[web.1]:     return ["--port=%d" % self.port] + self.service_args
2020-08-10T20:18:53.209262+00:00 app[web.1]: TypeError: %d format: a number is required, not Options
2020-08-10T20:19:06.776347+00:00 heroku[web.1]: Starting process with command `python google.py`
2020-08-10T20:19:10.012273+00:00 heroku[web.1]: Process exited with status 1
2020-08-10T20:19:10.081926+00:00 heroku[web.1]: State changed from starting to crashed
2020-08-10T20:19:09.881424+00:00 app[web.1]: Traceback (most recent call last):
2020-08-10T20:19:09.881458+00:00 app[web.1]:   File "google.py", line 8, in <module>
2020-08-10T20:19:09.881678+00:00 app[web.1]:     driver = webdriver.Chrome(os.environ.get("CHROMEDRIVER_PATH"),op)
2020-08-10T20:19:09.881717+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
2020-08-10T20:19:09.881941+00:00 app[web.1]:     self.service.start()
2020-08-10T20:19:09.881974+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/selenium/webdriver/common/service.py", line 71, in start
2020-08-10T20:19:09.882191+00:00 app[web.1]:     cmd.extend(self.command_line_args())
2020-08-10T20:19:09.882227+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/selenium/webdriver/chrome/service.py", line 45, in command_line_args
2020-08-10T20:19:09.882434+00:00 app[web.1]:     return ["--port=%d" % self.port] + self.service_args
2020-08-10T20:19:09.882481+00:00 app[web.1]: TypeError: %d format: a number is required, not Options

2 Answers2

0

TL;DR You need to redeploy the correct code (with chrome_options=op) to Heroku.

Explanation

Look closely at the traceback. It mentions the line:

driver = webdriver.Chrome(os.environ.get("CHROMEDRIVER_PATH"),
                          op)

which is not the same as the line in the code you think you execute:

driver = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"),
                          chrome_options=op)

The second positional argument for webdriver.Chrome.__init__ is indeed expected to be port, which totally explains the error

return ["--port=%d" % self.port] + self.service_args
TypeError: %d format: a number is required, not Options

which complains about self.port being Options object instead of an int (which %d expects).

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
0

chrome_options is deprecated now and you have to use options instead and your effective code block will be:

import os
from selenium import webdriver

op = webdriver.ChromeOptions()
op.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
op.add_argument("--headless")
op.add_argument("--no-sandbox")
op.add_argument("--disable-dev-sh-usage")
driver = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"), options=op)

References

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352