python - Can't deal with path
问题描述
I need to load the profile in Chrome using Python Selenium Webdriver. Everyting works fine when I use the direct path like this:
options.add_argument("user-data-dir=C:\path_to_folder_with_myprogram_user_data_folder_is_aready_there\User Data")
But I plan to use my code on several different PC, so my program should detect the path. I need to detect and insert path to this part of code:
options.add_argument("user-data-dir=C:\\Path") #Path to my user data folder which is in my program folder
I have a problem because I don't know how to get the path from the variable and insert it in 'user-data-dir= '. The variable "user_data_dir" contains the correct path, but when I'm trying to use it, the quotes ruins everything. Without the quotes it works neither.
options = Options()
user_data_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'User Data')
print (user_data_dir)
options.add_argument(r'user-data-dir=user_data_dir')
driver = webdriver.Chrome(executable_path=os.path.join(retval, 'webdrivers\chromedriver.exe'), options=options)
解决方案
尝试这个,
options = Options()
user_data_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'User Data')
print (user_data_dir)
options.add_argument(r'user-data-dir='+user_data_dir)
driver = webdriver.Chrome(executable_path=os.path.join(retval, 'webdrivers\chromedriver.exe'), options=options)
推荐阅读
- python - 在 Tensorflow 2.5 中训练 GAN 时 GPU 利用率不稳定
- node.js - 找不到模块“bson”的声明文件
- gradle - 在 gradle 多模块项目中将插件/runtimeOnly 配置从一个模块注入另一个模块(在同一项目中)
- docker - Laravel Docker - Composer 无法创建供应商文件夹
- utf-8 - 将 unicode 字符替换为一列
- javascript - Reactjs:删除具有多个类名的div标签的类名之间的空格
- laravel-8 - 如何在 laravel 8 中调度事件?
- json - 是否可以创建 vs-code 扩展来根据文件的内容更改文件的图标?
- flatbuffers - flatbuffers 中的引用究竟是如何工作的?
- hangouts-chat - 传入的 webhook 消息从移动应用程序中的“未知用户”出现,在 web 界面中正确的 webhook 名称