使用selenium上传文件 [英] Upload files using selenium

查看:122
本文介绍了使用selenium上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 selenium webdriver从本地窗口提示上传文件?

How to upload files from local via window prompt using selenium webdriver?

我想执行以下操作:


  1. 点击窗口上的浏览选项

  2. 从窗口提示转到当地的特定位置保存文件的位置

  3. 选择文件并单击打开以上传文件。


推荐答案

我使用以下三种不同的方式在selenium webdriver中上传文件。

I have used below three different ways to upload a file in selenium webdriver.


  1. 第一个简单的例子就是找到元素并在其中键入文档的绝对路径。但我们需要确保HTML字段是输入类型。 例如:< input type =filename =uploadsubmit>

  1. First simple case of just finding the element and typing the absolute path of the document into it. But we need to make sure the HTML field is of input type. Ex:<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);




  1. 第二种情况是使用机器人上传用于(生成本机系统输入事件)的类控制鼠标和键盘。

  1. 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上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆