How to close download banner using Chrome Webdriver?

In my tests, I upload a file that works fine, but later, when I try to click an item, I cannot scroll to the viewport, at the bottom of the page is the Chrome download dialog. It is not possible to move the button that I need to click on, so is there a way to close this boot box with chrome webdriver?

+4
source share
4 answers

You can use the class org.openqa.selenium.interactions.Actionsto go to the element view:

WebElement element = driver.findElement(By.id("my-id"));
Actions actions = new Actions(driver);
actions.moveToElement(element);
// actions.click();
actions.perform();
+2
source

Answer your question:

, (, , ) ( ) Selenium/WebDriver.

:

  • ( F12), , , , , .
  • driver.findElement(yourLocator).click();

, :

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

:

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

+1

, -, :

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);
+1

Sending 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, there is no download panel in the original window. It should be a new window, if you make a new tab, which it will transfer to get this, I use JavaScript. Switch to a new window, run the boot test, and then when finished, go to the original window.

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
0
source

Source: https://habr.com/ru/post/1598374/


All Articles