如何在使用 chrome 驱动程序/firefox 驱动程序时更改 Webdriver 中的文件下载位置 [英] how to change file download location in Webdriver while using chrome driver/firefox driver

查看:45
本文介绍了如何在使用 chrome 驱动程序/firefox 驱动程序时更改 Webdriver 中的文件下载位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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. I went through the similar questions asked on this forum but non of them helped so far.

代码是-

对于 Firefox-

For 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 中的大写字母).

secondly, you are setting preference browser.download.folderlist, it is browser.download.folderList (L caps in folderList).

一旦你做到了这两点,你就可以使用你的 Robot 类来执行所需的操作.

Once you have achieved this both, you can use then your Robot class to perform desired operations.

对于 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. :)

这篇关于如何在使用 chrome 驱动程序/firefox 驱动程序时更改 Webdriver 中的文件下载位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆