2016-04-21 32 views
28

我最近安装PhantomJS并遇到了我的第一次运行这个错误:无法加载的Atom'find_element”

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 

driver = webdriver.PhantomJS() 
driver.get("http://www.google.com") 
driver.find_element_by_id('some_element').send_keys('something' + Keys.RETURN) 

该代码可以使用火狐的webdriver但不与PhantomJS工作,我得到以下错误:

Traceback (most recent call last): 
    File "<PATHTOFILE>", line 20, in <module> 
    driver.find_element_by_id('lst-ib').send_keys('something' + Keys.RETURN) 
    File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 266, in find_element_by_id 
    return self.find_element(by=By.ID, value=id_) 
    File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 744, in find_element 
    {'using': by, 'value': value})['value'] 
    File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 233, in execute 
    self.error_handler.check_response(response) 
    File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/errorhandler.py", line 165, in check_response 
    raise exception_class(value) 
selenium.common.exceptions.WebDriverException: Message: Error - Unable to load Atom 'find_element' from file ':/ghostdriver/./third_party/webdriver-atoms/find_element.js' 
+0

你确定了phantomjs.exe文件,或任何文件,是在你的Python PATH?如果是这样,phantomjs进程是否处于活动状态? – DuckPuncher

回答

51

的原因是,我用apt-get install Phantomjs安装它,但它似乎是apt-get安装了一个不完整的功能Phantomjs版本,所以,从Phantomjs网站手动下载和含方向增加。 PATH环境变量。

+4

同样发生在这里:从Ubuntu 16.04 PhantomJS有这个错误,从网站的版本刚刚工作。 – epx

+10

是的,我通过执行'sudo apt purge phantomjs'来解决它,然后让npm用'npm install phantomjs-prebuilt'安装phantomjs。 – nnyby

+2

我不得不安装依赖关系http://stackoverflow.com/a/36843608/977622 – psychok7

5

对于我来说,开始工作后

apt install nodejs-legacy # just an alias node/nodejs to make npm install work 
apt purge phantomjs  # optionaly 
npm install -g phantomjs # most important part because apt installation failed for me 
7

这些是对我工作的具体步骤:

  1. 清除旧phantomjs
    apt purge phantomjs

  2. Wget的最新phantomjs (按照http://phantomjs.org/download.html
    wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2

  3. 解压它
    tar xvjf phantomjs-2.1.1-linux-x86_64.tar.bz2

  4. 感动可执行phantomjs到/ usr/bin中/(可能需要须藤)
    cp phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/bin/

+0

将二进制文件移动到'/ usr/local/bin'中。 – Onlyjob