015、文件上传操作 (input标签文件上传)
作者:互联网
一、文件上传操作
1、input标签文件上传
send_keys("D:\\wangyiyun\\web\\yuyu.png") ,需要传 绝对路径,传相对路径会报错:path is not absolute
目录结构如下:
示例代码如下:
# -*- coding:utf-8 -*- # Author: Sky # Email: 2780619724@qq.com # Time: 2021/8/18 1:06 # Project: day01 # Module: study_16.py # Environment: Python3.8.6 , Selenium3 环境 ( 3.141.0 版本) # Environment: Chrome ( 92.0.4515.131, 正式版本) + chromedriver(92.0.4515.107版本) from selenium import webdriver import time import os from selenium.webdriver import ActionChains driver = webdriver.Chrome() driver.maximize_window() driver.get("http://49.235.92.12:8200/users/login/") # 先登陆 time.sleep(3) driver.find_element_by_id("username").send_keys("112233456@qq.com") driver.find_element_by_id("password_l").send_keys("123456") driver.find_element_by_id("jsLoginBtn").click() # 鼠标悬停 time.sleep(3) mouse = driver.find_element_by_class_name("personal") ActionChains(driver).move_to_element(mouse).perform() # 进入个人中心 time.sleep(3) driver.find_element_by_class_name("personcenter").click() # 修改头像,需要用绝对路径,相对路径否则会报错:path is not absolute: yuyu.png # 方法1: 绝对路径 time.sleep(3) driver.find_element_by_id("avatarUp").send_keys(r"D:\SkyWorkSpace\WorkSpace\Web_AutoTest\Temp\day01\test\yuyu.png") # 方法2: 路径os获取 time.sleep(3) current_dir = os.path.dirname(os.path.realpath(__file__)) print(current_dir) file_path = os.path.join(current_dir, "yoyo.png") print("获取到文件的完整路径:%s" % file_path) driver.find_element_by_id("avatarUp").send_keys(file_path) time.sleep(3) driver.quit()
2、非input标签的文件上传 解决办法:需借助第三方工具,如:autoit
标签:文件,driver,element,sleep,015,time,path,上传,find 来源: https://www.cnblogs.com/qq-2780619724/p/15156030.html