简体   繁体   中英

Uploading a file using Selenium Webdriver and Java

I'm trying to upload a file using Selenium Webdriver and Java. I use sendKeys method on WebElement and this not working for me. I tried a lot of solution for example from this File Upload using Selenium WebDriver and Java Robot Class and also nothing.

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. Something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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