如何使用 Python 动态调用文件目录中的用户名

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

所以我试图在 python 命令中调用用户名来实例化 Selenium 驱动程序。

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

但是,$USER 将不起作用。我试过称它为

username

和:

print(username)

从目录中如下

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

然而,它似乎没有用,它只是给了我一个跟踪回溯,当我 运行 它表明它没有被识别时。

 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/python@3.8/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/python@3.8/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

有没有人想过如何将信息动态地插入到那些 webdriver 目录中,以便让 username 在该命令字符串中工作?

示例:

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

其中用户名给出的字符串为 echo $USERprint(username)?

您可以使用 os.environ 字典在 python 中获取系统环境值。

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

编辑:您可以使用另一种方法 os.path.expandvars 通过系统变量完成 PATH。

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