为什么使用 Selenium webdriver 在我的 Mac 上本地执行 JS

Docker 环境: Docker Container、ubuntu:18.10、geckodriver-v0.23.0-linux64、selenium-3.14.1


本地环境: Mac OSX、geckodriver-v0.23.0-linux64、selenium-3.14.1


为什么在我的 Mac 本地执行 JS 可以正常工作,但在 Docker 容器中却不能?


在 nfl.com 上触发错误的 Python 函数:


def scroll_down(driver, value):

    """ Scroll down some """

    driver.execute_script("window.scrollBy(0,"+str(value)+")")

来自 nfl.com 的追溯


Traceback (most recent call last):

  File "/app/foo/foo.py", line 159, in <module>

    main()

  File "/app/foo/foo.py", line 150, in main

    detect_full_html_loaded(driver)

  File "/app/foo/foo.py", line 55, in detect_full_html_loaded

    scroll_down(driver, 1000)

  File "/app/foo/foo.py", line 45, in scroll_down

    driver.execute_script("window.scrollBy(0,"+str(value)+")")

  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 636, in execute_script

    'args': converted_args})['value']

  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute

    self.error_handler.check_response(response)

  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response

    raise exception_class(message, screen, stacktrace)

selenium.common.exceptions.WebDriverException: Message: Failed to decode response from marionette

在 nytimes.com 和 sony.com 上触发错误的 Python 函数:


def capture_screenshot(assets_dir, url, sleep, driver):

    """ Take simple screenshot of above-the-fold """

    driver.get(url)

    time.sleep(sleep)

    driver.set_window_size(1400, 700)

    Image.open(BytesIO(driver.get_screenshot_as_png())).save(os.path.join(assets_dir,'screenshot.png'))

    print('Created: "' + assets_dir  + 'screenshot.png' + '"')


同样,这在本地工作正常。


不负相思意
浏览 220回答 1
1回答

慕丝7291255

这是一个与@Andersson 建议的资源相关的问题。在 docker-compose.yml 中添加适当的 shm_size 修复了 Chrome 和 Firefox Gecko 网络驱动程序中的问题。https://docs.docker.com/compose/compose-file/#shm_size
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python