Cannot go to "chrome://downloads/" in a newly opened tab with Selenium in Chrome
Asked Answered
M

3

6

I am using Selenium and Java to write a test for Chrome browser. My problem is that somewhere in my test, I download something and it covers a web element. I need to close that download bar (I cannot scroll to the element). I searched a lot and narrowed down to this way to open the download page in a new tab:

((JavascriptExecutor) driver).executeScript("window.open('chrome://downloads/');");

It opens that new tab, but does not go to download page.

I also added this one:

driver.switchTo().window(tabs2.get(1));
driver.get("chrome://downloads/");

but it didn't work either.

I tried:

driver.findElement(By.cssSelector("Body")).sendKeys(Keys.CONTROL + "t");

and

action.sendKeys(Keys.CONTROL+ "j").build().perform();
action.keyUp(Keys.CONTROL).build().perform();
Thread.sleep(500);

but neither one even opened the tab.

Marjana answered 12/1, 2017 at 17:51 Comment(0)
S
1

It is because you can't open local resources programmatically. Chrome raises an error:

Not allowed to load local resource: chrome://downloads/

Working solution is to run Chrome with following flags:

--disable-web-security --user-data-dir="C:\chrome_insecure"

But this trick doesn't work with Selenium Chrome Driver(I don't know actually why, a tried to remove all args that appears in chrome://version, but this doesn't helps).

So for me above solution is the only one that works:

C# example:

driver.Navigate().GoToUrl("chrome://downloads/")

There is another trick if you need to open downloaded file:

JavaScript example:

document.getElementsByTagName("downloads-manager")[0].shadowRoot.children["downloads-list"]._physicalItems[0].content.querySelectorAll("#file-link")[0].click()

Chrome uses Polymer and Shadow DOM so there is no easy way to query #file-link item.

Also you need to execute .click() method with JavaScript programatically because there is a custom event handler on it, which opens actual downloaded file instead of href attribute which point to url where you downloaded file from.

Strobel answered 12/1, 2018 at 11:52 Comment(0)
A
0

I started with this post and ended up with the solution given below. This one works in Chrome 71. First I highlighted the control and then clicked it. The window object is actually the IWebDriver, the second method is called after the first one.

  internal void NavigateToDownloads()
        {
            window.Navigate().GoToUrl("chrome://downloads/");
        }
 internal void OpenFirstDownloadLinkJS()
        {
            IJavaScriptExecutor js = (IJavaScriptExecutor) window;
            js.ExecuteScript("document.getElementsByTagName('downloads-manager')[0].shadowRoot.children[4].children[0].children[1].shadowRoot.querySelectorAll('#content')[0].querySelector('#details > #title-area > #file-link').setAttribute('style', 'background: yellow;border: 2px solid red;');");
            js.ExecuteScript("document.getElementsByTagName('downloads-manager')[0].shadowRoot.children[4].children[0].children[1].shadowRoot.querySelectorAll('#content')[0].querySelector('#details > #title-area > #file-link').click();");
        }
Almswoman answered 12/11, 2019 at 13:20 Comment(0)
B
0

Use this code (I wrote it in Python, but it should work in Java too with very slight modifications):

#switching to new window
driver.execute_script("window.open('');")
driver.switch_to.window(driver.window_handles[1])

#opening downloads
driver.get('chrome://downloads/')

#closing downloads:
driver.close()
Blatant answered 28/1, 2023 at 21:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.