【问题标题】:Issues while deploying selenium script on heroku在 heroku 上部署 selenium 脚本时出现问题
【发布时间】:2020-08-10 20:20:48
【问题描述】:

我对 heroku 完全陌生,我试图部署一个简单的 python 脚本来打印谷歌的源代码。 这是我的脚本:

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)

这些是我在 heroku 上的配置变量

CHROMEDRIVER_PATH = /app/.chromedriver/bin/chromedriver

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

我在heroku上遇到的错误是这个

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

【问题讨论】:

    标签: python selenium heroku


    【解决方案1】:

    chrome_options 现在已弃用,您必须改用options,您的有效代码块将是:

    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)
    

    参考文献

    您可以在以下位置找到一些相关讨论:

    【讨论】:

      【解决方案2】:

      TL;DR您需要将正确的代码(使用chrome_options=op)重新部署到 Heroku。

      解释

      仔细查看回溯。它提到了这一行:

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

      这与您认为您执行的代码中的行不同:

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

      webdriver.Chrome.__init__ 的第二个位置参数确实应该是port,这完全解释了错误

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

      它抱怨 self.portOptions 对象而不是 int%d 期望的)。

      【讨论】:

      • 感谢您的帮助。
      猜你喜欢
      • 2021-12-31
      • 1970-01-01
      • 2016-05-21
      • 2022-09-30
      • 2013-09-30
      • 2011-05-15
      • 2021-04-09
      • 2021-05-09
      • 2018-04-25
      相关资源
      最近更新 更多