web自动化(五)上传文件

admin2024-07-07  3

我们需要准备一个上传文件的html,创建a.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件上传示例</title>
</head>
<body>
 
<form action="/upload" method="post" enctype="multipart/form-data">
    <!-- 文件上传控件 -->
    <label for="fileUpload">选择文件:</label>
    <input type="file" id="fileUpload" name="file">
    
    <!-- 提交按钮 -->
    <input type="submit" value="上传">
</form>
 
</body>
</html>

selenuim

import time

from selenium import  webdriver
from selenium.webdriver.common.by import  By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import  expected_conditions as EC
web_driver=webdriver.Firefox()
web_driver.get("file:///C:/Users/Administrator/Desktop/a.html")

el=(By.ID,"fileUpload")

WebDriverWait(web_driver,10).until(EC.presence_of_element_located(el))

web_driver.find_element(*el).send_keys(r"C:\Users\Administrator\Desktop.jpg")

time.sleep(5)

web_driver.quit()

drissionpage

import time
from DrissionPage import  WebPage

web_page=WebPage("d")
web_page.get("file:///C:/Users/Administrator/Desktop/a.html")

el=(By.ID,"fileUpload")

web_page.wait.eles_loaded(el)
web_page.ele(el).click.to_upload(r"C:\Users\Administrator\Desktop.jpg")

time.sleep(5)

web_page.quit()
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明原文出处。如若内容造成侵权/违法违规/事实不符,请联系SD编程学习网:675289112@qq.com进行投诉反馈,一经查实,立即删除!