我将 Selenium 和 PhantomJS 与我正在编写的 Python 脚本一起使用,但我在通过脚本启动 PhantomJS 服务时遇到困难,我不断收到此错误:
Traceback (most recent call last):
File "asp_scraper.py", line 59, in <module>
scraper = DanishScraper()
File "asp_scraper.py", line 20, in __init__
self.driver = webdriver.PhantomJS(executable_path='./phantomjs/bin/phantomjs')
File "/Library/Python/2.7/site-packages/selenium/webdriver/phantomjs/webdriver.py", line 53, in __init__
self.service.start()
File "/Library/Python/2.7/site-packages/selenium/webdriver/phantomjs/service.py", line 84, in start
raise WebDriverException("Can not connect to GhostDriver")
selenium.common.exceptions.WebDriverException: Message: Can not connect to GhostDriver
到目前为止,这是我的应用程序:
import re
import string
import urlparse
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException, WebDriverException
from bs4 import BeautifulSoup
class DanishScraper(object):
def __init__(self):
self.url = "http://www.realtor.ca/RealtorSearch.aspx"
self.driver = webdriver.PhantomJS(executable_path='./phantomjs/bin/phantomjs')
self.driver.set_window_size(1120, 550)
def scrape(self):
self.driver.get(self.url)
# Select state selection dropdown
select = Select(self.driver.find_element_by_id('ProvinceIds'))
option_indexes = range(1, len(select.options))
for index in option_indexes:
select.select_by_index(index)
self.driver.find_element_by_id
print option_indexes
if __name__ == '__main__':
scraper = DanishScraper()
scraper.scrape()
它与可执行文件的位置有潜在关系吗?我在 Mac OS 10.11 (El Capitan) 上运行 Python 2.7。
请您参考如下方法:
我在 Mac OS 上遇到类似症状的问题。安装从 here 下载的已打补丁的 PhantomJS 二进制文件帮助我解决了这个问题。
