需要有关隐藏文件上传元素的 webdriver 和 Javascript 的帮助 [英] Need help for webdriver and Javascript for a hidden File upload element
问题描述
我正在尝试在客户端 Web 应用程序中自动执行文件上传方案.整个文件上传表单的代码是这样的
I am trying to automate a file upload scenario in a client web application. The code of the entire file upload form looks like this
<td valign="top">
<iframe id="batchLoad:inputFile:uploadFrame" class="iceInpFile" width="600px" scrolling="no" height="30px" frameborder="0" title="Input File Frame" style="border-collapse:collapse; border-spacing:0px; padding:0px;" src="/hip-webapp/block/resource/LTExMzg4MjQzMTY=/" name="batchLoad:inputFile:uploadFrame" marginwidth="0" marginheight="0" allowtransparency="true">
<html style="overflow:hidden;">
<head>
<body style="background-color:transparent; overflow:hidden">
<form id="fileUploadForm" enctype="multipart/form-data" action="/hip-webap/uploadHtml" method="post">
<input type="hidden" value="batchLoad:inputFile" name="ice.component">
<input type="hidden" value="3" name="ice.view">
<input class="iceInpFileTxt" type="file" size="35" name="upload">
<input class="iceInpFileBtn" type="submit" value="Upload">
</form>
</body>
</html>
</iframe>
<br>
<span id="batchLoad:j_id537"></span>
</td>
我尝试使用传统的文件上传方法,但没有奏效.
I tried with the conventional File upload method, but that did not work.
请参考:Selenium Webdriver 文件上传错误元素 ice:inputFile
我对 Javascript 不太熟悉,因此我认为我在做一些语法错误.我试过的是:
I am not much familiar with Javascript hence I think I am doing some syntax error. What I tried is:
String ew = (String)js.executeScript("document.getElementByXPath('//form[@id='fileUploadForm']/input[3]')");
String j = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';";
js.executeScript(j, ew);
从这里得到提示 Selenium WebDriver 点击隐藏元素.
但是现在我遇到了语法错误.我使用 Selenium IDE 获得了 XPath.我也试过这个,但也没有用.
But now I am getting syntax error. I got the XPath using Selenium IDE. I have also tried this, but that did not work either.
((JavascriptExecutor)driver).executeScript("document.getElementByClassName(iceInpFileTxt).style.visibility = 'visible';");
((JavascriptExecutor)driver).executeScript("document.getElementByClassName('iceInpFileTxt').value = 'D:\\AD\\Prac\\Prac\\002 EditPrac Add Person Error.xml-revHEAD.svn000.tmp.xml'");
请指教.
推荐答案
因为您正在使用的页面上有一个 元素,您需要 首先将
driver
上下文切换到该 元素:
Because there is an <iframe>
element on the page you're working with, you need to switch the driver
context to that <iframe>
element first:
driver.switchTo().frame("batchLoad:inputFile:uploadFrame");
完成后,再次尝试通常的上传方法.
Once you've done that, try the usual upload method again.
driver.findElement(By.name("upload")).sendKeys("D:\\AD\\Prac\\Prac\\002 EditPrac Add Person Error.xml-revHEAD.svn000.tmp.xml");
请注意,当您尝试与 之外的元素进行交互时,您必须执行
driver.switchTo().defaultContent();
事先.
Note that when you'll try to interact with elements outside the <iframe>
, you'll have to do driver.switchTo().defaultContent();
beforehand.
附注:
iframe
没有结束</head>
标记.这不是问题,而是您可能会考虑的问题,因为您很可能正在开发网络 :)- 除非对于某些 Flash/Silverlight 控件,您当然不必使不可见元素可见,也不应该手动将文件注入隐藏的输入元素.您可以直接设置
value
. String ew = (String)js.executeScript("document.getElementByXPath('//form[@id='fileUploadForm']/input[3]')");
- The
iframe
does not have a closing</head>
tag. This is not an issue, but something you might think about since you're most likely developing the web :). - You certainly don't have to make the invisible elements visible nor should you inject the file to the hidden input element manually unless for some Flash/Silverlight controls. You could have simply set the
value
right away. String ew = (String)js.executeScript("document.getElementByXPath('//form[@id='fileUploadForm']/input[3]')");
包含错误.您正在使用无效的嵌套 '
.您应该通过 \
转义内部单引号.此外,它应该返回一个 WebElement
,所以你应该将结果转换为 WebElement
,而不是一个 String
.
contains an error. You are using nested '
which does not work. You should escape the inner single quote via a \
. Also, it should return a WebElement
, so you should cast the result to WebElement
, not to a String
.
js.executeScript(j, ew);
如上所述,第二个参数应该是一个 WebElement
.此外,如果您之前切换到正确的帧,您可以通过 driver.findElement()
以通常的方式找到它.
As said above, the second argument should be a WebElement
. Also, if you had switched to the correct frame before, you could have found it the usual way via driver.findElement()
.
document.getElementByClassName()
不存在.正确的方法名称是 document.getElementsByClassName()
(注意复数元素").它返回一组元素,因此您需要在 for 循环中迭代它们,或者盲目地选择第一个 ([0]
).
document.getElementByClassName()
does not exist. The right method name is document.getElementsByClassName()
(note the plural "elements"). And it returns a set of elements, so you need to iterate over them in a for loop, or blindly pick the first one ([0]
).
document.getElementByClassName(iceInpFileTxt).style.visibility = 'visible';
包含错误,该方法(如果存在)需要一个字符串,因此您应该引用 "iceInpFileTxt"
.
contains an error, the method (if it existed) takes a string, so you should quote the "iceInpFileTxt"
.
这篇关于需要有关隐藏文件上传元素的 webdriver 和 Javascript 的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!