Using javascriptexecutor with a file chooser causing problems

66 views
Skip to first unread message

Tony Chamberlain

unread,
Oct 25, 2023, 10:26:33 AM10/25/23
to Selenium Users
I am using javascriptexecutor with Selenium (IEdriver used on Edge (IE compatibility).
There is a input with w BROWSE button that brings up a Windows file search.  (See picture).  Selenium can't handle Windows file browser.

The HTML looks like this

    <span class="options">
        <input id="importSource" title="Source" size="50" type="file" 
         name="importSource" requiredfield="1">
   </span>

Doing a sendKeys(file) to the element will bring up the file browser.
I wanted to use JavascriptExecutor, so I did

     WebElement ele = driver.findElement(By.xpath("//input[@id='importSource']"));
    JavascriptExecutor jse = (JavascriptExecutor) driver;
    String script = "arguments[0].setAttribute('value', '" + fileName + "');";
    jse.executeScript(script, ele);

fileName is the name of a file.  But when I look at the box it is empty and when I inspect
ele.getAttribute("value") it is blank.

If I manually click browse and select a file and do an inspect ele.getAttribute("value") it shows the filename.  So setting it should work but doesn't.

is there some reason the Javascript is not working?  The value of ele is not null as I can get the attribute. (In the picture I removed any company-specific confidential info, but it is just text).

fil.png

David

unread,
Oct 30, 2023, 7:06:38 PM10/30/23
to Selenium Users
What version of Selenium are you using? I recall Selenium (some version) supported sending keys to file upload element before, and also supported it under RemoteWebDriver / grid mode, but you may have to enable a setting for that so that selenium behind the scenes uploaded the file from the test execution machine to the remote node where the browser runs to then be able to have the browser select the file to upload (from the remote node after it is uploaded to remote node first).

And is this issue across all browsers or specific Edge browser which you mention.

Tony Chamberlain

unread,
Nov 6, 2023, 3:52:28 PM11/6/23
to Selenium Users
All.  Selenium is selenium-java 4.11.0
Reply all
Reply to author
Forward
0 new messages