WebUI文件拖拽上传与下载文件

# pip install webdriver-helper

# 文件拖拽上传
from selenium.webdriver.common.by import By
from webdriver_helper import debugger,get_webdriver

#get_webdriver()后,不使用with也会自动关闭浏览器
#with get_webdriver() as driver:
#    driver.get("https://www.baidu.com")

#使用get_webdriver会在程序执行完之后自动关闭浏览器
driver = get_webdriver()
driver.get("https://www.baidu.com")  #被测试页面
file =r"D:\picture\test.png"   #本地图片的绝对路径
ele = driver.find_element(By.XPATH,'[//*[id="upload_ele"]]')  #拖拽到这个元素
debugger(driver)  #检查报错的原因
ele.upload_by_drop(file)   #拖拽文件

driver.quit()



#文件下载

from selenium.webdriver.common.by import By
from webdriver_helper import debugger,get_webdriver

download_path =r"D:\download"   #本地下载绝对路径

driver = get_webdriver()
driver.set_download_path(download_path)

driver.get("https://api.tttt.one/httpbin/stream0-bytes/200")  #下载文件rul


driver.quit()

with open(download_path+r"200","rb") as f:
    assert  len(f.read()) ==200

你可能感兴趣的:(Web自动化,python,java)