通過設置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()
————————————————
版權聲明:本文為CSDN博主「Chris~Ma」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/Marry_Ma/article/details/96424482