python - Selenium + Python + Chrome:同时添加_experimental_option和设置desired_capabilities
问题描述
我正在尝试更改 Chrome 驱动程序的设置,以便它允许我执行以下两项操作:
尽管这两种解决方案在孤立的情况下都非常有效,但我尝试将它们结合起来却失败了。下面是两个孤立的部分解决方案。感谢这里的任何帮助。
代码 1:
### This version save pdf automatically but has automation popup.
from selenium import webdriver
import time
timestr = time.strftime("%Y%m")
options = webdriver.ChromeOptions()
prefs = {
"download.default_directory": r"C:\temp\\"+timestr,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True
}
options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(executable_path="C://temp//chromedriver.exe",options=options)
driver.get("https://www.tutorialspoint.com/selenium/selenium_tutorial.pdf")
代码 2:
### This version has no automation popup but doesn't save pdf automatically.
from selenium import webdriver
import time
timestr = time.strftime("%Y%m")
capabilities = {
'browserName': 'chrome',
'chromeOptions': {
'useAutomationExtension': False,
'forceDevToolsScreenshot': True,
'args': ['--start-maximized', '--disable-infobars']
}
}
driver = webdriver.Chrome(executable_path="C://temp//chromedriver.exe",desired_capabilities=capabilities)
driver.get("https://www.tutorialspoint.com/selenium/selenium_tutorial.pdf")
解决方案
desired_capabilities
您可以将选项转换为所需的功能,并在创建驱动程序期间将其传递给参数:
capabilities.update(options.to_capabilities())
希望对你有帮助!
推荐阅读
- c# - Android 全球不支持 PushAsync,请使用 NavigationPage
- r - 如何在r中的新列内的分号分隔符内将几列合并为单列
- rxjs - Angular 6:operators.share 未定义
- python - 如何根据可变数量的参数创建不同的类类型
- jquery - 当Jquery更改文本输入时,Vuejs如何强制v-model更新?
- python - Pandas:两个数据帧之间的精确字符串匹配,带有位置
- c# - 如何在 MacOS 上使用 DataObject(拖放文件)
- javascript - Web2py - 如何在ajax之后确定负载控制的结束
- php - 如何将错误消息与注册表单中的表单字段分开?
- f# - List.unfold/Array.unfold 导致内存爆裂