折腾:
【记录】尝试用Python操作PhantomJS+Selenium去模拟购物操作
期间,结果用代码:
from selenium import webdriver
browser = webdriver.Chrome()
browser.get(‘http://www.baidu.com/‘)
在PyCharm中调试出错:
好像是:
“‘%s’ executable needs to be in PATH. %s” % (
还是:
[Errno 2] No such file or directory
-》
的确是需要在命令行中运行:
/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py –multiproc –qt-support –client 127.0.0.1 –port 53046 —file xxx/AutoOrder.py
warning: Debugger speedups using cython not found. Run ‘”/usr/local/opt/python/bin/python2.7″ “/Applications/PyCharm.app/Contents/helpers/pydev/setup_cython.py” build_ext –inplace’ to build.
pydev debugger: process 16353 is connecting
Connected to pydev debugger (build 163.15188.4)
Traceback (most recent call last):
File “/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py”, line 1596, in <module>
globals = debugger.run(setup[‘file’], None, None, is_module)
File “/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py”, line 974, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File “/xxx/AutoOrder.py”, line 3, in <module>
browser = webdriver.Chrome()
File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py”, line 62, in __init__
self.service.start()
File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/common/service.py”, line 81, 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
那换到命令行中试试:
结果命令行中也找不到:
➜ AutoOrder ll
total 8
-rw-r–r– 1 crifan staff 97B Nov 9 20:27 AutoOrder.py
➜ AutoOrder python AutoOrder.py
Traceback (most recent call last):
File “AutoOrder.py”, line 1, in <module>
from selenium import webdriver
ImportError: No module named selenium
➜ AutoOrder python –version
Python 2.7.10
➜ AutoOrder where python
/usr/bin/python
➜ AutoOrder which pytho
pytho not found
➜ AutoOrder which python
/usr/bin/python
➜ AutoOrder
换到Mac的Terminal中也是同样现象:
➜ AutoOrder python AutoOrder.py
Traceback (most recent call last):
File “AutoOrder.py”, line 3, in <module>
browser = webdriver.Chrome()
File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py”, line 62, in __init__
self.service.start()
File “/usr/local/lib/python2.7/site-packages/selenium/webdriver/common/service.py”, line 81, 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
看来只能是想办法解决上面那个:
selenium.common.exceptions.WebDriverException: Message: chromedriver executable needs to be in PATH
linux – Running Selenium WebDriver python bindings in chrome – Stack Overflow
python – selenium – chromedriver executable needs to be in PATH – Stack Overflow
-》
Downloads – ChromeDriver – WebDriver for Chrome
-》
https://chromedriver.storage.googleapis.com/index.html?path=2.33/
-》
https://chromedriver.storage.googleapis.com/2.33/chromedriver_mac64.zip
linux – Running Selenium WebDriver python bindings in chrome – Stack Overflow
-》
试试
➜ AutoOrder brew install chromedriver
==> Downloading https://chromedriver.storage.googleapis.com/2.32/chromedriver_mac64.zip
ChromeDriver – WebDriver for Chrome
“ChromeDriver
WebDriver is an open source tool for automated testing of webapps across many browsers. It provides capabilities for navigating to web pages, user input, JavaScript execution, and more. ChromeDriver is a standalone server which implements WebDriver’s wire protocol for Chromium. |
”
https://chromedriver.storage.googleapis.com/index.html
-》
https://chromedriver.storage.googleapis.com/index.html?path=2.32/
https://chromedriver.storage.googleapis.com/index.html?path=2.33/
下载得到:
chromedriver_mac64.zip
解压后得到chromedriver:
然后就可以去试试了。
对于:
➜ AutoOrder echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
所以干脆把chromedriver拷贝到/usr/local/bin吧
不过暂时试试软链接是否可行:
➜ bin git:(master) ✗ ln -s /Users/crifan/dev/dev_root/projects/亚马逊/自动下单/AutoOrder/chromedriver chromedriver
➜ bin git:(master) ✗ ll
。。。
lrwxr-xr-x 1 crifan admin 81B 11 9 21:15 chromedriver -> /Users/crifan/dev/dev_root/projects/亚马逊/自动下单/AutoOrder/chromedriver
结果再去运行,倒是貌似可以了:
然后点击一下才能继续运行:
【总结】
此处PyCharm中运行selenium报错:
selenium.common.exceptions.WebDriverException: Message: chromedriver executable needs to be in PATH
的原因:
不是PyCharm的问题,而是本身selenium如果用Chrome的话,需要先下载和能调用到chromedriver的。
对应的办法:
1.Mac中,可以直接用brew下载:
brew install chromedriver
其会自动去下载对应的chromedriver的mac的包的:
https://chromedriver.storage.googleapis.com/2.32/chromedriver_mac64.zip
-》但是此处翻墙网速不行,即使翻墙速度没法下载。
2.自己手动下载
去:
https://chromedriver.storage.googleapis.com/index.html
可以找到所有的版本的,包括最新版本的:
https://chromedriver.storage.googleapis.com/index.html?path=2.33/
中的:
https://chromedriver.storage.googleapis.com/2.33/chromedriver_mac64.zip
(或:
Windows平台的话是:
https://chromedriver.storage.googleapis.com/2.33/chromedriver_win32.zip
)
下载后,解压得到chromedriver的二进制文件
-》放到PATH的路径中即可
-》此处通过:
echo $PATH
而得到很多路径
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
选择其中一个:
/usr/local/bin
去:
制作一个chromedriver的软链接
ln -s /your/path/to//chromedriver chromedriver
直接拷贝chromedriver到该目录
均可。
之后代码中再去调用:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get(‘http://www.baidu.com/‘)
assert u”百度” in driver.title
searchElement = driver.find_element_by_name(“wd”)
searchElement.send_keys(“crifan”)
searchElement.send_keys(Keys.RETURN)
print driver.page_source
就可以正常调用Chrome浏览器内容,显示出Chrome浏览器了:
【后记】
后来在:
Selenium take screenshot – Python Tutorial
看到好像有个ChromeOptions,然后可以指定binary_location
-》如果可以,这样就可以不用非要把
chromedriver
放到PATH环境变量中了
直接放到和程序代码一起,便于后续把下载好的chromedriver和python代码一起给别人使用,别人就不用安装了。
去试试
selenium ChromeOptions
Capabilities & ChromeOptions – ChromeDriver – WebDriver for Chrome
顺带再去看看chromedriver的信息:
➜ python git:(master) ll /usr/local/bin/chromedriver
lrwxr-xr-x 1 crifan admin 81B 11 9 21:15 /usr/local/bin/chromedriver -> /Users/crifan/dev/dev_root/projects/亚马逊/自动下单/AutoOrder/chromedriver
➜ python git:(master) /Users/crifan/dev/dev_root/projects/亚马逊/自动下单/AutoOrder
➜ AutoOrder chromedriver –help
Usage: chromedriver [OPTIONS]
Options
–port=PORT port to listen on
–adb-port=PORT adb server port
–log-path=FILE write server log to file instead of stderr, increases log level to INFO
–verbose log verbosely
–version print the version number and exit
–silent log nothing
–url-base base URL path prefix for commands, e.g. wd/url
–port-server address of server to contact for reserving a port
–whitelisted-ips comma-separated whitelist of remote IPv4 addresses which are allowed to connect to ChromeDriver
➜ AutoOrder chromedriver –version
ChromeDriver 2.33.506106 (8a06c39c4582fbfbab6966dbb1c38a9173bfb1a2)
➜ AutoOrder which chromedriver
/usr/local/bin/chromedriver
DesiredCapabilities · SeleniumHQ/selenium Wiki
Capabilities & ChromeOptions – ChromeDriver – WebDriver for Chrome
用了代码:
# driver = webdriver.Chrome()
chromeOptions = webdriver.ChromeOptions()
chromeOptions.binary_location = “./libs/chromedriver”
logging.info(“chromeOptions=%s”, chromeOptions)
sys.path.append(“./libs”)
driver = webdriver.Chrome(chrome_options=chromeOptions)
结果还是报之前错误:
selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’ executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
-》看来代码中用sys.path.append加上chromedriver的路径的做法,并不能设置PATH
-》所以还是会报必须在PATH中才行。
-》所以这条路行不通,放弃。
再去设置路径是设置的是绝对路径看看是否可行。
结果:
chromeOptions = webdriver.ChromeOptions()
curPath = os.getcwd()
logging.info(“curPath=%s”, curPath)
chromeBinFullPath = os.path.join(curPath, “libs/chromedriver”)
logging.info(“chromeBinFullPath=%s”, chromeBinFullPath)
chromeOptions.binary_location = chromeBinFullPath
logging.info(“chromeOptions=%s”, chromeOptions)
sys.path.append(“./libs”)
driver = webdriver.Chrome(chrome_options=chromeOptions)
问题依旧。还是不行。
所以放弃这个思路。
【后记 2017-11-18】
后来看到:
8. Appendix: Frequently Asked Questions — Selenium Python Bindings 2 documentation
中提到:
webdriver.Chrome初始化时可以传入executable_path指定chromedriver二进制的位置
去试试,结果:
driver = webdriver.Chrome(executable_path=“./libs/chromdriver”)
或:
logging.info(“chromeOptions=%s”, chromeOptions)
driver = webdriver.Chrome(executable_path=”./libs/chromdriver”, chrome_options=chromeOptions)
都还是报同样的错误:
selenium.common.exceptions.WebDriverException: Message: ‘chromdriver’ executable needs to be in PATH.
所以,看来还是不行。
-》感觉根本就不支持设置binary的path,这条路完全不通。
转载请注明:在路上 » 【已解决】Mac中PyCharm调试selenium出错:selenium.common.exceptions.WebDriverException chromedriver executable needs to be in PATH