简体   繁体   中英

failed to upload a file using selenium webdriver in Java

I am trying to upload a file using selenium webd river but couldn't get this spinet code working

driver.findElement(By.id("uploadForm:j_id135")).sendKeys("path_to_file");
driver.findElement(By.name("uploadForm:j_id139")).click();

I also tried this suggestion: File Upload using Selenium WebDriver and Java too bad didn't work for me

Any ideas?much appreciated

You will have give the "id" of the actual input box (where the path is provided) when you manually click to upload a file (when working manually). In the Automation scenario we need to provide the correct id of that input box by using this command:

driver.findElement(By.id("upload")).sendKeys("/path/to/the/file");

next step is off course is click the upload button.

If you are using WebDriverBackedSelenium you can use:

selenium.type("locator", "/path/to/the/file");

selenium.click("upload_button");

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