我用brew来安装selenium(webdriver):
$ phantomjs --version
2.1.1
然后,我尝试在pycharm中运行此代码,但得到错误:
from selenium import webdriver
if __name__ == '__main__':
driver = webdriver.PhantomJS()
driver.get("https://www.amazon.com/")
print driver.current_url
有错误消息:
Traceback (most recent call last):
File "/Users/haomao/pythonVir/workspace/tutorial/tutorial/url_judge.py", line 166, in <module>
driver = webdriver.PhantomJS()
File "/Users/haomao/pythonVir/envt1/lib/python2.7/site-packages/selenium/webdriver/phantomjs/webdriver.py", line 52, in __init__
self.service.start()
File "/Users/haomao/pythonVir/envt1/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: 'phantomjs' executable needs to be in PATH.
但是当我在终端尝试相同的代码并且它有效时。
(envt1) haomaodeMacBook-Pro:~ haomao$ python
Python 2.7.10 (default, Feb 7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> driver = webdriver.PhantomJS()
>>> driver.get("https://www.amazon.com/")
>>> print driver.current_url
https://www.amazon.com/
>>>
我不知道这个问题,并在谷歌搜索,它告诉我,我需要添加PhantomJS()的路径,如 webdriver.PhantomJS(路径)。但是,我不知道幻影的位置。如果您有任何想法,请帮助我。
提前致谢!
6月23日
我找到了phantomjs的路径,&#39; / usr / local / phantomjs&#39;并将其添加到代码中,如webdriver.PhantomJS(&#39; / usr / local / phantomjs&#39;)。
但是当我将项目带到Linux时,我需要更改此代码,还有其他方法吗?