How to Dynamically Call a Username in a File Directory with Python

607 views Asked by At

So I am trying to call a username inside of a python command to instantiate the Selenium Driver.

# WebDriver Path for System
if platform.system() == ('Windows'):
    browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\chromedriver.exe",options=chrome_options)
elif platform.system() == ('Linux'):
    browser = webdriver.Chrome(executable_path='/home/$USER/Drivers/Google/Chrome/chromedriver',options=chrome_options)
elif platform.system() == ('Darwin'):
    browser = webdriver.Chrome(executable_path='/Users/$USER/Drivers/Google/Chrome/chromedriver',options=chrome_options)
else:
    print("Are you sure you have the Selenium Webdriver installed in the correct path for your Browser?")

However, the $USER will not work. I have tried calling it as

username

AND:

print(username)

from within the directory as follows

username = getpass.getuser()
print(username)

# WebDriver Path for System
if platform.system() == ('Windows'):
    browser = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\chromedriver.exe",options=chrome_options)
elif platform.system() == ('Linux'):
    browser = webdriver.Chrome(executable_path='/home/username/Drivers/Google/Chrome/chromedriver',options=chrome_options)
elif platform.system() == ('Darwin'):
    browser = webdriver.Chrome(executable_path='/Users/username/Drivers/Google/Chrome/chromedriver',options=chrome_options)
else:
    print("Are you sure you have the Selenium Webdriver installed in the correct path for your Browser?")

However, it doesn't seem to work and it just gives me a following traceback, when I run it indicating that it's not recognized.

 rbarrett@MacBook-Pro  ~/Projects/Mirantis/Train  python scrape_creds.py                       ✔  3812  14:36:47
rbarrett
The file secrets.gpg does not exist and the secrets file is not using GPG Encryption
GPG Encryption is not Enforced on secrets.json
Your Current Version of the Chrome is:
86.0.4240.80
Using Target URL:
https://mirantis.awsapps.com/start/#/
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 72, in start
    self.process = subprocess.Popen(cmd, env=self.env,
  File "/usr/local/Cellar/[email protected]/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/local/Cellar/[email protected]/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 1702, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/$USER/Drivers/Google/Chrome/chromedriver'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "scrape_creds.py", line 202, in <module>
    browser = webdriver.Chrome(executable_path='/Users/$USER/Drivers/Google/Chrome/chromedriver',options=chrome_options)
  File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/usr/local/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 81, in start
    raise WebDriverException(
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

Does anyone have any thoughts on how I can dynamically piece in information into those webdriver directories that will allow me to have the username work inside that command string?

Example:

/Users/username/Drivers/Google/Chrome/chromedriver'

where username gives the string as echo $USER or print(username)?

1

There are 1 answers

2
PaxPrz On BEST ANSWER

You can get System environment value in python using os.environ dictionary.

import os
USER = os.environ.get('USER')
if USER:
    PATH = f'/Users/{USER}/Drivers/Google/Chrome/chromedriver'

Edit: Another way you can use os.path.expandvars to complete PATH with system variables.

PATH = os.path.expandvars('/Users/$USER/Drivers/Google/Chrome/chromedriver')