0

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">

Seanny123
9,43617 gold badges74 silver badges136 bronze badges
asked Nov 26, 2013 at 6:42
2
  • <input type="file" onmousedown="this.blur();" onclick="//this.blur();" onchange="$('file-loader').show(); this.form.submit();" name="metadata_item" id="metadata_item"> Commented Nov 26, 2013 at 7:51
  • Code tried:Actions ac=new Actions(driver); WebElement fileinput=driver.findElement(By.name("metadata_item")); ac.click(fileinput).perform(); Commented Nov 26, 2013 at 7:52

3 Answers 3

3

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

answered Nov 26, 2013 at 22:22

Comments

0

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

Seanny123
9,43617 gold badges74 silver badges136 bronze badges
answered Nov 26, 2013 at 7:51

1 Comment

This won't work if you decide to run tests remotely using Selenium Hub.
0

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();
opalenzuela
3,16923 silver badges42 bronze badges
answered Jan 15, 2014 at 10:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.