python - 如何使用 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 $USER
或print(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')
推荐阅读
- java - 在 Api 请求使用 Query 返回数据时显示加载图像
- rtai - RTAI:编写数据结构的模块内核
- azure-devops - TFS build vNext 允许在队列时间更改不可设置的变量
- javascript - 在 javascript 和 angularjs 中将键对象与字符串值进行比较
- android - 我们如何将上下文转换为片段?
- jmeter - 未创建 JMeter 报告文件夹:“预期列数不匹配”
- python - 如何使用 Python + Selenium 在 JavaScript 中获取 href 值
- sql-server - SQL Server 自动索引调整功能是否会删除唯一索引?
- tabs - 你能解释一下使用制表符和空格的区别吗?
- php - PHP 连接到远程 MSSQL 但得到错误列表