1

I was executing a selenium automation on IE11. Now for an Element, say ele; ele.sendKeys(characters) are not working directly. So I was trying to change the 'value' attribute for that <input> tag through JavaScript Executor. Once I change that, I am verifying the same by ele.getAttribute('value'). But that time I am getting that the value is still null like earlier. And My test is also failing for the same.

HTML code

<form id="upload" method="post" action="/upload" enctype="multipart/form-data" style="width: 90%">
<label for="uploadinputFile">
<br style="clear:all">
<input id="browse_file" class="bttn-primary" type="button" value="Browse">
<input id="file_input_browser" type="file" name="upload_File">
<div id="button">
<input id="submit" class="bttn-primary" type="submit" disabled="" value="Upload">
</div>
</form>

Selenium Code

WebElement brw=driver.findElement(By.id("file_input_browser"));
((JavascriptExecutor) driver).executeScript("document.getElementById('file_input_browser').setAttribute('value', 'new value for element')");
System.out.println("value:"+brw.getAttribute("value"));

I have also used following JavaScriptExecutor: ((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('value', '" +c+"')",brw);//c is a String

But Everytime I am getting output for brw.getAttribute("value") as blank/null

8
  • What exactly is the issue with .sendKeys()? Does the code throw some Exception? Because manipulating the DOM is merely a workaround.
    – Robert G
    Commented Feb 24, 2017 at 13:41
  • It is not throwing any error...The selenium code is running..but for some reason IE is not prompting as expected as it does manually. So the Script keeps waiting for the next element to appear. The thing is if this SendKeys() works then only the <input id="submit"...> will be enabled and I can click on that. But as the sendKeys is not putting any value so that Upload button keeps disabled.
    – RCode
    Commented Feb 24, 2017 at 13:49
  • Manually when <input id="browse_file"...> is clicked then the Windows default file selection opens to select a file. In Firefox I was clicking that browser option and then select files through Robot class. But on IE11 though the click is happening the windows default file selection was not poping up. So I was trying to do sendKeys(filepath) for those two <input> tag. But that also didnot work then I was directly trying to inject the file path on DOM element through javaScriptExecutor. If you could tell me work around of windows default file selection pop up that also resolve my purpose.
    – RCode
    Commented Feb 24, 2017 at 14:10
  • Manually while we are clicking that browser option on IE the windows default file selection pops up, but on automation it is failing to open after successful click on that element. Failing in the means after clicking the UI has no response...it doesnot pop up that file selection option. I also tried clicking with Actions class...Thanks in Adv.
    – RCode
    Commented Feb 24, 2017 at 14:10
  • Can you post a link to the page?
    – JeffC
    Commented Feb 24, 2017 at 14:17

1 Answer 1

1

You have to instantiate your WebElement after manipulating the DOM, not before. Otherwise the stored WebElement won't contain the information you are looking for and will return null.

driver.executeScript("document.getElementById('ID').setAttribute('value','NEW_VALUE');");
System.out.print("value: "+driver.findElement(By.id("ID")).getAttribute("value"));
2
  • That's weird, I edited my post slightly to better represent what I tested. The posted code worked fine for me, however I'm using Selenium 3.1.0 running against Chrome and not IE - maybe the issue is IE related.
    – Robert G
    Commented Feb 24, 2017 at 14:08
  • I am trying selenium 2.53.1 with IE11...Let me check once more with latest selenium for this...
    – RCode
    Commented Feb 24, 2017 at 14:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.