Reputation: 91
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.
Please refer: Selenium Webdriver FIle Upload error element ice:inputFile
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);
Got the hint from here Selenium WebDriver clicking on hidden element.
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'");
Please advice.
Upvotes: 1
Views: 5914
Reputation: 38434
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");
Note that when you'll try to interact with elements outside the <iframe>
, you'll have to do driver.switchTo().defaultContent();
beforehand.
Side notes:
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 :).value
right away.String ew = (String)js.executeScript("document.getElementByXPath('//form[@id='fileUploadForm']/input[3]')");
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);
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()
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';
contains an error, the method (if it existed) takes a string, so you should quote the "iceInpFileTxt"
.
Upvotes: 1