python3.6+selenium使用chrome浏览器自动将文件下载到指定路径

通过设置Chrome属性,实现脚本自动下载网页上的文件,并保存到指定路径下,以下是实例完成代码,请参考:
import unittest
from selenium import webdriver
import time

class MyTestCase(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Chrome(executable_path=’/python/driver/chromedriver’)
#创建Chrome浏览器配置对象
chromeOptions=webdriver.ChromeOptions()
#设定下载文件的保存目录为D盘的D:\downloadFile
#如果该目录不存在则直接创建
prefs={“download.default_directory”:“D:\downloadFile”}
#将自定义设置添加到chrome配置对象实例中
chromeOptions.add_experimental_option(“prefs”,prefs)
#启动带有自定义设置的chrome浏览器
self.driver=webdriver.Chrome(executable_path=’/python/driver/chromedriver’,chrome_options=chromeOptions)
def test_downloadFile(self):
url=“http://pypi.python.org/pypi/selenium
self.driver.get(url)
time.sleep(5)
#定位下载文件链接页面元素,并单击进行下载
self.driver.find_element_by_partial_link_text(“selenium-server-standalone-3.141.0.jar”).click()
time.sleep(100)

if name == ‘main’:
unittest.main()

    原文作者:Chris~Ma
    原文地址: https://blog.csdn.net/Marry_Ma/article/details/96424482
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞