【问题标题】:how to change file download location in Webdriver while using chrome driver/firefox driver如何在使用 chrome 驱动程序/firefox 驱动程序时更改 Webdriver 中的文件下载位置
【发布时间】:2015-03-05 14:57:20
【问题描述】:

我正在尝试通过在特定文件夹中使用另存为选项来保存图像。我找到了一种方法,可以使用另存为选项右键单击要保存的图像。但是我被卡住的问题是在获得询问将文件保存在哪里的操作系统窗口之后,我无法发送所需的位置,因为我不知道该怎么做。我经历了在这个论坛上提出的类似问题,但到目前为止都没有帮助。

代码是-

对于 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

对于铬-

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
   }
 }

【问题讨论】:

    标签: java file-upload selenium-webdriver download automated-tests


    【解决方案1】:

    代码中有两件事出错了。

    对于 Firefox: 你需要设置

    profile.setPreference("browser.download.dir", "C:\\Users\\Admin\\Desktop\\ScreenShot\\");
    

    不要

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

    其次,您正在设置偏好browser.download.folderlist,它是browser.download.folderList(文件夹列表中的L大写)。

    一旦你实现了这两个,你就可以使用你的机器人类来执行所需的操作。

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

    希望这会有所帮助。 :)

    【讨论】:

    • 它现在可以在 Firefox 上运行,但是如何在使用 Chome 驱动程序时做到这一点?
    • 我可以为此目的使用 AutoIT 吗?是否必须将 AutoIT 与 TestNG 一起使用,或者我可以单独与 selenium webdriver 一起使用
    • AutoIT 是最好的方法。机智机器人的问题是您需要在系统执行时冻结您的系统。 AutoIT 消除了该问题,但在使用 autoIT 时,记住系统不应暂停。
    • 和 autoIt 与 testng 或 selenium webdriver? AutoIt 用作 Process 调用,因此它与 testng 或 selenium 无关。
    • 也许你想使用 File.separator 作为路径中的斜线?
    【解决方案2】:

    对于 Chrome 浏览器

    你甚至可以用下面的代码 sn-p 来禁用窗口对话框(Save As Dialogue)。您需要在 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);
      

    【讨论】:

      【解决方案3】:

      我花了很多时间研究如何在没有“另存为”弹出窗口的情况下在 Firefox 浏览器中下载 pdf 文件。它可能是帮助某人。

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

      profile.setPreference("pdfjs.disabled", true);
      profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
      

      当然你可以添加一些额外的偏好。

      它适用于 Firefox 45-46 版本。

      【讨论】:

        【解决方案4】:

        您部分回答了自己的问题:

        我被卡住的问题是在获取操作系统窗口之后

        Selenium 是一个 浏览器 自动化工具 - 操作系统窗口 不是浏览器!您将需要使用其他东西。有很多选择,具体取决于您的需求:Sikuli、Robot、AutoIt、...

        【讨论】:

        • 是否可以使用 Robot 更改目标文件夹位置?
        【解决方案5】:

        使用相同的 Robot 类并按 Enter 键在 Windows 对话框中选择“保存”。

        robo.keyPress(KeyEvent.VK_ENTER); robo.keyRelease(KeyEvent.VK_ENTER);

        如果你需要重命名它,复制剪贴板中的文件名并像下面这样传递

        StringSelection file = new StringSelection("D:\\image.jpg");
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(file, null);
        
        Robot rb = new Robot();
        
        rb.setAutoDelay(2000); // Similar to thread.sleep
        
        rb.keyPress(KeyEvent.VK_CONTROL);
        rb.keyPress(KeyEvent.VK_V);
        
        rb.keyRelease(KeyEvent.VK_CONTROL);
        rb.keyRelease(KeyEvent.VK_V);
        
        rb.setAutoDelay(2000);
        
        rb.keyPress(KeyEvent.VK_ENTER);
        rb.keyRelease(KeyEvent.VK_ENTER);
        

        【讨论】:

          【解决方案6】:

          可能不是最好的解决方案,但您可以尝试使用 sikuli api 来确认显示的框的保存。

          另存为框是一个操作系统窗口。

          【讨论】:

            【解决方案7】:

            对于 Chrome,它可以工作

            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);
            WebDriver driver = new ChromeDriver(options);
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-03-20
              • 2012-08-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多