第七:selenium文件上传和键盘操作

发布时间:2026/6/11 21:25:56
第七:selenium文件上传和键盘操作 一.文件上传1.文件上传input1.1.当页面标签为input时使用send_keys向其输入一个文件地址来实现上传 selenium只支持input元素的上传直接使用send_keys将文件绝对地址写入元素即可 ​importtimefromseleniumimportwebdriver ​withwebdriver.Chrome()asdirver:dirver.get(http://www.baidu.com)dirver.find_element_by_xpath(//span[classsoutu-btn]).click()time.sleep(2)inputdirver.find_element_by_xpath(//input[classupload-pic])input.send_keys(rC:\Users\Administrator\Desktop\123.png)time.sleep(5)2.文件上传pywinauto2.1.很多时候页面不是使用input来进行文件上传则需要使用第三方包来操作系统界面2.2.pywinauto优点只能在windows使用 缺点能选择多个文件可使用中文路径2.3.安装pip install pywinauto2.4.使用importtime ​fromseleniumimportwebdriverfrompywinauto.keyboardimportsend_keys ​withwebdriver.Chrome()asdriver:driver.get(http://www.baidu.com)spandriver.find_element_by_xpath(//span[classsoutu-btn])span.click()time.sleep(1)​# 打开选择文件窗口select_spandriver.find_element_by_class_name(upload-wrap)select_span.click()time.sleep(2)​# 选择文件send_keys(rC:\Users\Administrator\Desktop\123.png)time.sleep(1)nb学习地址https://blog.csdn.net/m0_65657661/article/details/129682947