简体   繁体   English

使用Selenium WebDriver处理加载文件对话框

[英]Handling load file dialog with selenium webdriver

I'm trying to handle 'select files to load' dialog with the help of selenium webdriver. 我正在尝试借助Selenium Webdriver处理“选择要加载的文件”对话框。 Here is HTML: 这是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'; 删除class ='hide';
  2. senndkeys with file path, after elemnt is unhidden; 取消隐藏elemnt后,带有文件路径的senndkeys;
  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. 谢谢user1433852。 It looks like the problem was really with wait. 看来问题确实在于等待。 After adding waiting for ajax, it starts to work! 添加等待的ajax后,它开始工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM