Selenium with GhostDriver in Python on Windows - python

This is embarrassing to ask because it seems like something with so slim chance of error. I wouldn't think this would be difficult, but I've been plugging away at this for almost 3 hours now and it's giving me a headache. I've read several dozen stackoverflow threads and Google threads.
I've installed PhantomJS, added it to my System Variables PATH, and it works properly in the command line. I also installed Selenium earlier with easy_install.
The error I get is:
__init__ C:\Python27\lib\site-packages\selenium-2.39.0-py2.7.egg\selenium\webdriver\phantomjs\webdriver.py 50
start C:\Python27\lib\site-packages\selenium-2.39.0-py2.7.egg\selenium\webdriver\phantomjs\service.py 66
WebDriverException: Message: 'Unable to start phantomjs with ghostdriver.' ; Screenshot: available via screen
Here's my code:
from selenium import webdriver
driver = webdriver.PhantomJS(executable_path="C:\Python27\misc\phantomjs\phantomjs.exe")
I also tried:
from selenium import webdriver
driver = webdriver.PhantomJS()
I get the same error message. This has to be something simple that I'm doing wrong. I'd appreciate any comments or answers.
Windows 7 64 bit
Python 2.7

This may have been a version issue for you, but since I just went through setting this up on my Windows 7 PC without issues I'm going to share my 'journey' here.
First of, I'm more used to the Mac/Linux Terminal and having the python package manager pip at my disposal is essential to me. After installing Python 2.7.8 and adding ;c:\Python27 to my PATH I noticed that pip isn't included with Python versions lower than 2.7.9, so I had to add it myself. Afterwards I added ;c:\Python27\Scripts to my PATH.
After that fetching the python package selenium was as easy as typing the following into the cmd:
pip install selenium
Then I downloaded the phantomjs-1.9.7-windows.zip from here, unzipped it and placed it here:
C:\Python27\misc\phantomjs-1.9.7-windows\phantomjs.exe
From there I had a working Python 2.7/Selenium Webdriver/PhantomJS example for Windows 7.
from selenium import webdriver
import os
phantomjs_path = "C:\Python27\misc\phantomjs-1.9.7-windows\phantomjs.exe"
browser = webdriver.PhantomJS(executable_path=phantomjs_path, service_log_path=os.path.devnull)
browser.set_window_size(1400, 1000)
browser.get("https://stackoverflow.com/")
print browser.title
Note that I added the argument service_log_path=os.path.devnull to the function webdriver.PhantomJS() to prevent PhantomJS from creating a ghostdriver.log in the directory of the python file being executed.

I had the same problem running Python 3.4 on Windows Server 2012 R2. PhantomJS was failing to create the ghostdriver.log file. I followed these steps that fixed it for me:
Made sure phantomjs.exe was not showing "Blocked" on the File Properties|Security tab, and ran it as standalone app to confirm.
Deleted an old copy of the ghostdriver.log file that was in the same directory.
Ran python REPL from the console while checking to see if the code that instantiated the driver was getting called successfully.
browser = webdriver.PhantomJS(executable_path='phantomjs.exe', desired_capabilities=argdc, service_args=svc_args)

Do you any other file or directory with a same name , or a file of coding (like .. phantomjs.py) which you have named same as phantomjs is so then rename it to something else. i hope it works

Related

Selenium and python : "Message: 'geckodriver' executable needs to be in PATH. "

I was recently working with web automation, with Selenium and python, and I faced into the error almost everyone got.
from selenium import webdriver
window = webdriver.Firefox()
window.get("https://ecoledirecte.com")
But I looked up for several answers, and None of them worked ! I tried the solution for this question, but nothing changed. I checked several times, and the geckodriver is in PATH (I extracted the file into my Downloads folder, then moved the executable into my main folder). I checked if indicating the mozilla geckovriver folder in the calling of webdriver.Firefox() would help (webdriver.Firefox("C:\\Users\\user\\Downloads\\mozilla-geckodriver-9b5f85c")), but no.
Is there any reason for that ?
I think I once put it in my System32 folder (which is in PATH!) but this is probably a little sketchy. Wherever you put the geckodriver, you just have to add that location to your PATH environment variable.
Try following this instruction:
http://www.learningaboutelectronics.com/Articles/How-to-install-geckodriver-Python-windows.php
I believe you could also install in the same folder where your python file is located - it should probably pick up that folder (but I'm not sure).
this is an easy solution:
webdriver.Firefox(executable_path='Your full path to the geckodriver executable'+'geckodriver')

Can Python install files into PATH?

In order to install Selenium, step 3 on (this site indicates needing to install the chromedriver file in your PATH. I am on a work computer that does not have access to the system PATH directly. I have tried listing in the local PATH (I'm on Windows 7) variable chain like so: C:\Users\mknerr\AppData\Local\Programs\Python\Python36-32\Scripts\;C:\Users\mknerr\AppData\Local\Programs\Python\Python36-32\;C:\Users\mknerr\AppData\Local\atom\bin;C:\Users\mknerr\Programs\ChromeDriver\
(The .exe is in the ChromeDriver folder)
When I run the script with webDriver.Chrome(), I still get a WebDriverException that chromedriver needs to be in my PATH. If anyone has an idea why this isn't working from my local PATH, I'd love to hear them.
However, my real question is when I distribute this script to the rest of my team, they will likely have the same issue since my script will be calling chromedriver, which none of them will have installed, much less in their PATH. Can Python directly install a program or dependency in the PATH so they don't have to go directly accessing environment variables? I can guarantee nobody is going to feel comfortable doing that.
You can place chromedriver.exe in the same folder as the executable. Just run the program with the driver right next to it.
In our internal automation framework, we actually just distribute the Chromedriver executable as part of the framework, in the same folder as the framework's entry point.
Then, whenever we need a browser session, we do something similar to this:
import os
from selenium import webdriver
chromedriver_location = os.path.join(
os.path.dirname(os.path.abspath(__file__)), 'chromedriver.exe')
context.browser = webdriver.Chrome(executable_path=chromedriver_location)
chromedriver.exe is the default Windows name of the executable, of course; change to whatever you need.
This avoids any user setup other than installing the framework itself. No messing with PATH or any other local files.

Python Sellenium Chromedrive - can't set up the path (InstaPy)

I am playing around with this repo:
https://github.com/timgrossmann/InstaPy
I have cloned the repo, installed Python 3.6, Selenium, py virtual driver (Although I am not sure if I have to save it somewhere).
I have two questions:
1) How do I set up a path for PY command? -- What does that mean exactly? And what does it do? I have googled that, and I don't have any Python experience so I am having a hard time understanding it.
2) I have downloaded chromedriver and put it in the assets, but I am having trouble figuring out if what I put there makes sense.
When I run chromedriver, I get this error: Only local connections are allowed.
[0.011][SEVERE]: bind() returned an error, errno=48: Address already in use (48). I have tried killing it, but it says there's nothing to kill.
And when I run python quickstart.py, I get this error Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home, which I am assuming is related to the fact that I didn't set a path.
If I run python instapy/login_util.py, I get File "instapy/login_util.py", line 2, in <module>
from .time_util import sleep
ValueError: Attempted relative import in non-package.
Sorry for the long message, I am just hoping to understand everything. I have read the docs and questions from the repo, but I still feel very confused
Try setting the path for chrome driver
import os
os.environ["Selenium_Chrom_Path"] = "/foo/path_to_chrome_exec"

Error when trying to use webdriver.PhantomJS() from Selenium

Windows7, selenium 2.45.0, python 2.7.5, phantomjs 2.0.0
Installed selenium with pip (seems to be working fine otherwise), installed phantomjs from http://phantomjs.org/download.html. Moved the folder for phantomjs into a folder within C:\Python27 and then added C:\Python27...\bin\phantomjs to system Path (tried both phantomjs and phantonjs.exe). Whenever driver = webdriver.PhantomJS() is used this occurs...
Error:
raise WebDriverException("Unable to start phantomjs with ghostdriver.", e)
WebDriverException: Message: Unable to start phantomjs with ghostdriver.
Screenshot: available via screen
Tried every solution I could find so far: reverting to version 2.37.0, trying phantomjs vs phantomjs.exe in path, altering the service.py file, installing it through node.js, rebooting after adding it to path, and using executable_path=.... within the PhantomJS() parameter. Any additional ideas?
PhantomJS recently dropped Python support altogether. However, PhantomJS now embeds Ghost Driver.
A new project has since stepped up to fill the void: Ghost.py (http://jeanphix.me/Ghost.py/). You probably want to use that instead:
from ghost import Ghost
ghost = Ghost()
page, extra_resources = ghost.open("http://jeanphi.fr")
assert page.http_status==200 and 'jeanphix' in ghost.content

Selenium include mozilla instance

I have a server on which I want to build a script to login to page which is using javascript. I want to use python selenium to achieve the same.
We have a shared drive which contains all the installed binaries and the same has to be included. So when running a python program I won't be using my #!/usr/bin/python instead efs/path../python, similarly all the packages are to be included in this ways. sys.path.append("/efs/path.../selenium-egg-info"). This works good, but as selenium would need firefox included, I could see mozilla in the path, but where are it's binary, exactly which folder to include inside mozilla.
You can think Selenium as launching 'firefox' behind the scenes. You won't see it but it's there and then accordingly opening up the webpage and manipulating things.
How do you think it does all that cool stuff without writing explicit url header etc. So for that you need to have a firefox installed with a physical display(monitor) installed.
You can fake a pyhsical terminal it's just input/output but you AFAIK you need to have a firefox installed. Sad news but that's the way it is.
You don't need firefox executable since it comes with the Selenium
Firefox driver is included in the selenium-server-stanalone.jar available in the downloads. The driver comes in the form of an xpi (firefox extension) which is added to the firefox profile when you start a new instance of FirefoxDriver.
See my another answer here

Categories