Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python动态调用文件目录中的用户名_Python_Selenium_Automation_Python Os - Fatal编程技术网

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

如何使用Python动态调用文件目录中的用户名,python,selenium,automation,python-os,Python,Selenium,Automation,Python Os,因此,我尝试在python命令中调用用户名来实例化Selenium驱动程序 #系统的Web驱动程序路径 如果platform.system()==('Windows'): browser=webdriver.Chrome(“C:\ProgramFiles(x86)\Google\Chrome\chromedriver.exe”,options=Chrome\u options) elif platform.system()==('Linux'): browser=webdriver.Chrome

因此,我尝试在python命令中调用用户名来实例化Selenium驱动程序

#系统的Web驱动程序路径
如果platform.system()==('Windows'):
browser=webdriver.Chrome(“C:\ProgramFiles(x86)\Google\Chrome\chromedriver.exe”,options=Chrome\u options)
elif platform.system()==('Linux'):
browser=webdriver.Chrome(可执行文件路径='/home/$USER/Drivers/Google/Chrome/chromedriver',options=Chrome\u options)
elif platform.system()==('Darwin'):
browser=webdriver.Chrome(可执行文件路径='/Users/$USER/Drivers/Google/Chrome/chromedriver',options=Chrome\u options)
其他:
打印(“您确定在浏览器的正确路径中安装了Selenium Webdriver吗?”)
但是,
$USER
将不起作用。我试着把它叫做

用户名
以及:

打印(用户名)
从目录中,如下所示

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-专业的 ~/项目/米兰提斯/火车 python scrape_creds.py ✔  3812 14:36:47
巴雷特
文件secrets.gpg不存在,并且secrets文件未使用gpg加密
未对secrets.json强制执行GPG加密
您当前的Chrome版本是:
86.0.4240.80
使用目标URL:
https://mirantis.awsapps.com/start/#/
回溯(最近一次呼叫最后一次):
文件“/usr/local/lib/python3.8/site packages/selenium/webdriver/common/service.py”,第72行,开始
self.process=subprocess.Popen(cmd,env=self.env,
文件“/usr/local/cillar”/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py”,第854行,在__
self.\u execute\u child(参数、可执行文件、预执行文件、关闭文件、,
文件“/usr/local/cillar”/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py”,第1702行,在执行子进程中
引发子项异常类型(错误号、错误消息、错误文件名)
FileNotFoundError:[Errno 2]没有这样的文件或目录:'/Users/$USER/Drivers/Google/Chrome/chromedriver'
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“scrape_creds.py”,第202行,在
browser=webdriver.Chrome(可执行文件路径='/Users/$USER/Drivers/Google/Chrome/chromedriver',options=Chrome\u options)
文件“/usr/local/lib/python3.8/site packages/selenium/webdriver/chrome/webdriver.py”,第73行,在__
self.service.start()
文件“/usr/local/lib/python3.8/site packages/selenium/webdriver/common/service.py”,第81行,开始
引发WebDriverException(
selenium.common.exceptions.WebDriverException:消息:“chromedriver”可执行文件需要位于路径中。请参阅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 = os.path.expandvars('/Users/$USER/Drivers/Google/Chrome/chromedriver')

您需要类似于
browser=webdriver.Chrome(可执行文件路径=f'/home/{username |/Drivers/Google/Chrome/chromedriver',options=Chrome\u options)
browser=webdriver.Chrome(可执行文件路径='/home/%s/Drivers/Google/Chrome/chromedriver'%username,options=Chrome\u options)
@JoranBeasley不错。谢谢你让我知道谢谢@JoranBeasley
os.path.expandvars
为我工作。