Selenium - 'Service'对象没有属性'process'



我正在尝试使用Python 3.5在Ubuntu 16.04实例上运行一个简单的程序。程序如下;

from bs4 import BeautifulSoup
from selenium import webdriver
driver = webdriver.PhantomJS("p/phantomjs")
driver.get("http://www.bbc.co.uk")
s = BeautifulSoup(driver.page_source, "lxml")
print(s.findAll("a"))
try:
   driver.close()
except AttributeError:
   pass

所有模块安装正确。然而,当我运行程序时,我收到以下错误:

Traceback (most recent call last):
  File "t.py", line 4, in <module>
    driver = webdriver.PhantomJS("p/phantomjs")
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/phantomjs/webdriver.py", line 52, in __init__
    self.service.start()
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/common/service.py", line 64, in start
    stdout=self.log_file, stderr=self.log_file)
  File "/usr/lib/python3.5/subprocess.py", line 947, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child
    raise child_exception_type(errno_num, err_msg)
OSError: [Errno 8] Exec format error
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.phantomjs.service.Service object at 0x7fb05cd964a8>>
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/common/service.py", line 163, in __del__
    self.stop()
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/common/service.py", line 135, in stop
    if self.process is None:
AttributeError: 'Service' object has no attribute 'process'

这似乎是一个问题与硒而不是与PhantomJS。但是,我不知道如何使程序正常工作。

在其他类似的问题中,问题似乎与关闭headless实例有关。但是,当我尝试实例化PhantomJS时,就会收到此错误。

如何解决这个问题?

如果p文件夹(如您所提到的)位于与脚本相同的目录中,那么您可能需要使用类似

的内容开始代码。
from bs4 import BeautifulSoup
from selenium import webdriver
import os
path_to_phantom_js = os.path.dirname(__file__) + '/p/phantomjs'
driver = webdriver.PhantomJS(path_to_phantom_js)

注:如果不工作,告诉我print(path_to_phantom_js)

的输出

最新更新