# 定位元素
element = driver.find_element()# 元素调用send_keys方法,传入参数为要上传文件的地址
element.send_keys()
【Example】
# 导包from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
# 定义一个浏览器对象
driver = webdriver.Chrome()# 定位到图片上传元素
element = driver.find_element(By.XPATH,'//*[@id="app"]/div/section/section/main/div/div[2]/div[1]/div/div/div[4]/div[1]/div/div[2]/form/div[9]/div/div[1]/div[1]/div/input')# 对定位到的元素调用send_keys()方法即可实现文件上传# 地址的最前面加上一个字母“r”,可以使得反斜杠\不起到转义功能
element.send_keys(r'C:\Users\Olivia\Pictures\300-300\22.gif')# 为方便看清楚上传效果,页面停留5秒钟
sleep(5)# 退出浏览器
driver.quit()
file download
picture download
Implementation steps
Open the page where the image exists
Locate the image element on the page
Use element.attribut('属性名')the method to get srcthe attribute value of the attribute, which is the storage address url of the image
Call reqests.get(url)the method to obtain image data (image data is binary data); if the obtained data is binary, it is required reqests.get(url).content; if the obtained data is a file type, it is requiredreqests.get(url).text
save the file locally
[Example]: Download the logo picture of Baidu homepage