当前位置:首页 > python > 正文内容

用python写个selenium上传文件的程序

zhangsir3年前 (2023-03-24)python329

当您使用 Selenium 来自动化测试时,上传文件是一个非常常见的需求。下面是一个使用 Python 和 Selenium WebDriver 来上传文件的示例代码:


from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 创建 WebDriver 对象并打开浏览器
driver = webdriver.Chrome()
# 打开目标网站
driver.get("https://example.com/upload")
# 等待上传按钮出现并点击
upload_button = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.ID, "upload_button"))
)
upload_button.click()
# 找到文件输入框并输入文件路径
file_input = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.NAME, "file"))
)
file_input.send_keys("/path/to/your/file")
# 提交表单
submit_button = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.ID, "submit_button"))
)
submit_button.click()
# 关闭浏览器
driver.quit()

请注意,这只是一个简单的示例,您需要根据您的实际情况对代码进行适当修改。您需要将示例代码中的网站地址、上传按钮 ID、文件输入框名称、提交按钮 ID 和文件路径更改为您要使用的实际值


zhangsir版权f2防采集https://mianka.xyz

扫描二维码推送至手机访问。

版权声明:本文由zhangsir or zhangmaam发布,如需转载请注明出处。

本文链接:https://www.mianka.xyz/post/143.html

分享给朋友:

“用python写个selenium上传文件的程序” 的相关文章

Python三方库ddddocr实现验证码识别

Python三方库ddddocr实现验证码识别环境要求python >= 3.8安装三方库pip install ddddocr -i https://pypi.tuna.tsinghua.edu.cn/simple参数说明:参数名参数类型默认值说明us...

python 爬虫 报错:UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0x8b in position”解决方案

发现报错“UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1:invalid start byte”,方法一:根据报错提示,错误原因有一条是这样的:“'Accept-Encodi...

python 将json数据转成csv文件

从JSON数据转化CSV文件下面的这个Python脚本能够将JSON数据转化到CSV文件的表格当中去,我们输入的是带有.json后缀的文件,输出的是.csv后缀的表格文件,代码如下import json def converter(input_file, output...

python—pymysql的增删改查操作实例展示

Python使用pymysql连接数据库1.导包import pymysql2.连接数据库connection = pymysql.connect(     host='',  # ...

Linux系统下使用Python+selenium+谷歌浏览器下载文件

from seleniumwire import webdriver import time ch_options = webdriver.ChromeOptions() ch_options.add_argument("-...

怎么用python连接websocket

要使用 Python 连接 WebSocket,可以使用 websocket 模块或 websocket-client 库。以下是使用 websocket-client 库连接 WebSocket 的基本步骤:安装 websocket-client 库。可以使用 pip 进行安装:pip ...