phantomjs issue on mac with jupyter notebook

2019-08-25 17:59发布

问题:

I’m trying to experiment with some code I found at the repo

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

basically to help me login in to this grade portal called aeries and pull my kids high school grades. But I keep getting errors related to “phantomjs”. Also before it was throwing an error about chrome needing to be headless.

I’ve got some examples below where I’m loading the module from a local directory and I’ve added the some code to try to resolve the errors. I’m running it on a mac in a jupyter notebook with python 3.6. Any tips greatly appreciated.

Code:

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')

Warning:

/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 '

Error:

---------------------------------------------------------------------------
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.

Update:

code:

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()

Error:

---------------------------------------------------------------------------
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

回答1:

This error message...

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

...implies that the phantomjs.exe was not found in the specified location.

There seems to be a bit of messup as follows:

  • PhantomJS Browser itself is a headless web browser scriptable with JavaScript which can run on Windows, macOS, Linux, and FreeBSD. So you don't need to explicitly pass the argument headles and you can remove it.
  • You havn't mentioned about the underlying Operating System.
    • If you are on Linux 64 bit OS you need to download the phantomjs-2.1.1-linux-x86_64.tar.bz2 from Download PhantomJS, extract the binary and while mentioning the absolute path of the phantomjs binary you need to strip off the extension part (i.e. .exe)
    • If you are on Linux 32 bit OS you need to download the phantomjs-2.1.1-linux-i686.tar.bz2 from Download PhantomJS , extract the binary and while mentioning the absolute path of the phantomjs binary you need to strip off the extension part (i.e. .exe)
    • If you are on Mac OS X you need to download the phantomjs-2.1.1-macosx.zip from Download PhantomJS , extract the binary and while mentioning the absolute path of the phantomjs binary you need to strip off the extension part (i.e. .exe)
    • If you are on Windows OS you need to download the phantomjs-2.1.1-linux-x86_64.tar.bz2 from Download PhantomJS , extract the binary and while mentioning the path of the phantomjs binary you need to provide the absolute path including the extension (i.e. phantomjs.exe)
  • Though you observe the deprecation notice but the support is still there. So for the time being you can ignore the error:

    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 '
    
  • On Windows8 system here is the working code:

    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()
    
  • Console Output:

    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