通过Selenium与Python一起使用PhantomJS - WebDriverException错误

时间:2013-11-11 16:30:31

标签: selenium-webdriver phantomjs

我正在使用以下代码,如下所示:Is there a way to use PhantomJS in Python?

from selenium import webdriver

driver = webdriver.PhantomJS()
driver.set_window_size(1024, 768) # optional
driver.get('https://google.com/')
driver.save_screenshot('screen.png') # save a screenshot to disk
sbtn = driver.find_element_by_css_selector('button.gbqfba')
sbtn.click()

当我尝试运行它时,我收到以下错误:

WebDriverException - “无法使用ghostdriver启动phantomjs。”

显然,可以通过将第3行替换为:

来解决此错误
driver = webdriver.PhantomJS(executable_path='/usr/local/lib/node_modules/phantomjs/lib/phantom/bin/phantomjs')

我正在使用Windows7,我的路径似乎是:“C:\ Users \ myname \ AppData \ Roaming \ npm \ node_modules \ phantomjs \ bin \ phantomjs”。我已经尝试使用它和它的变体作为executable_path,但我仍然得到相同的错误。我也尝试将文件夹位置添加到路径中。没有任何效果。我怀疑我错过了一些非常明显的东西。

1 个答案:

答案 0 :(得分:0)

啊哈!

这是一个略有不同的位置。我正在寻找的那条线是:

driver = webdriver.PhantomJS(executable_path=r'C:\Users\myname\AppData\Roaming\npm\node_modules\phantomjs\lib\phantom\phantomjs')