【问题标题】:phantomjs issue on mac with jupyter notebook带有jupyter笔记本的mac上的phantomjs问题
【发布时间】:2019-03-16 00:45:39
【问题描述】:

我正在尝试使用在 repo 中找到的一些代码

https://github.com/AlbanyCompSci/aeries-api

基本上是为了帮助我登录这个名为 aeries 的年级门户网站并为我的孩子提供高中成绩。但我不断收到与“phantomjs”相关的错误。同样在它引发关于 chrome 需要无头的错误之前。

下面有一些示例,我从本地目录加载模块,并添加了一些代码来尝试解决错误。我在带有 python 3.6 的 jupyter notebook 中的 mac 上运行它。非常感谢任何提示。

代码:

import pandas as pd
import numpy as np
import Main as m
import Gradebooks as Gb
import GradebookDetails as GD
import Assignments as Ag
import AeriesSession as Ar


# below added to try to resolve phantomjs errors

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()

options.add_argument('headless')

driver = webdriver.PhantomJS(executable_path='/Users/username/Desktop/Stuff/Aeries/api_version/phantomjs-2.1.1-macosx/bin/phantomjs.exe')

警告:

/Users/username/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/phantomjs/webdriver.py:49: UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
  warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless '

错误:

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/common/service.py in start(self)
     75                                             stderr=self.log_file,
---> 76                                             stdin=PIPE)
     77         except TypeError:

~/anaconda/envs/py36/lib/python3.6/subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors)
    708                                 errread, errwrite,
--> 709                                 restore_signals, start_new_session)
    710         except:

~/anaconda/envs/py36/lib/python3.6/subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, restore_signals, start_new_session)
   1343                             err_msg += ': ' + repr(err_filename)
-> 1344                     raise child_exception_type(errno_num, err_msg, err_filename)
   1345                 raise child_exception_type(err_msg)

FileNotFoundError: [Errno 2] No such file or directory: '/Users/username/Desktop/Stuff/Aeries/api_version/phantomjs-2.1.1-macosx/bin/phantomjs.exe': '/Users/username/Desktop/Stuff/Aeries/api_version/phantomjs-2.1.1-macosx/bin/phantomjs.exe'

During handling of the above exception, another exception occurred:

WebDriverException                        Traceback (most recent call last)
<ipython-input-1-653e61cc9221> in <module>()
     14 options.add_argument('headless')
     15 
---> 16 driver = webdriver.PhantomJS(executable_path='/Users/username/Desktop/Stuff/Aeries/api_version/phantomjs-2.1.1-macosx/bin/phantomjs.exe')

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/phantomjs/webdriver.py in __init__(self, executable_path, port, desired_capabilities, service_args, service_log_path)
     54             service_args=service_args,
     55             log_path=service_log_path)
---> 56         self.service.start()
     57 
     58         try:

~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/common/service.py in start(self)
     81                 raise WebDriverException(
     82                     "'%s' executable needs to be in PATH. %s" % (
---> 83                         os.path.basename(self.path), self.start_error_message)
     84                 )
     85             elif err.errno == errno.EACCES:

WebDriverException: Message: 'phantomjs.exe' executable needs to be in PATH.

更新:

代码:

import pandas as pd
import numpy as np
import Main as m
import Gradebooks as Gb
import GradebookDetails as GD
import Assignments as Ag
import AeriesSession as Ar
import os

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()

options.add_argument('headless')


driver = webdriver.Chrome(executable_path=os.path.abspath('/Users/username/Desktop/Stuff/Aeries/api_version/chromedriver'), chrome_options=chrome_options) 

driver.set_window_size(1120, 550) 
driver.get("duckduckgo.com/") 
driver.find_element_by_id('search_form_input_homepage').send_keys('realpython') 
driver.find_element_by_id('search_button_homepage').click() 
print(driver.current_url)
driver.quit()

错误:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-48f57c808807> in <module>()
     17 #downlod Chrome driver.exe
     18 
---> 19 driver = webdriver.Chrome(executable_path=os.path.abspath('/Users/username/Desktop/Stuff/Aeries/api_version/chromedriver'), chrome_options=chrome_options)
     20 
     21 driver.set_window_size(1120, 550)

NameError: name 'chrome_options' is not defined

【问题讨论】:

  • Phantomjs 现在已被弃用。您可以使用 Chrome 无头
  • @AnkurSingh 感谢您这么快回复我。我对此很陌生。我正在运行在 chrome 上的 jupyter 笔记本的单元格中运行它。我添加了所有试图解决问题的“webdriver”内容。但在我启动 chrome 之前,我需要从终端做些什么吗?能否请您概括地告诉我 phantomjs 和 chrome headless 正在做什么?
  • 什么都不需要做 ..from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_argument('headless') #downlod Chrome driver.exe driver = webdriver.Chrome(executable_path=os.path.abspath(“chromedriver”), chrome_options=chrome_options) driver.set_window_size(1120, 550) driver.get("duckduckgo.com/") driver.find_element_by_id('search_form_input_homepage').send_keys(" realpython") driver.find_element_by_id("search_button_homepage").click() print driver.current_url driver.quit()
  • 试试上面的代码。
  • @AnkurSingh 再次感谢您的帮助。我添加了对原始帖子的更新。我下载了 chromedriver.exe 并尝试运行您建议的代码。现在我收到 chrome_options 的名称错误。我的代码有错误吗?

标签: python-3.x selenium selenium-webdriver webdriver phantomjs


【解决方案1】:

此错误消息...

WebDriverException: Message: 'phantomjs.exe' executable needs to be in PATH.

...表示在指定位置找不到 phantomjs.exe。

好像有点乱如下:

  • PhantomJS 浏览器 本身是一个无头 Web 浏览器,可使用 JavaScript 编写脚本,可以在 Windows、macOS、 上运行Linux 和 FreeBSD。因此,您无需显式传递参数 headles,您可以将其移除。
  • 您没有提到底层操作系统。
    • 如果您使用的是 Linux 64 位操作系统,您需要从 Download PhantomJS 下载 phantomjs-2.1.1-linux-x86_64.tar.bz2,解压缩二进制文件并同时提及 phantomjs 的绝对路径你需要去掉扩展部分的二进制文件(即.exe)
    • 如果您使用的是 Linux 32 位操作系统,您需要从 Download PhantomJS 下载 phantomjs-2.1.1-linux-i686.tar.bz2,解压缩二进制文件并同时提及 phantomjs 的绝对路径你需要去掉扩展部分的二进制文件(即.exe)
    • 如果您使用的是 Mac OS X,您需要从 Download PhantomJS 下载 phantomjs-2.1.1-macosx.zip,解压缩二进制文件并同时提及 phantomjs 二进制文件的绝对路径你需要去掉扩展部分(即.exe)
    • 如果您使用的是 Windows 操作系统,您需要从 Download PhantomJS 下载 phantomjs-2.1.1-linux-x86_64.tar.bz2,提取二进制文件并同时提及您需要的 phantomjs 二进制文件的路径提供包含扩展名的绝对路径(即phantomjs.exe)
  • 尽管您遵守了弃用通知,但支持仍然存在。所以暂时你可以忽略错误:

    C:\Python\lib\site-packages\selenium\webdriver\phantomjs\webdriver.py:49: UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
      warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless '
    
  • 在 Windows8 系统上,这里是工作代码:

    from selenium import webdriver
    
    driver = webdriver.PhantomJS(executable_path=r'C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe')
    driver.get('https://www.google.com/')
    print(driver.title)
    driver.quit()
    
  • 控制台输出:

    C:\Python\lib\site-packages\selenium\webdriver\phantomjs\webdriver.py:49: UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
      warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless '
    Google
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-15
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    相关资源
    最近更新 更多