将文件与Chromedriver保存在chrome:// downloads中

问题描述 投票:0回答:2

我正在尝试使用Google Chrome自动下载XML文件。我正在使用:

  • 谷歌浏览器v73.0.3683.75(64位)
  • Chromedriver v73
  • Selenium WebDriver v3.14.0
  • C#

出现有害文件的消息时出现问题:

Harmful file message

当我使用Chromedriver时,我无法与此消息进行交互,因此我尝试接受来自chrome:// downloads页面的下载。

打开chrome:// downloads页面后,我点击Keep按钮,但会再次出现Alert以确认下载。

Confirm download popup in chrome://downloads

这个弹出窗口不是弹出的Selenium和Chromedriver可以使用Dismiss()/ Accept()/ SendKeys()/ ...方法处理。当我尝试SwitchTo()时,Chromedriver崩溃了。

我试图直接发送{TAB}和{SPACE} / {RIGHT}和{ENTER}的按键,但Chrome似乎没有抓住它们......

完整的代码是:

String currentWindow = this.Drivers[Navegador].CurrentWindowHandle;
String popupHandle = "";

((IJavaScriptExecutor)this.Drivers[Navegador]).ExecuteScript("window.open('about:blank','_blank')");

ReadOnlyCollection<String> tabs = this.Drivers[Navegador].WindowHandles;
foreach (string handle in tabs){
    if (handle != currentWindow){
        popupHandle = handle;
        break;
    }
}
this.Drivers[Navegador].SwitchTo().Window(popupHandle);
this.Drivers[Navegador].Navigate().GoToUrl("chrome://downloads");
String script = "return document.querySelector('body > downloads-manager').shadowRoot.querySelector('#downloadsList > downloads-item').shadowRoot.querySelector('#dangerous > paper-button:nth-child(2)');";
//String script = "return document.querySelector('body > downloads-manager').shadowRoot.querySelector('#downloadsList > downloads-item:nth-child(2)').shadowRoot.querySelector('#url').textContent;";
IWebElement boton = (IWebElement) ((IJavaScriptExecutor) this.Drivers[Navegador]).ExecuteScript(script);
boton.Click();
Thread.Sleep(2000);
SendKeys.Send("{TAB}{SPACE}");
Thread.Sleep(1000);
this.Drivers[Navegador].Close();
this.Drivers[Navegador].SwitchTo().Window(currentWindow);
this.Drivers[Navegador].SwitchTo().DefaultContent();
result = true;

重要说明:我尝试使用所有标志/选项/ experimental_options / user_preferences / ...启动Chrome,但它不起作用。这些选项/参数似乎在最新版本的Chrome或Chromedriver中已弃用。

selenium selenium-webdriver selenium-chromedriver
2个回答
0
投票

正如OP讨论的那样,回答Java中的问题。

几个月前遇到了同样的问题,所以这就是它对我有用的方式,也可能适合你。

Map<String, Object> chromePreferences = new Hashtable<String, Object>();
// Below preferences will disable popup dialog while downloading the file
chromePreferences.put("profile.default_content_settings.popups", 0);
chromePreferences.put("download.prompt_for_download", "false");

// Set the customised path for downloading the file, if required
chromePreferences.put("download.default_directory", "path");

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("prefs", chromePreferences);

DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

//Now initiate ChromeDriver with the capabilities set above and continue with your automation
ChromeDriver chromeDriver = new ChromeDriver(cap);

0
投票

我最近遇到了这个问题,由于ChromeDriver中某些方法的弃用,上述解决方案无效。

经过大量研究后,我决定转而使用本文的灵感来探索IE并探索另一种选择 - https://sqa.stackexchange.com/questions/3169/downloading-a-file-in-internet-explorer-through-selenium/3520我在JAVA中提出了这个解决方案。

它不像'干净',但它对我有用。

 public static void main(String[] args) throws NoAlertPresentException,InterruptedException  {                                  
    System.setProperty("webdriver.ie.driver","C:\\selenium-java-3.141.59\\IEDriverServer.exe");

    String url ="myfileurl";

    WebDriver driver = new InternetExplorerDriver();
    driver.get(url);

    try {
    Robot robot = new Robot();
    Thread.sleep(2000);

    //press alt+s key to save       
    robot.keyPress(KeyEvent.VK_ALT);
    robot.keyPress(KeyEvent.VK_S);
    robot.keyRelease(KeyEvent.VK_S);
    robot.keyRelease(KeyEvent.VK_ALT);
    Thread.sleep(2000);

    }
     catch (AWTException e) {

     e.printStackTrace();
     }

    driver.close();

}   
© www.soinside.com 2019 - 2024. All rights reserved.