Can Python install files into PATH? - python

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.

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

'chromedriver' executable needs to be in PATH in Python Django

I am trying to execute a Python script in Django. My view file is:
def generate_traffic(request):
#other code
out = run([sys.executable, 'C://Users//Maisum Abbas//learning_users//trafficapp//traffic.py', "--domain", url, "--thread", requests, "--max-clicks", maximum, "--min-clicks", minimum])
If I run the script alone without running it in Django, it works fine. Now, the problem that I am facing is that the script is not able to detect chromedriver if I run it from my function generate_traffic in view.py. I don't know what seems to be the problem since the chromedriver is in the same directory as of my other files.
myFolder
-view.py
-traffic.py (the script I am executing)
-chromedriver.exe
-otherfiles
Kindly guide me of what I am doing wrong or what seems to be the problem here.
chromedriver.exe should be added to the PATH environment variable. Apparently, you are using Windows so here are the steps to add it in Windows 10 PATH:
Search for View Advanced System Settings in the start menu and open it.
Click on the Environment Variables button.
In the newly opened window, choose Path and click on Edit.
In the new window, click on New to add a new file/dir into PATH.
For your case it should be something like this:
Place your chromedriver.exe file in your Python Directory -> Scripts Directory, it will resolve the issue. Then just write it like this in your code:
driver = webdriver.Chrome(options=options)
or
driver = webdriver.Chrome()
depending on your use.

Permission denied: 'geckodriver.log' while running selenium webdriver in python

I've installed Firefox and Selenium on centos. I'm using Xvfb and pyvirtualdisplay to open the browser.
When I try to run selenium webdriver, I'm able to open a new display but as soon as I do
browser = webdriver.Firefox()
I get the error:
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 134, in __init__
self.service = Service(executable_path, log_path=log_path)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/firefox/service.py", line 45, in __init__
log_file = open(log_path, "a+")
IOError: [Errno 13] Permission denied: 'geckodriver.log'
Any clues on what's going wrong here?
EDIT : After overcoming the permission error, I'm getting
Message: 'geckodriver' executable needs to be in PATH
I had this same issue recently on a Windows 10 workstation. I fixed it by explicitly setting the service_log_path to a location I know I've got write access to:
browser = webdriver.Firefox(
service_log_path="C:\\Users\\[username]\\AppData\\Local\\Temp\\geckodriver.log"
)
Apparently this can come from an incompatibility between your firefox and your Selenium. Try pip install --upgrade selenium, and if the error is still there, try downloading a different version of Firefox, or of the gecko driver.
Regarding the message:
'geckodriver' executable needs to be in PATH
You could set the path of the driver on the script:
ff_profile_dir = "/usr/local/selenium/webdriver/firefox"
ff_profile = selenium.webdriver.FirefoxProfile(profile_directory=ff_profile_dir)
driver = selenium.webdriver.Firefox(ff_profile)
Or, according to this answer, you could run, on Unix systems, on a bash-compatible shell:
export PATH=$PATH:/path/to/directory/of/executable/downloaded/in/previous/step
On Windows you will need to update the Path system variable to add the
full directory path to the executable geckodriver manually or command
line(don't forget to restart your system after adding executable
geckodriver into system PATH to take effect). The principle is the
same as on Unix.
I was having the same problem. I tried using Ivan Chaer's answer, but it didn't work. I tried a whole bunch of other things until I finally came across a working solution. All the time I had been trying to run Selenium, I had been using the interactive shell. When I tried putting the code in a script and then running it instead, everything worked fine.
I then noticed that a file named "geckodriver.log" had been created in the same directory as the script. That gave me an idea. I created a "geckodriver.log" file in C:\Program Files\Python36 and then using Selenium with Firefox no longer threw any errors.
The following solution worked for me. In my case I was starting my Python script from within the Windows Notepad++ application. My geckodriver.exe was in the PATH and happened to be located in my C:\Python 27 folder. A full path to Firefox was also provided. Selenium, geckodriver and Firefox were all on the latest versions.
It turn out that the geckodriver.log file was being created here:
C:\Program Files (x86)\Notepad++\geckodriver.log
I found this by running Notepad++ as an Administrator. It was then able to create the file where it wanted. I then located the file and changed the file permissions to full access for the normal user account on Windows (it was set to be read only).
After doing this, I was able to run Notepad++ normally and the error was gone.
I had the same issue while working with python IDLE.
I just ran IDLE shell as Administrator.
Now on running the commands below works fine.
from selenium import webdriver
browser = webdriver.Firefox() #opens up a new Firefox window
I had this exact same error.
[Errno 13] Permission denied: 'geckodriver.log'
The problem was not at all with this .log file.
The real problem was that my script (.py file) and the geckodriver.exe were not located in the same folder.
Once I put them in the same folder, the problem was solved and the function was executed normally.
browser = webdriver.Firefox()
Hope this helps.
I had the exact same problem. I went on the selenium's pypi page. In the Drivers section they talk about geckodriver and you can find the link to the geckodriver executable that suits your browser and OS.
I just downloaded it, unzipped it and then placed the geckodriver.exe file in C:\Program Files\Python37 (Python37 is already in my PATH). And it now works fine.
in my case, geckodriver.log was created on the script folder, but by root user, from a previous execution, them running again as a normal user python didn't have access to file and fail
I was having the same problem trying to run an interactive shell exactly where my webdriver (I'm using Firefox's geckodriver) was and nothing happened, and what I came to the conclusion of is that the directory doesn't have writing privileges when you make it, therefore I set writing privileges using chmod (on Linux) and everything worked as intended. Be sure to check on privileges first to see if you can create the geckodriver.log file in the first place.
Had the same problem with Permission denied: 'geckodriver.log'
Tried a few of the options above, to no avail.
I was launching my script as a bat file from Windows scheduler. By default this runs in windows\system32...where you don't want to put the log and is a protected path.
Fixed it by updating the bat file to move to the my source code python folder
cd "C:\Users\[username]\mypy"
python.exe simpleSelenium.py
pause

How do I add the wires.exe directory to the system environment variables

I have a python program that uses the marionette driver for firefox and for it to work the directory of the driver must be added to PATH. I've packaged the driver with the program and I want the python script to automatically add the driver directory to the system PATH. Is it possible to do that in python?
Yes, it's possible :)
import sys
sys.path.insert(0, "C:/path/to/your/driver")
This adds to PATH only temporarily. If you want more permanent change, take a look at Permanently add a directory to PYTHONPATH

Selenium with GhostDriver in Python on Windows

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

Categories