使用Chrome浏览器自动将文件下载到指定路径

时间:2024-03-06 15:43:25
#!usr/bin/env python  
#-*- coding:utf-8 -*-  
#使用Chrome浏览器自动将文件下载到指定路径

from selenium import webdriver
import unittest,time

class TestDemo(unittest.TestCase):
    def setUp(self):
        chromeOptions = webdriver.ChromeOptions()
        prefs = {\'download.default_directory\':\'e:\\download\'}
        chromeOptions.add_experimental_option(\'prefs\',prefs)
        self.driver = webdriver.Chrome()

    def test_downloadFileByChrome(self):
        url = \'http://pypi.python.org/pypi/selenium\'
        self.driver.get(url)
        self.driver.find_element_by_partial_link_text\
            ("selenium-3.8.1-py2.py3-none-any.whl").click()
        time.sleep(100)

    def tearDown(self):
        self.driver.quit()

if __name__ == \'__main__\':
    unittest.main()