【发布时间】: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