0

this is my first post and sorry for asking the same question once again. I am stuck with a issue regarding uploading a file in Selenium Web Driver. I have searched a lot in this forum but the solutions are not working for me. The element which is the file Browse button is embedded in the file text area ( i.e. where the path of the file gets printed after the browsing through file browse dialog box), but the upload button is separate.

The entire element code is:

<input class="iceInpFileTxt" type="file" size="35" name="upload">

I am unable to click on the "browse" button using click() method. I have tried using Autoit/Robot also.

The code of the element from JSP page: <ice:inputFile id="fileUpload" width="600" autoUpload="true" value="#{practitionerLoadDataBean.inputFile}" actionListener="#{practitionerLoadControllerBean.browse}"/>

I know the input type is file so sendkeys() should work. The codes I have been trying are:

WebElement elem = driver.findElement(By.xpath("//input[@name='upload']")); elem.sendKeys("<PATH>");

The error message shows as: org.openqa.selenium.remote.ErrorHandler$UnknownServerException:Unable to locate element: {"method":"xpath","selector":"//input[@name='upload']"}

Please let me know where I my mistake is. Thanks in advance.

Avishek2585835
  • 91
  • 6
  • 16

4 Answers4

1

If the element was simply invisible, it would have been found, but you wouldn't be able to interact with it. The usual solution is to look around for frames.

You can't search for elements contained in frames, you have to switch your driver's context to that frame first.

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

Then you'll be able to find the element and upload the file the usual way (please, use the sendKeys() method describes by other answers in here).

Community
  • 1
  • 1
Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
0

Please confirm that the input element is visible

Do not click on the browse button, it will open an system level dialogue box to upload the file & Its very tedious to handle this in selenium.

you can use the following method:

driver.find_element(:id,'videoupload').send_keys("E:\\video.flv")

Please check the "\\" in your code.

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

Hope this will work for you.

Cheers!!

  • Thanks for the reply @ Sidram Waghmare.This is the entire form of the page:
    . Can you confirm if the element is hidden? If yes, Is there any workaround to automate this? TIA.
    – Avishek2585835 Jul 16 '13 at 06:22
  • Hi avishek, Yes your input element is hidden because in your code it is clearly showing that **input type="hidden"**. Above code will work only if your input element is visible & should NOT be hidden. One kindly suggestion that from next time please paste your code in the script. that will be very useful for us to more understanding.. :) – Sidram Waghmare Jul 18 '13 at 05:10
  • Thanks @Sidram, by the way is there any workaround to automate this? Please mention if you can give any pointer as I am stuck with the automation for this file uploading part. I talked to a developer who said the properties of an element are not set by them, its the framework which they are using sets it automatically. – Avishek2585835 Jul 18 '13 at 11:41
0

File Upload Using SendKeys

FirefoxDriver driver = new FirefoxDriver();

driver.get("URl");

File file=null;

try
{
file=new File("file path");
}

catch(Exception e)
{
e.printStackTrace();
}

Assert.assertTrue(file.exists());

WebElement browserButton=driver.findElement(By.id("button Id"));

browserButton.sendkeys(file.getAbsolutePath());
  • Thanks for the reply but the can you check the element tags I have uploaded and check if that will work for your code. What I thought is your code is idiot proofing and more correct than what I have shared. But the purpose is the same and works the same as well. But I am unable to locate the element itself. I would appreciate if you can give some more detailed input on this. TIL. – Avishek2585835 Jul 17 '13 at 05:36
  • As you can see ,Input type is file so CLICK WILL NOT WORK.. – Archana Singh Jul 17 '13 at 05:53
  • I modified the code as below:File file=null; try { file=new File("D:\\AD\\Prac\\Prac\\002 EditPrac Add Person Error.xml-revHEAD.svn000.tmp.xml"); } catch(Exception e) { e.printStackTrace(); } Assert.assertTrue(file.exists()); WebElement browserButton=driver.findElement(By.className("iceInpFileTxt")); browserButton.sendKeys(file.getAbsolutePath()); - still it gives the same error-element not found. I tried using xpath as well but that did not work either. Please advice, – Avishek2585835 Jul 18 '13 at 06:24
0

Try this code:

driver.FindElement(By.XPath("/html/body/div[2]/div[5]/div/div/div/div[2]/div[2]/div[1]/div/div[1]")).click();
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129