docker工人:使用容器与无头seleniumChromedriver

我试图将peroumal1的“docker-chrome-selenium”容器与使用Selenium的刮码代码链接到另一个容器。

他将他的容器暴露在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 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二进制文件并将其添加到path来解决此问题,但是我不知道该怎么做。

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

编辑:我也试图用Selenium的官方图像做到这一点,不幸的是,它也不工作(同样的错误消息要求chromedriver二进制文件出现)。

有什么需要在Python代码上完成?

谢谢!

更新:正如@ peroumal1所说,问题是我没有连接到使用Selenium的远程驱动程序 。 然而,我做了之后,我有连接问题( urllib2.URLError: <urlopen error [Errno 111] Connection refused> ),直到我修改了Selenium驱动程序连接到的IP地址(当使用boot2docker ,您必须连接到虚拟机器的IP而不是你的计算机的本地主机,你可以通过inputboot2docker ip来find),并更改了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,而是代码。 Selenium映像通过远程Webdriver为Selenium服务器提供了一个接口,并且提供的代码尝试使用chromedriver直接实例化Chrome浏览器,只要可以从环境访问chromedriver,就可以使用Selenium Python绑定。

也许这会更好地使用文档中的示例:

 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)