Selenium Tutorial 08: File Upload + Download Sample Exercise

1. Upload the Li Bai.txt file. The send_keys method is used here instead of the click operation. Because after using the click operation, there is no way to operate the .exe program in Selenium. It can only automate the operation of the web page.
Insert image description here

# @Author : 小红牛
# 微信公众号:WdPython
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://sahitest.com/demo/php/fileUpload.htm')
Upload_element = driver.find_element(By.ID, 'file')
# 1.上传李白.txt文件的路径
Upload_element.send_keys(r'D:/Wdpython/爬虫/李白.txt')
sleep(2)

# 2.查看上传文件的详情
driver.find_element(By.NAME, 'submit').click()
sleep(5)
driver.quit()

2. Chrome browser file download example: Set the download.default_directory in its options to indicate the download path of the set file.
Insert image description here

from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()
# prefs指定下载文件的存放路径
prefs = {
    
    'download.default_directory': 'D:\Wdpython\爬虫\pic'}
options.add_experimental_option('prefs', prefs)
# 1.添加options路径配置
driver = webdriver.Chrome(options=options)
driver.maximize_window()
driver.get('https://sahitest.com/demo/saveAs.htm')
# 2.点击下载
driver.find_element(By.LINK_TEXT, 'testsaveas.zip').click()
sleep(5)
driver.quit()

complete! ! thanks for watching

----------★★Historical blog post collection★★----------
My zero-based Python tutorial, Python introduction to advanced video tutorial Py installation py project Python module Python crawler Json
Insert image description here

Guess you like

Origin blog.csdn.net/gxz888/article/details/135452423