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

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

zhangsir3年前 (2023-03-24)python324

当您使用 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版权c3防采集https://mianka.xyz

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

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

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

分享给朋友:

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

如何用python获取一个网页的所有连接

如何用python获取一个网页的所有连接很简单直接上代码:# -*- coding: utf-8 -*- ''' 如何用python获取一个网页的所有连接 author:zhangsir ''' imp...

python selenium find_element_by_xpath 方法已经被弃用的解决办法

背景:在使用最新3.10.4Python版本时候,用selenium进行xpath定位元素,编译器提示:DeprecationWarning:find_element_by_xpath is deprecated. Please use find_element(by=By.XPATH, value...

python 给电脑设置闹钟

python会自动触发windows桌面通知,提示重要事项,比如说:您已工作两小时,该休息了我们可以设定固定时间提示,比如隔10分钟、1小时等用到的第三方库:win10toast - 用于发送桌面通知的工具from win10toast import ToastNoti...

python 将json数据转成csv文件

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

计算机学习视频教程

人工智能机器学习:Python&R实践课程介绍:https://www.aihorizon.cn/1百度网盘地址: https://pan.baidu.com/s/1a743NTKFRjsgexMTagWooA?pwd=e39j动手使用Python进行自然语言处理(NLP)课程介绍:http...

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

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