【发布时间】:2018-08-15 18:53:07
【问题描述】:
由于超过 2 小时,我试图在非容器 alpine 上使用 chrome 在 python 中设置 Selenium。我不知道为什么我会收到此错误消息:
browser = webdriver.Chrome()
File "/usr/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 68, in __init__
self.service.start()
File "/usr/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
有人可以帮助我吗? 非常感谢
PS: 这是我的 dockerfile 的一部分
RUN wget "https://chromedriver.storage.googleapis.com/2.36/chromedriver_linux64.zip" &&\
busybox unzip chromedriver_linux64.zip &&\
chmod a+x chromedriver &&\
mv chromedriver /usr/bin/
这是我的方法:
def __init__(self, url, parser = "lxml") :
self.url = url
self.parser = parser
browser = webdriver.Chrome()
browser.get(self.url)
...
ps:码头文件:
FROM alpine:3.7
RUN apk add --update bash &&\
apk update &&\
apk upgrade
RUN apk add --no-cache python-dev ;\
apk add --no-cache python
#telecharge lib python scraper
RUN apk add --no-cache py-pip &&\
apk add --no-cache linux-headers &&\
apk add --no-cache texinfo &&\
apk add --no-cache gcc &&\
apk add --no-cache g++ &&\
apk add --no-cache gfortran &&\
apk add --no-cache libxml2-dev &&\
apk add --no-cache xmlsec-dev &&\
apk add --no-cache py-requests &&\
apk add --no-cache chromium &&\
apk add --no-cache chromium-chromedriver
#install lib python scraper
RUN pip install beautifulsoup4 &&\
pip install requests &&\
pip install lxml &&\
pip install html5lib &&\
pip install urllib3 &&\
pip install -U selenium
#telecharge driver pour selenium
RUN wget "https://chromedriver.storage.googleapis.com/2.36/chromedriver_linux64.zip" &&\
busybox unzip chromedriver_linux64.zip &&\
chmod a+x chromedriver &&\
mv chromedriver /usr/bin/
# prepare le shell
CMD ["bash"]
WORKDIR "/root"
【问题讨论】:
-
您是否尝试过使用
os.chdir()手动将目录更改为chromedriver所在的目录? -
什么意思?我不明白...我试过这个:webdriver.Chrome("/usr/bin/chromedriver")
-
在您的 Python 脚本中,在使用
browser = webdriver.Chrome()创建 chromedriver 的实例之前,使用os.chdir('Path to chromedriver')将目录更改为您的 chromedriver 在您的机器上所在的路径 -
或者试试
webdriver.Chrome(executable_path="/usr/bin/chromedriver") -
你确定你有正确的权限来执行它,或者它是否被设置为一个可执行文件?
标签: python python-2.7 selenium docker