Selenium WebDriver - 将文档上载到非输入按钮 [英] Selenium WebDriver - Upload document to non-input button

查看:88
本文介绍了Selenium WebDriver - 将文档上载到非输入按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Chromedriver通过Selenium WebDriver上传文档。我已经尝试了所有的Action类和Javascript的东西,但那些不起作用。我假设它们不起作用,因为那些依赖于按钮作为输入字段,但是,我正在处理的上传按钮不是。它的HTML看起来像这样:

I am needing to upload a document via Selenium WebDriver using Chromedriver. I have tried all the Action class and Javascript stuff, but those do not work. I am assuming they do not work because those are relying on the button to be an input field, however, the upload button I'm dealing with is not. It's HTML looks like this:

<a id="Dialogs_Dialogs_lbAttachUpload" onclick="return ButtonVerifyEnabled(this, ShowFileSelect);" class="SkinButton sbBlue" onmouseover="ButtonHover(this,30);" onmouseout="ButtonLeave(this);" onmousedown="ButtonDown(this,30);" onmouseup="ButtonHover(this,30);" skinheight="30" style="color: white; width: 132px; height: 30px; line-height: 30px; background-position: 0px 0px;" title=""><div class="SkinButtonLeft" style="background-position: 0px 0px;"></div><div class="SkinButtonRight" style="background-position: -4px 0px;"></div>Upload documents</a>

我有AutoIT和Sikuli实施和工作,但这些解决方案的问题是我无法让他们通过Jenkins运行Selenium测试时的工作。

I have AutoIT and Sikuli implemented and working, but the problem with those solutions is I cannot get them to work when running the Selenium tests via Jenkins.

我的最新尝试如下:

    WebElement upload = SConfirmOrder.uploadDocuments_btn(driver);
    Actions actions = new Actions(driver);
    actions.moveToElement(upload);
    actions.sendKeys("filepath\\Test PDF.pdf");

它成功运行,但实际上没有文件上传。

It runs through successfully, but no document actually gets uploaded.

推荐答案

浏览器无法上传没有< input> 元素的文件,除非文件从桌面。能够通过代码上传文件将是一个安全漏洞。

The browser cannot upload a file without an <input> element, unless the file is dropped from the desktop. It would be a security breach to be able to upload a file by code.

所以在你的情况下,< input> 可能是在用户点击链接后创建的。

So in your case, the<input> is probably created once the user has clicked the link.

处理此案例的一种方法是使点击事件,点击链接,然后将文件设置为< input>

One way to handle this case is to silence the click event, click the link and then set the file to the <input>:

// disable the click event on an `<input>` file
((JavascriptExecutor)driver).executeScript(
    "HTMLInputElement.prototype.click = function() {                     " +
    "  if(this.type !== 'file') HTMLElement.prototype.click.call(this);  " +
    "};                                                                  " );

// trigger the upload
driver.findElement(By.id("Dialogs_Dialogs_lbAttachUpload"))
      .click();

// assign the file to the `<input>`
driver.findElement(By.cssSelector("input[type=file]"))
      .sendKeys("filepath\\Test PDF.pdf");

请注意,您可能还需要等待< input> 即可创建。

Note that you may also need to wait for the <input> to be created.

这篇关于Selenium WebDriver - 将文档上载到非输入按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆