简体   繁体   中英

Upload file with Webdriver: nothing happens after sendKeys()

I'm trying to automate uploading of the file via Selenium Webdriver.

I've read a lot of questions on this theme on stackoverflow and used all the tips given there, and my code is actually working on test page: https://demo.vaadin.com/sampler/#ui/data-input/other/upload

But I can't make it working on my real project. First of all - my input field is invisible, so I made it visible with:

js.executeScript("arguments[0].setAttribute('style', arguments[1])",element, "opacity:100; width:50px");

I verified that the field is indeed became visible. Than I'm sending absolute path to the file via sendKeys() method:

Path path = Paths.get(value);
Path absolutePath = path.toAbsolutePath();
sendKeys(absolutePath.toString());

But when I try to check value of the field:

String v=elem.getAttribute("value");

it is always empty.

I tried to send incorrect path to file and this code gave me an error - not absolute path, so it is indeed trying to send something to the input field.

I suppose it has something to do with the code of the webpage under the test but I don't have enough knowledge to allocate the problem.

HTML code of the input element is the following:

<input type="file" data-bind="attr: {id: uid, name: inputName, multiple: isMultipleFiles}, disable: disabled, afterRender: onElementRender">

Following Code Worked for me in C#

SendKeys.SendWait(@"D:\filename");

SendKeys.SendWait(@"{Enter}");

Add "using System.Windows.Forms;" in the NameSpace
Add "System.Windows.Form" in the Reference

You might find this is because the path to the file is invalid. Be sure that the:

  • The path is correct
  • The file exists and is readable
  • The path is full and not relative
  • The path absolute (does not contain ..)
  • Your slashes are going int he right direction for the OS (some browsers care, some don't)

I found that this feature silently stopped working for me when I introduced a .. into the path.

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