0

I am trying to use PhantomJS with Selenium and Python.

My understanding is:

I will have to write Python script utilizing Selenium package which will interact with Selenium to operate on PhantomJS WebDriver to automate web application testing.

I have installed following:

  • Python v3.5.1.
  • Selenium using pip install selenium v3.7.0.
  • PhantomJS v2.1.1

In meantime I tested using Chrome WebDriver by placing it in PATH, and it executes without errors. Following is my script to open google.com using chrome webdriver.

from selenium import webdriver

driver = webdriver.Chrome() # or add to your PATH
driver.get('https://google.com/')

Using PhantomJS:

from selenium import webdriver

url = "http://www.google.com"
path_phantom = r'H:\phantomjs\bin\phantomjs.exe'

driver = webdriver.PhantomJS(executable_path=path_phantom)
driver.get(url)
driver.save_screenshot(r'H:\out.png')
driver.quit()

Errors:

Traceback (most recent call last): File "C:\Users\acer\Desktop\testing\openYoutube.py", line 5, in driver = webdriver.PhantomJS() File "C:\Users\acer\AppData\Local\Programs\Python\Python35-32\lib\site-package s\selenium\webdriver\phantomjs\webdriver.py", line 51, in init log_path=service_log_path) File "C:\Users\acer\AppData\Local\Programs\Python\Python35-32\lib\site-package s\selenium\webdriver\phantomjs\service.py", line 50, in init service.Service.init(self, executable_path, port=port, log_file=open(log _path, 'w')) PermissionError: [Errno 13] Permission denied: 'ghostdriver.log'

Am I misplacing PhantomJS exe or missing any step ?

Rahul
  • 2,658
  • 12
  • 28
  • place it anywhere but just ensure it is available in environment path and you're getting permission error so where ever it is present it is not able create a log file I would recommend to place it in such directory where administrator rights are not required – Mahesh Karia Nov 10 '17 at 04:44

3 Answers3

0

You can place the PhantomJS v2.1.1 binary at any location within your system and use the following code block :

from selenium import webdriver

url = "http://www.url.com.br/contact.asp"
path_phantom = r'C:\your_path\phantomjs-2.1.1-windows\bin\phantomjs.exe'

driver = webdriver.PhantomJS(executable_path=path_phantom)
driver.set_window_size(1400,1000)
driver.get(url)

Update :

Please consider the following points and try the following code block with debug messages:

  • Run CCleaner tool to wipe off all the OS chores from your system.
  • You can opt for a System Reboot.
  • Try to keep the Python Application, WebBrowser binaries and the WebDriver binaries i.e. phantomjs.exe on the same drive.

    from selenium import webdriver
    
    url = "http://www.google.com"
    path_phantom = r'C:\Utility\phantomjs-2.1.1-windows\bin\phantomjs.exe'
    
    driver = webdriver.PhantomJS(executable_path=path_phantom)
    print("PhantomJS browser invoked")
    driver.get(url)
    print("Browser Initialized")
    driver.save_screenshot("C://Utility//out.png")
    driver.quit()
    print("Browser Closed")
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Problem seems to be with the log file.

Changing path of log file solved this problem.

path_phantom = r'H:\phantomjs\bin\phantomjs.exe'
log_path=r'H:\ghostdriver.log'  #changed path to a temporary file.
# service_log_path is required to change path of log file.
driver = webdriver.PhantomJS(executable_path=path_phantom,service_log_path=log_path) 
Rahul
  • 2,658
  • 12
  • 28
0

From your error:

PermissionError: [Errno 13] Permission denied: 'ghostdriver.log

Seems that it try to create this file ghostdriver.log but fails because of the permissions.

As suggested in this answer, try to add the argument

service_log_path=os.path.devnull 

to the function webdriver.PhantomJS().

Or make sure it is able to create the file.

Davide Patti
  • 3,391
  • 2
  • 18
  • 20