简体   繁体   English

使用 Selenium Webdriver 和 Java 上传文件

[英]Uploading a file using Selenium Webdriver and Java

I'm trying to upload a file using Selenium Webdriver and Java.我正在尝试使用 Selenium Webdriver 和 Java 上传文件。 I use sendKeys method on WebElement and this not working for me.我在 WebElement 上使用 sendKeys 方法,这对我不起作用。 I tried a lot of solution for example from this File Upload using Selenium WebDriver and Java Robot Class and also nothing.我尝试了很多解决方案,例如使用 Selenium WebDriver 和 Java Robot Class 从这个文件上传中尝试了很多解决方案。

Code with button:带按钮的代码:

<div class="button_to_upload" xpath="1">
    <span tabindex="0" class="upload" role="button">
        <input type="file" style="display: none;">
        <button class="any_class_button" type="button" style="min-width: 50px;">
            <span class="icon">
            </span><span style="">Upload something</span>
        </button>
    </span>
</div>

The easiest way to upload file using WebDriver is to set the text value of to file path.使用 WebDriver 上传文件的最简单方法是将文本值设置为文件路径。 Something like this:像这样的东西:

driver.findElement(By.ByXpath("//input[@type = 'file']")).sendKeys(filePath);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM