1

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?

2
  • 1
    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. You can make the script download the chromedriver if cannot find it on the path. Commented Aug 21, 2019 at 3:12
  • 1
    ^ i think this would be a better solution imo. Commented Aug 21, 2019 at 3:19

4 Answers 4

1

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}")
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help!
You're very welcome. Check version2, less code for the same.
1

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

0

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

3 Comments

yes, but I have chromedriver coming installed with the package so is there any way I can locate the file wherever the user decides to download the folder
Also is there anyway the user could input the directory once the first time the python script is used and then that input would be saved as the default
I think it's messy, it's probably better to have the script download the chrome driver and save it to an area to use as Pedro's comment above.
0

First You Need To import os

import os
os.path.join(os.path.expandvars("%userprofile%"),"Downloads\Chromedriver.exe")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.