【问题标题】:Docker: using container with headless Selenium ChromedriverDocker:使用带有无头 Selenium Chromedriver 的容器
【发布时间】:2015-04-21 19:20:43
【问题描述】:

我正在尝试使用 Selenium 的抓取代码将 peroumal1's "docker-chrome-selenium" container 链接到另一个容器。

他将他的容器暴露在 4444 端口(Selenium 的默认端口),但我无法从我的爬虫容器访问它。这是我的docker-compose 文件:

chromedriver:
  image: eperoumalnaik/docker-chrome-selenium:latest

scraper:
  build: .
  command: python manage.py scrapy crawl general_course_content
  volumes:
    - .:/code
  ports:
    - "8000:8000"
  links:
    - chromedriver

这是我的爬虫 Dockerfile:

FROM python:2.7

RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/

RUN pip install --upgrade pip
RUN pip install -r requirements.txt
ADD . /code/

但是,当我尝试从我的代码中使用 Selenium(见下文)时,我收到以下错误消息:selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be available in the path. Please look at http://docs.seleniumhq.org/download/#thirdPartyDrivers and read up at http://code.google.com/p/selenium/wiki/ChromeDriver。在 Mac OS X 上,当我不使用 Docker 时,我通过下载 chromedriver binary 并将其添加到路径来解决此问题,但我不知道在这里做什么。

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('http://google.com')
driver.close()

编辑:我也在尝试使用Selenium's official images 执行此操作,但不幸的是,它也不起作用(出现要求 chromedriver 二进制文件的相同错误消息)。

Python 代码有什么需要做的吗?

谢谢!

更新:正如@peroumal1 所说,问题在于我没有连接到remote driver using Selenium。但是,在我这样做之后,我遇到了连接问题(urllib2.URLError: <urlopen error [Errno 111] Connection refused>),直到我修改了 Selenium 驱动程序连接到的 IP 地址(使用 boot2docker 时,您必须连接到虚拟机的 IP 而不是计算机的本地主机,这您可以通过键入boot2docker ip) 找到并更改docker-compose 文件。这就是我最终得到的结果:

chromedriver:
  image: selenium/standalone-chrome
  ports:
    - "4444:4444"

scraper:
  build: .
  command: python manage.py scrapy crawl general_course_content
  volumes:
    - .:/code
  ports:
    - 8000:8000
  links:
    - chromedriver

而Python代码(boot2docker我电脑上的IP地址是192.168.59.103):

driver = webdriver.Remote(
           command_executor='http://192.168.59.103:4444/wd/hub',
           desired_capabilities=DesiredCapabilities.CHROME)
driver.maximize_window()
driver.get('http://google.com')
driver.close()

【问题讨论】:

  • docker exec -it container_id env检查你的路径它显示了什么?

标签: python selenium docker selenium-chromedriver docker-compose


【解决方案1】:

我认为这里的问题可能不是 Docker,而是代码。 Selenium 图像通过远程 Webdriver 提供到 Selenium 服务器的接口,并且提供的代码尝试使用 chromedriver 直接实例化 Chrome 浏览器,这可以通过 Selenium Python 绑定实现,前提是 chromedriver 可以从环境中访问。

使用docs 中的示例可能会更好:

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

driver = webdriver.Remote(
command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME)

【讨论】:

  • 你也可以直接使用http://my-selenium-container-name:4444/wd/hub而不是输入IP地址
  • 或者更好:使用 PyPI 中的 docker 包来获取 HostIpHostPort
猜你喜欢
  • 2022-06-17
  • 2017-05-11
  • 2021-08-01
  • 2018-11-15
  • 1970-01-01
  • 2018-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多