0

我正在使用以下代码,如此处推荐: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”。我已经尝试使用它及其变体作为可执行文件路径,但我仍然得到同样的错误。我也尝试将文件夹位置添加到路径中。没有任何效果。我怀疑我遗漏了一些非常明显的东西。

4

1 回答 1

0

啊哈!

这是一个稍微不同的位置。我正在寻找的行是:

driver = webdriver.PhantomJS(executable_path=r'C:\Users\myname\AppData\Roaming\npm\node_modules\phantomjs\lib\phantom\phantomjs')
于 2013-11-11T17:02:51.647 回答