簡體   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