I was building a web testing application using python selenium and want people to be able to use it, who are not too tech savvy. However, this application requires the chromedriver.exe file to be able to get the webpage. So is there some way I can always access that file no matter where it is downloaded and kept. Or maybe is there someway to have the user input the location once and then have it saved so the user doesn't need to input put it every time the application is launched?
-
1There's absolutely no way for the script to find the chromedriver "*no matter where it is downloaded and kept" unless it's on the system path. You can make the script download the chromedriver if cannot find it on the path.Pedro Lobito– Pedro Lobito2019-08-21 03:12:16 +00:00Commented Aug 21, 2019 at 3:12
-
1^ i think this would be a better solution imo.Umar.H– Umar.H2019-08-21 03:19:54 +00:00Commented Aug 21, 2019 at 3:19
4 Answers
There's absolutely no way for the script to find the chromedriver "no matter where it is downloaded and kept" unless it's on the system path.
I quickly coded this for python3, It checks if chromedriver exists on windows path, if not, it downloads it from an url.
import requests, zipfile, io, os
curr_dir = os.path.dirname(os.path.abspath(__file__))
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
def chromedriver_exist():
if is_exe (curr_dir + "\chromedriver.exe"):
return curr_dir + "\chromedriver.exe"
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, "chromedriver.exe")
if is_exe(exe_file):
print("chromedriver exist and is executable", exe_file)
return exe_file
chromedriver = chromedriver_exist()
if chromedriver:
print(chromedriver)
else:
url = "https://chromedriver.storage.googleapis.com/72.0.3626.69/chromedriver_win32.zip"
r = requests.get(url)
z = zipfile.ZipFile(io.BytesIO(r.content))
z.extractall()
chromedriver = curr_dir + "\chromedriver.exe"
print(chromedriver)
V2
import requests, zipfile, io, os, subprocess
curr_dir = os.path.dirname(os.path.abspath(__file__))
chromedriver = "chromedriver.exe"
out = subprocess.getoutput(f"{chromedriver} -v")
if "ChromeDriver" in out:
print(f"{out} \nChromeDriver exists in path and is executable" )
else:
url = "https://chromedriver.storage.googleapis.com/72.0.3626.69/chromedriver_win32.zip"
try:
r = requests.get(url)
z = zipfile.ZipFile(io.BytesIO(r.content))
z.extractall()
chromedriver = f"{curr_dir}\chromedriver.exe"
except Exception as e:
print(f"Cannot download chromedriver\n {e}")
2 Comments
There is an even better solution for that. You can use the WebDriverManager. It will download and install the latest version of the driver you need. This means you don't need to take care where the file is located and you will not run in problems if a browser gets updated.
Found another one here.
Unfortunately it seems that you need to specify a version if you don't want the latest one. Not like in Java where it automatically downloads the correct version.
Comments
If I understand correctly, you want the script to locate the chrome driver on the local user's environment? Assuming you are using Windows.
os.path.join(os.path.expandvars("%userprofile%"),"Downloads\Chromedriver.exe")