我想用硒和铬测试多个移动用户代理。我正在使用python 3.6并尝试部署到heroku。我正在使用无头chrome = 68.0.3440.75 chromedriver 2.40.565383。基于http://chromedriver.chromium.org/mobile-emulation:
def create_chromedriver(ua=False):
options = webdriver.ChromeOptions()
CHROMEDRIVER_PATH = os.getenv('$HOME') or basedir+'/chromedriver.exe'
FLASK_CONFIG = os.getenv('FLASK_CONFIG')
if FLASK_CONFIG and FLASK_CONFIG == "production":
# CHROMEDRIVER_PATH = '/app/.chromedriver/bin/chromedriver'
CHROMEDRIVER_PATH = '/app/.chromedriver/bin/chromedriver'
GOOGLE_CHROME_SHIM = os.getenv('$GOOGLE_CHROME_SHIM') or 'no path found'
print(GOOGLE_CHROME_SHIM)
print(GOOGLE_CHROME_SHIM)
options.add_argument("--headless")
options.add_argument("--disable-gpu")
if ua:
print('ua block33')
mobile_emulation = {"deviceName": "iPad Mini"}
options.add_experimental_option("mobileEmulation", mobile_emulation)
return webdriver.Chrome(executable_path=CHROMEDRIVER_PATH, chrome_options=options)
def some_long_calculation():
driver = create_chromedriver('test')
# driver = create_chromedriver()
print(driver.capabilities['version'])
print(driver.capabilities['version'])
driver.get("https://www.yahoo.com/")
print(1)
png = driver.get_screenshot_as_png()
driver.close()
# return 'a'
return png
在本地的两个Windows上以及如果我使用
在heroku上运行此项目时,如果我在heroku上运行def some_long_calculation():
# driver = create_chromedriver('test')
driver = create_chromedriver()
我得到了预期的Yahoo屏幕截图
但是使用:
def some_long_calculation():
driver = create_chromedriver('test')
导致:
2018-07-26T15:08:21.448393+00:00 heroku[web.1]: State changed from starting to up
2018-07-26T15:08:23.555153+00:00 app[web.1]: no path found
2018-07-26T15:08:23.555197+00:00 app[web.1]: no path found
2018-07-26T15:08:23.555199+00:00 app[web.1]: ua block33
2018-07-26T15:08:25.772281+00:00 heroku[router]: at=info method=GET path="/" host=mobiletest16.herokuapp.com request_id=4f72f6b0-3d8f-418f-a01b-a0281c4f644d fwd="54.86.59.209" dyno=web.1 connect=0ms service=2219ms status=500 bytes=456 protocol=https
2018-07-26T15:08:25.770879+00:00 app[web.1]: [2018-07-26 15:08:25,756] ERROR in app: Exception on / [GET]
2018-07-26T15:08:25.770892+00:00 app[web.1]: Traceback (most recent call last):
2018-07-26T15:08:25.770894+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
2018-07-26T15:08:25.770895+00:00 app[web.1]: response = self.full_dispatch_request()
2018-07-26T15:08:25.770897+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
2018-07-26T15:08:25.770899+00:00 app[web.1]: rv = self.handle_user_exception(e)
2018-07-26T15:08:25.770900+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
2018-07-26T15:08:25.770902+00:00 app[web.1]: reraise(exc_type, exc_value, tb)
2018-07-26T15:08:25.770904+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
2018-07-26T15:08:25.770907+00:00 app[web.1]: raise value
2018-07-26T15:08:25.770908+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
2018-07-26T15:08:25.770910+00:00 app[web.1]: rv = self.dispatch_request()
2018-07-26T15:08:25.770911+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
2018-07-26T15:08:25.770913+00:00 app[web.1]: return self.view_functions[rule.endpoint](**req.view_args)
2018-07-26T15:08:25.770914+00:00 app[web.1]: File "/app/err_test.py", line 115, in sanity_check
2018-07-26T15:08:25.770916+00:00 app[web.1]: png = some_long_calculation()
2018-07-26T15:08:25.770918+00:00 app[web.1]: File "/app/err_test.py", line 89, in some_long_calculation
2018-07-26T15:08:25.770919+00:00 app[web.1]: driver = create_chromedriver('test')
2018-07-26T15:08:25.770921+00:00 app[web.1]: File "/app/err_test.py", line 78, in create_chromedriver
2018-07-26T15:08:25.770923+00:00 app[web.1]: return webdriver.Chrome(executable_path=CHROMEDRIVER_PATH, options=options)
2018-07-26T15:08:25.770925+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 75, in __init__
2018-07-26T15:08:25.770926+00:00 app[web.1]: desired_capabilities=desired_capabilities)
2018-07-26T15:08:25.770928+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 156, in __init__
2018-07-26T15:08:25.770929+00:00 app[web.1]: self.start_session(capabilities, browser_profile)
2018-07-26T15:08:25.770931+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 251, in start_session
2018-07-26T15:08:25.770933+00:00 app[web.1]: response = self.execute(Command.NEW_SESSION, parameters)
2018-07-26T15:08:25.770934+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 320, in execute
2018-07-26T15:08:25.770936+00:00 app[web.1]: self.error_handler.check_response(response)
2018-07-26T15:08:25.770937+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
2018-07-26T15:08:25.770939+00:00 app[web.1]: raise exception_class(message, screen, stacktrace)
2018-07-26T15:08:25.770944+00:00 app[web.1]: selenium.common.exceptions.SessionNotCreatedException: Message: session not created exception
2018-07-26T15:08:25.770946+00:00 app[web.1]: from tab crashed
2018-07-26T15:08:25.770948+00:00 app[web.1]: (Session info: headless chrome=68.0.3440.75)
2018-07-26T15:08:25.770949+00:00 app[web.1]: (Driver info: chromedriver=2.40.565383 (76257d1ab79276b2d53ee976b2c3e3b9f335cde7),platform=Linux 4.4.0-1019-aws x86_64)
2018-07-26T15:08:25.771063+00:00 app[web.1]:
2018-07-26T15:08:25.773980+00:00 app[web.1]: 10.30.235.45 - - [26/Jul/2018:15:08:25 +0000] "GET / HTTP/1.1" 500 291 "https://dashboard.heroku.com/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
我该如何工作?
您可以在以下位置下载适用于Windows和Heroku的项目:
https://github.com/kc1/mobiletest
(请记住,如果您部署到heroku,则必须将FLASK_CONFIG设置为生产环境。此外,您还需要添加屏幕快照中显示的3个buildpack。)