1

I'm using Selenium, java and classloader to upload a pdf-file to a HTML-page with the element

type="file"

My code is:

    String fileURI = ClassLoader.getSystemResource("file.pdf").getFile();

    setText(DOCUMENTATION_FILE_FIELD, fileURI);

    public void setText(Locator textField, String text) throws Exception {
         HtmlInput htmlInput = (HtmlInput) getElement(textField);
        pageUtils.page = (HtmlPage) htmlInput.setValueAttribute(text);
    }

When I press the "upload" button on the page, nothing happens. No errors, no file is uploaded. When I get the text from the DOCUMENTATION_FILE_FIELD, the correct path is shown. When googling I read in a forum that it is not possible to automatically upload a file because of security reasons. Is this correct? If this is the case, is there any other ways to do this.

TorK
  • 567
  • 2
  • 10
  • 27
  • Hey, I previously had a problem with uploading because the element supported multiple uploads as described here: http://stackoverflow.com/questions/14592853/how-to-upload-a-file-in-selenium-with-no-text-box Does your element have the "multiple" attribute? – Nathan Merrill Jul 24 '13 at 12:58

2 Answers2

0

I think you can use sendkeys() method and type the entire path of the file and hit submit button. Identify file object and against it use sendkeys method.

Vinay
  • 648
  • 4
  • 12
  • Thanks for your reply. I am having trouble finding the sendkeys() method. I do not think my system supports it. I am using selenium, gargoyle(htmlUnit). – TorK Jul 24 '13 at 08:31
  • Hmm how do you type content into a text box? – Vinay Jul 24 '13 at 09:46
  • `public void setText(Locator textField, String text) throws Exception { HtmlInput htmlInput = (HtmlInput) getElement(textField); pageUtils.page = (HtmlPage) htmlInput.setValueAttribute(text); }` – TorK Jul 24 '13 at 10:17
  • Can you pass the locator for the file upload dialog and the path as a string text? This might work. – Vinay Jul 24 '13 at 10:19
  • If I understand you correct, is that not what I tried in my question? – TorK Jul 24 '13 at 10:47
  • Instead of passing the URI can you pass the path of the pdf file along with file name and the extension. Just trying out. – Vinay Jul 24 '13 at 10:54
  • One last thing are you sure you have identified the file element properly? Can you put the setText code inside a try catch block rather than using throws. Then can you post the stack trace? Should be able to pin point the issue. Also if possible can you give me the url of the page or the html? – Vinay Jul 24 '13 at 11:25
  • Yes, the file element is identified with the correct id. There is no stack trace. There is no error. Everything is running fine, but no file is uploaded... The page is on an intranet, so I can not send the URL. – TorK Jul 24 '13 at 11:39
  • Sorry mate hope some one can help you out. will follow this post and will let you know if I find some other solution. – Vinay Jul 24 '13 at 11:53
  • Thank you so much for your effort in helping me. I will post here if I find an answer to my problem. – TorK Jul 24 '13 at 11:57
0

To set an <input type="file"> field, you don't set its text, you set its value! Therefore, don't use the setText() method (whereever it comes from), use something that sets a value (setValue()? setArgument()?).

Also, the <input type="file"> element does not accept a URL of the file, it takes an absolute path. This also means that your file has to have an absolute path - it can't be buried inside a JAR file. If you have it buried inside a JAR file, you need to copy it out first. Then obtain a path to it. If it accessible, do

URL fileUrl = ClassLoader.getSystemResource("file.pdf");
String filePath = new File(fileUrl.toURI)).getAbsolutePath();

You can then set the filePath as the value attribute of the <input> element.


You mention Selenium and yet you don't use any of its methods. If you actually use Selenium, then there are two options:

  • for Selenium RC, the Selenium#attachFile() method:

    selenium.attachFile("id=yourFileInputId", fileUrl);
    
  • for Selenium WebDriver, the WebElement#sendKeys() method

    driver.findElement(By.id("yourFileInputId")).sendKeys(filePath);
    
Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
  • Thank you for your reply. In my frustration yesterday, I forgot to add the setText() method. I edited my post now, and you can see the setText() use setValueAttribute(""). I can't manage to use the selenium methods. When I start up a class, I run: `client = new ExtendedWebClient(testConfiguration); TestData testdata = new TestData(testConfiguration); homePage = new HomePage(client, testConfiguration, testdata);` – TorK Jul 25 '13 at 06:25
  • @TorK Okay, that basically means that you're using plain HTMLUnit, not Selenium. While using plain HTMLUnit is good for low-level tinkering, if you want to be more productive and control the browser (HTMLUnit or any other one) on a higher level, think about using Selenium. Now - have you tried sending an absolute path of the file to the `` element instead of an URI? – Petr Janeček Jul 25 '13 at 08:30
  • oh ok, it was not I who sat up the environment :p If you read the comments in the posts above this one, you can see what I have tried. Yes, I have tried to send the absolute path. – TorK Jul 25 '13 at 08:56
  • @TorK Oh, okay, because your code doesn't show that (it still sends an URI). Have you tried [this](http://ksblog.org/index.php?q=how-to-perform-file-upload-with-htmlunit&id=42)? Also, alternatively, if there's some JS attached to the `` element, you could try [firing an `onchange` event manually](http://stackoverflow.com/questions/2856513/how-can-i-trigger-an-onchange-event-manually) to trigger the JS. – Petr Janeček Jul 25 '13 at 09:00