使用Selenium ChromeDriver中的ChromeOptions和通过Python的Chrome,“选项”对象没有属性“ set_preference”错误

时间:2019-06-12 09:07:21

标签: python selenium google-chrome selenium-webdriver selenium-chromedriver

我正在尝试使用selenium和python单击“下载”按钮来下载pdf。但是在运行Chrome和我为chrome浏览器开发的完整测试时遇到了上述错误。

driver.find_element_by_xpath("/html/body/app-root/app-full-layout/div/div[2]/div/div/div/app-list/section/div[2]/div/div[2]/div[2]/div/div[2]/button").click()
time.sleep(7)
options = Options()
options.set_preference("browser.download.folderList", 0)
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.download.dir", "/DFS")
options.set_preference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream,application/pdf")
driver = driver.Chrome(chrome_options=self.options)

预期执行此代码并下载pdf文件

1 个答案:

答案 0 :(得分:1)

selenium.webdriver.chrome.options 不包含方法set_preference()

set_preference(name, value)来自selenium.webdriver.firefox.options Class

因此,在为 ChromeDriver Chrome 使用 ChromeOptions 时,您需要使用add_argument(argument)方法,如下所示:

options = webdriver.ChromeOptions()
options.add_argument("--safebrowsing-disable-download-protection")
options.add_argument("safebrowsing-disable-extension-blacklist")
driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
  

您可以在How to download XML files avoiding the popup This type of file may harm your computer through ChromeDriver and Chrome using Selenium in Python

中找到相关的讨论