首页 > 解决方案 > 如何使用 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

有人对我如何将信息动态地拼凑到那些允许我username在该命令字符串中进行工作的 webdriver 目录有任何想法吗?

例子:

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

其中用户名将字符串作为echo $USERprint(username)

标签: pythonseleniumautomationpython-os

解决方案


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

推荐阅读