Rahul
Rahul

Reputation: 2738

Where to place PhantomJS exe?

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:

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 ?

Upvotes: 0

Views: 2301

Answers (3)

Davide Patti
Davide Patti

Reputation: 3471

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.

Upvotes: 0

Rahul
Rahul

Reputation: 2738

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) 

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193108

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

Upvotes: 0

Related Questions