Upload files using selenium(使用 selenium 上传文件)
问题描述
如何使用 selenium webdriver 通过窗口提示从本地上传文件?
How to upload files from local via window prompt using selenium webdriver?
我想执行以下操作:
- 点击窗口上的浏览"选项
- 从窗口提示转到保存文件的本地特定位置
- 选择文件并点击打开"以上传文件.
推荐答案
我使用了以下三种不同的方式在 selenium webdriver 中上传文件.
I have used below three different ways to upload a file in selenium webdriver.
- 第一个简单的例子是找到元素并在其中输入文档的绝对路径.但我们需要确保 HTML 字段是输入类型.
例如:<input type="file" name="uploadsubmit">
下面是简单的代码:
WebElement element = driver.findElement(By.name("uploadsubmit"));
element.sendKeys("D:/file.txt");
driver.findElement(By.name("uploadSubmit"));
String validateText = driver.findElement(By.id("message")).getText();
Assert.assertEquals("File uploaded successfully", validateText);
第二种情况是使用Robot类上传,该类用于(生成本机系统输入事件)控制鼠标和键盘.
Second case is uploading using Robot class which is used to (generate native system input events) take the control of mouse and keyboard.
另一种选择是使用AutoIt"(开源工具).
The the other option is to use 'AutoIt' (open source tool).
您可以找到以上三个示例:- 使用 Selenium Webdriver 上传文件
You can find the above three examples : - File Uploads with Selenium Webdriver
这篇关于使用 selenium 上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 selenium 上传文件
基础教程推荐
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
