问题描述
因此,我试图在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
AND:
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/[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,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'
其中username给出的字符串为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')