简体   繁体   English

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

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

I am trying to save an image by using save as option inside a specific folder. 我试图通过在特定文件夹中使用另存为选项来保存图像。 I found a way by which I am able to right click on the image which I want to save using save as option. 我找到了一种方法,通过另存为选项,我可以右键单击要保存的图像。 But the problem where I am stuck is after getting the os window which asks where to save the file I am not able to send the desired location because I don't know how to do it. 但我遇到的问题是在获取os窗口后询问保存文件的位置我无法发送所需的位置,因为我不知道该怎么做。 I went through the similar questions asked on this forum but non of them helped so far. 我经历了在这个论坛上提出的类似问题但到目前为止他们没有帮助。

Code is- 代码是 -

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

For chrome- 对于铬 -

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

这是我被卡住的弹出窗口

There are two things that are going wrong in code. 代码中有两个问题。

For Firefox: You need to set 对于Firefox:您需要设置

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

not to 不要

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

secondly, you are setting preference browser.download.folderlist , it is browser.download.folderList (L caps in folderList). 其次,您正在设置首选项browser.download.folderlist ,它是browser.download.folderList (在folderList中为L caps)。

Once you have achieved this both, you can use then your Robot class to perform desired operations. 一旦完成了这两项,您就可以使用Robot类来执行所需的操作。

For Chromedriver try out with: 对于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);

Hope this helps. 希望这可以帮助。 :) :)

I spent a lot of time to investigate how to download pdf file in firefox browser without Save As popup appearance. 我花了很多时间研究如何在没有另存为弹出窗口的Firefox浏览器中下载pdf文件。 It migth be help someone. 它可以帮助某人。

After some local investigation, how to download pdf file in firefox without any Save As popup, I found the minimum required preference in firefox profile: 经过一些本地调查,如何在没有任何另存为弹出窗口的情况下在firefox中下载pdf文件,我在firefox配置文件中找到了所需的最低首选项:

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

Of course you can add some additional preferences. 当然,您可以添加一些额外的首选项。

It works in Firefox 45-46 versions. 它适用于Firefox 45-46版本。

For Chrome Browser : 对于Chrome浏览器

Even you can disable the windows dialogue (Save As Dialogue) with the following code snippet. 即使您可以使用以下代码段禁用Windows对话框(另存为对话框)。 You need to do following settins in the chromedriver preferences: 您需要在chromedriver首选项中进行以下设置:

  • turn off the download prompt if it appears 如果出现,请关闭下载提示
  • set the default directory to download the file 设置默认目录以下载文件
  • If PDF view plugin is enabled which opens the PDF file in browser, you can disable that so that download can start automatically 如果启用了PDF视图插件,可以在浏览器中打开PDF文件,则可以禁用该文件以便下载可以自动启动
  • Accept any certificate in browser 在浏览器中接受任何证书

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

Probably not the best solution but you could try using sikuli api to confirm the save for the box that shows up. 可能不是最好的解决方案,但你可以尝试使用sikuli api来确认显示的框的保存。

The save as box is an OS window. 保存为框是一个操作系统窗口。

You partially answered your own question: 你部分回答了自己的问题:

the problem where i am stuck is after getting the os window 我被卡住的问题是在获得操作系统窗口之后

Selenium is a browser automation tool - os window is not a browser! Selenium是一个浏览器自动化工具 - os窗口不是浏览器! You will need to use something else. 您将需要使用其他东西。 There are many choices, depending on your needs: Sikuli, Robot, AutoIt, ... 根据您的需求,有很多选择:Sikuli,Robot,AutoIt,......

Use the same Robot class and press enter to select the "Save" in the Windows dialog box. 使用相同的Robot类,然后按Enter键在Windows对话框中选择“保存”。

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

if you need to rename it copy the file name in the clipboard and pass like below 如果你需要重命名它,请复制剪贴板中的文件名并传递如下

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

For Chrome, it will works 对于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.

相关问题 如何在运行时使用带有 chrome 驱动程序的 Selenium 更改下载路径 - How to change download path during runtime using Selenium with chrome driver 将Chrome驱动程序与Selenium Webdriver一起使用时SocketException - SocketException when using Chrome driver with Selenium Webdriver 在设备Apple iPhone 5上使用Chrome模拟器(Chrome驱动程序)时,如何自动执行文件下载? - How to automate file downloads, while using chrome emulator (chrome driver) with device apple iPhone 5? 在webdriver中打开Firefox驱动程序时出现以下错误 - While opening the Firefox driver in webdriver am getting below error 我正在使用 Selenium Web 驱动程序并使用 chrome 与我想设置下载位置但失败 - i am using Selenium web driver and using chrome with with i want to set download location but failed 驱动初始化后如何在运行时更改Webdriver的下载路径? - How can I change the download path of the Webdriver at runtime after initialization of the driver? Firefox的Firefox驱动程序或Chrome驱动程序出现问题 - Having issues with firefox driver or chrome driver for selenium 如何在硒中动态更改Firefox驱动程序首选项 - How to dynamically change Firefox Driver preferences in selenium 如何在Selenium Webdriver 3中为Firefox驱动程序设置默认配置文件? - How can I set a default profile for the Firefox driver in Selenium Webdriver 3? 如何在最新的Chrome驱动程序Ver:2.40中启用Headless Chrome中的下载文件 - How to enable Download file in Headless Chrome in the latest Chrome Driver Ver: 2.40
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM