I am using Selenium and the ChromeDriver to automatically download certain files. Inspired by this append and this append I am trying to get hold of the name of a downloaded file (I have to do that since the site I am trying to download from generates random filenames when clicking “Download”).
The below code tries to obtain the downloaded file’s name from the browser’s download history:
...
driver.get("chrome://downloads");
JavascriptExecutor js = (JavascriptExecutor)driver;
String filename = (String) js.executeScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item'.shadowRoot.querySelector('div#content #file-link').text");
...
This always yields me the error message:
Exception javascript error: Cannot read properties of null (reading 'shadowRoot')
(Session info: chrome=131.0.6778.109)
Build info: version: '4.21.0', revision: '79ed462ef4'
System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '21.0.5'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [f43fc017e91fd07edeed66d94414d699, executeScript {script=return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div#content #file-link').text, args=[]}]
However, if I open the Chrome browser’s Developer Tools Console and enter (i.e. copy&paste) that very same command string [return] document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item'.shadowRoot.querySelector('div#content #file-link').text
on the command line it returns me the downloaded file’s name, so the expression seems correct.
Note that one has to omit the leading “return” when pasting the command on the browser console. In the Java application I tried with a return
(i.e. js.executeScript("return document.querySelector...");
as well as without (i.e. js.executeScript("document.querySelector...");
) but that didn’t make any difference and the error message is the exactly the same.
Any idea anyone, why the js.executeScript(...)
doesn’t yield me same result as entering the same command on the browser console?
This is using the currently latest Chrome version “131.0.6778.109 (Official Build) (64-bit)” and Selenium as well as ChromeDriver v4.21.0 (on Java v21).