Quick learner
Quick learner

Reputation: 709

How to browse and upload file through selenium Web driver

I am trying to upload a file through selenium webDriver but selenium gives an error:Unable to locate element. I have used all method to find element and click on browse but not any click occurring on that.

The HTML is <input type="file" onmousedown="this.blur();" onclick="//this.blur();" onchange="$('file-loader').show(); this.form.submit();" name="metadata_item" id="metadata_item">

Upvotes: 0

Views: 9094

Answers (3)

Ajay
Ajay

Reputation: 395

cant answer without HTML code. in order to click Browse button place the pointer in previous field i.e, email address field and use

    Robot robot=new Robot();
    robot.keyPress(KeyEvent.VK_TAB);
    robot.keyRelease(KeyEvent.VK_TAB);
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);

and then a system popup displays then use AutoIT tool to handle it

Upvotes: 0

user3081552
user3081552

Reputation: 1

It could be under another frame. Try switching to that frame and click the element. It would work.

For Example - If the browse button is under another frame which has Id = "frameUpload", then switch the webdriver to that frame like this:

driver.switchTo().frame("frameUpload");

Now click on the browse button like this:

driver.findElement(By.Id("Id of the button")).click();

once you are done with the click, you can always get back to the default window like this

driver.switchTo().DefaultContent();

Upvotes: 0

Sergio Cazzolato
Sergio Cazzolato

Reputation: 462

First check the input element is visible

Then, you don't have to click on the browse button, it will trigger an OS level dialogue box and effectively stop your test dead.

In order to deal with this follow this code:

driver.findElement(By.id("myElementId")).sendKeys("<pathToFile>");

myElementId is the id of that element (button in this case) and in sendKeys you have to specify the absolute path of the content you want to upload. The Webdriver will do the rest for you.

Keep in mind that the upload will work only If the element you send a file should be in the form

Upvotes: 3

Related Questions