繁体   English   中英

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

[英]how to change file download location in Webdriver while using chrome driver/firefox driver

我试图通过在特定文件夹中使用另存为选项来保存图像。 我找到了一种方法,通过另存为选项,我可以右键单击要保存的图像。 但我遇到的问题是在获取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

对于铬 -

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

这是我被卡住的弹出窗口

代码中有两个问题。

对于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 (在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);

希望这可以帮助。 :)

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

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

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

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

它适用于Firefox 45-46版本。

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

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

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

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

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

Selenium是一个浏览器自动化工具 - os窗口不是浏览器! 您将需要使用其他东西。 根据您的需求,有很多选择:Sikuli,Robot,AutoIt,......

使用相同的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);

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM