4

In my tests I download a file, which works fine, but later when I am trying to click on an element I cannot scroll into view, the chrome download dialog box on the bottom of the page is in the way. There is no way to move the button I need to click into view, so is there a way to close that download box with chrome webdriver?

Josh
  • 51
  • 1
  • 6

4 Answers4

2

You can use the org.openqa.selenium.interactions.Actions class to move to an element view:

WebElement element = driver.findElement(By.id("my-id"));
Actions actions = new Actions(driver);
actions.moveToElement(element);
// actions.click();
actions.perform();
SidJj
  • 142
  • 1
  • 1
  • 11
1

Answer to your question:

No, there is currently no way to access (and therefore close) the download dialog of the browser (in your case chrome) via Selenium/WebDriver.

What you can do instead:

  • use the browser developer tools (press F12) to determine if the button you want to click has an id or sth else to locate it
  • then you can just do driver.findElement(yourLocator).click();

Let's say your button is sth like this:

<input id="my-button" class="button" type="submit" value="Click">

Then you can define your locator as follows:

By yourLocator = By.id("my-button");

drkthng
  • 6,651
  • 7
  • 33
  • 53
1

You can use this code snippet below to open your download web page, then close it an go back to your target page:

action.sendKeys(Keys.CONTROL+ "j").build().perform();
action.keyUp(Keys.CONTROL).build().perform();
Thread.sleep(500);        
ArrayList<String> tabs2 = new ArrayList<String> (driverChrome.getWindowHandles());
driverChrome.switchTo().window(tabs2.get(1));
Thread.sleep(500);
driverChrome.close();
driverChrome.switchTo().window(tabs2.get(0));
Thread.sleep(500);
Gustavo Morales
  • 2,614
  • 9
  • 29
  • 37
LoveLovelyJava one
  • 343
  • 1
  • 3
  • 16
1

Sending the control keys did not work for me, but I developed a workaround. I do any download test in a new window, then close the download window, the original window does not have the download bar. It must be a new window, if you do a new tab it will transfer over, to get this I use JavaScript. Switch to the new window, run download test and then switch to the original window when done.

string javascript = $"$(window.open('', '_blank', 'location=yes'))";

((IJavaScriptExecutor)Driver).ExecuteScript(javascript); //create new window

Driver.SwitchTo().Window(Driver.WindowHandles.Last())); //switch to new window

//do download test here

Driver.Close(); //close created window

Driver.SwitchTo().Window(Driver.WindowHandles.First()); //back to original window with no download bar
Matthew Bierman
  • 360
  • 3
  • 12