如何在使用chrome driver/firefox驱动程序时更改Webdriver中的文件下载位置

Sha*_*dan 19 java automated-tests file-upload download selenium-webdriver

我试图通过在特定文件夹中使用另存为选项来保存图像.我找到了一种方法,通过另存为选项,我可以右键单击要保存的图像.但我遇到的问题是在获取os窗口后询问保存文件的位置我无法发送所需的位置,因为我不知道该怎么做.我经历了在这个论坛上提出的类似问题但到目前为止他们没有帮助.

代码是 -

对于Firefox-

public class practice {

 public void pic() throws AWTException{
     WebDriver driver;

     //Proxy Setting     
        FirefoxProfile profile = new FirefoxProfile();
        profile.setAssumeUntrustedCertificateIssuer(false);
        profile.setEnableNativeEvents(false);
        profile.setPreference("network.proxy.type", 1);
        profile.setPreference("network.proxy.http", "localHost");
        profile.setPreference("newtwork.proxy.http_port",3128);

        //Download setting
        profile.setPreference("browser.download.folderlist", 2);
        profile.setPreference("browser.helperapps.neverAsk.saveToDisk","jpeg");
        profile.setPreference("browser.download.dir", "C:\\Users\\Admin\\Desktop\\ScreenShot\\pic.jpeg");
        driver = new FirefoxDriver(profile);

        driver.navigate().to("http://stackoverflow.com/users/2675355/shantanu");
        driver.findElement(By.xpath("//*[@id='large-user-info']/div[1]/div[1]/a/div/img"));
        Actions action = new Actions(driver);
        action.moveToElement(driver.findElement(By.xpath("//*[@id='large-user-info']/div[1]/div[1]/a/div/img"))).perform();
        action.contextClick().perform();
        Robot robo = new Robot();
        robo.keyPress(KeyEvent.VK_V);
        robo.keyRelease(KeyEvent.VK_V);
    // Here I am getting the os window but don't know how to send the desired location
    }//method   
}//class
Run Code Online (Sandbox Code Playgroud)

对于铬 -

public class practice {
   public void s() throws AWTException{
        WebDriver driver;   
        System.setProperty("webdriver.chrome.driver","C:\\Users\\Admin\\Desktop\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.navigate().to("http://stackoverflow.com/users/2675355/shantanu");
        driver.findElement(By.xpath("//*[@id='large-user-info']/div[1]/div[1]/a/div/img"));
        Actions action = new Actions(driver);
        action.moveToElement(driver.findElement(By.xpath("//*[@id='large-user-info']/div[1]/div[1]/a/div/img"))).perform();
        action.contextClick().perform();
        Robot robo = new Robot();
        robo.keyPress(KeyEvent.VK_V);
        robo.keyRelease(KeyEvent.VK_V);
        // Here I am getting the os window but don't know how to send the desired location
   }
 }
Run Code Online (Sandbox Code Playgroud)

这是我被卡住的弹出窗口

Viv*_*ngh 23

代码中有两个问题.

对于Firefox: 您需要设置

profile.setPreference("browser.download.dir", "C:\\Users\\Admin\\Desktop\\ScreenShot\\");
Run Code Online (Sandbox Code Playgroud)

不要

profile.setPreference("browser.download.dir", "C:\\Users\\Admin\\Desktop\\ScreenShot\\pic.jpeg");
Run Code Online (Sandbox Code Playgroud)

其次,您正在设置首选项browser.download.folderlist,它是browser.download.folderList(在folderList中为L caps).

一旦完成了这两项,您就可以使用Robot类来执行所需的操作.

对于Chromedriver试用:

String downloadFilepath = "/path/to/download";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(cap);
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.:)


小智 5

我花了很多时间研究如何在 Firefox 浏览器中下载 pdf 文件而不出现“另存为”弹出窗口。它可能会帮助某人。

经过一些本地调查,如何在没有任何“另存为”弹出窗口的情况下在 Firefox 中下载pdf文件,我在 Firefox 配置文件中发现了所需的最低首选项:

profile.setPreference("pdfjs.disabled", true);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
Run Code Online (Sandbox Code Playgroud)

当然,您可以添加一些额外的首选项。

它适用于 Firefox 45-46 版本。


小智 5

对于Chrome 浏览器

即使您可以使用以下代码片段禁用 Windows 对话框(另存为对话框)。您需要在 chromedriver 首选项中执行以下设置:

  • 如果出现下载提示,请关闭
  • 设置下载文件的默认目录
  • 如果启用了在浏览器中打开 PDF 文件的 PDF 查看插件,您可以禁用它,以便下载可以自动开始
  • 在浏览器中接受任何证书

    String downloadFilepath = "/path/to/download/directory/";
    Map<String, Object> preferences = new Hashtable<String, Object>();
    preferences.put("profile.default_content_settings.popups", 0);
    preferences.put("download.prompt_for_download", "false");
    preferences.put("download.default_directory", downloadFilepath);
    
    // disable flash and the PDF viewer
    preferences.put("plugins.plugins_disabled", new String[]{
        "Adobe Flash Player", "Chrome PDF Viewer"});
    
    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("prefs", preferences);
    
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    driver = new ChromeDriver(capabilities);
    
    Run Code Online (Sandbox Code Playgroud)