Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用Chrome Webdriver关闭下载横幅?_Java_Google Chrome_Selenium_Webdriver - Fatal编程技术网

Java 如何使用Chrome Webdriver关闭下载横幅?

Java 如何使用Chrome Webdriver关闭下载横幅?,java,google-chrome,selenium,webdriver,Java,Google Chrome,Selenium,Webdriver,在我的测试中,我下载了一个文件,该文件工作正常,但是当我尝试单击一个无法滚动到视图中的元素时,页面底部的chrome下载对话框挡住了我的去路。没有办法将我需要单击的按钮移动到视图中,那么有没有办法用chrome webdriver关闭下载框?您可以使用org.openqa.selenium.interactions.Actions类移动到元素视图中: WebElement element = driver.findElement(By.id("my-id")); Actions actions

在我的测试中,我下载了一个文件,该文件工作正常,但是当我尝试单击一个无法滚动到视图中的元素时,页面底部的chrome下载对话框挡住了我的去路。没有办法将我需要单击的按钮移动到视图中,那么有没有办法用chrome webdriver关闭下载框?

您可以使用
org.openqa.selenium.interactions.Actions
类移动到元素
视图中:

WebElement element = driver.findElement(By.id("my-id"));
Actions actions = new Actions(driver);
actions.moveToElement(element);
// actions.click();
actions.perform();
回答你的问题: ,目前无法通过Selenium/WebDriver访问(并因此关闭)浏览器的下载对话框(在您的情况下是chrome)

您可以做些什么:
  • 使用浏览器开发工具(按F12键)确定要单击的按钮是否具有id或其他内容来定位它
  • 然后您可以只执行
    driver.findElement(您的定位器)
假设你的按钮是这样的:

然后,您可以按如下方式定义定位器:


By yourLocator=By.id(“我的按钮”)

您可以使用下面的代码片段打开下载网页,然后关闭它并返回到目标网页:

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);
action.sendKeys(key.CONTROL+j”).build().perform();
action.keyUp(Keys.CONTROL).build().perform();
睡眠(500);
ArrayList tabs2=新的ArrayList(driverChrome.getWindowHandles());
driverChrome.switchTo().window(tabs2.get(1));
睡眠(500);
driverChrome.close();
driverChrome.switchTo().window(tabs2.get(0));
睡眠(500);

发送控制键对我不起作用,但我开发了一种解决方法。我在一个新窗口中做任何下载测试,然后关闭下载窗口,原来的窗口没有下载栏。它必须是一个新窗口,如果你做了一个新的标签,它会转移过来,为了得到这个,我使用JavaScript。切换到新窗口,运行下载测试,完成后切换到原始窗口

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