简体   繁体   中英

Handling load file dialog with selenium webdriver

I'm trying to handle 'select files to load' dialog with the help of selenium webdriver. Here is HTML:

<form class="upload">
    <button class="btn" data-capture="" type="button">Browse</button>
    <input class="hide" type="file" accept="..." multiple="" data-file-input=""/>
</form>

What i'm trying to do:

  1. remove class='hide';
  2. senndkeys with file path, after elemnt is unhidden;
  3. hide element back.

Here is the code to do this:

JavascriptExecutor js = (JavascriptExecutor) webDriver;
js.executeScript("$('.hide:not(.layout)').removeClass('hide')");
(webDriver.findElement(By.cssSelector(".upload>input"))).sendKeys("path_to_file");
js.executeScript("$('.hide:not(.layout)').addClass('hide')");

But i got exception on 3rd line:

"Runtime.evaluate threw exception: TypeError: Cannot read property 'click' of null"

What am i doing wrong?

Thank you, user1433852. It looks like the problem was really with wait. After adding waiting for ajax, it starts to work!

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