29.文件下载

Chrome浏览器设置其options:

  • download.default_directory:设置下载路径
  • profile.default_content_settings.popups:设置为 0 禁止弹出窗口

chromeoptions 是一个方便控制 chrome 启动时属性的类。通过 selenium 的源码,可以看到,chromeoptions 主要提供如下的功能:

  • 设置 chrome 二进制文件位置 (binary_location)
  • 添加启动参数 (add_argument)
  • 添加扩展应用 (add_extension, add_encoded_extension)
  • 添加实验性质的设置参数 (add_experimental_option)
  • 设置调试器地址 (debugger_address)

创建test.py输入一下代码

from selenium import webdriver
from time import sleep

options = webdriver.ChromeOptions()
prefs = {'profile.default_content_settings.popups': 0,'download.default_directory': 'd:\\'}
options.add_experimental_option('prefs',prefs)
dr = webdriver.Chrome(executable_path='C:\Program Files\Google\Chrome\Application\chromedriver',chrome_options=options)
dr.get('http://sahitest.com/demo/saveAs.htm')
dr.find_element_by_xpath('//a[text()="testsaveas.zip"]').click()

sleep(2)
dr.quit()

拓展:

https://www.jianshu.com/p/b03ef6ffc4a5

猜你喜欢

转载自www.cnblogs.com/luoshuifusheng/p/9186495.html
今日推荐