Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在使用chrome driver/firefox驱动程序时更改Webdriver中的文件下载位置_Java_File Upload_Selenium Webdriver_Download_Automated Tests - Fatal编程技术网

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

Java 如何在使用chrome driver/firefox驱动程序时更改Webdriver中的文件下载位置,java,file-upload,selenium-webdriver,download,automated-tests,Java,File Upload,Selenium Webdriver,Download,Automated Tests,我试图通过在特定文件夹中使用“另存为”选项来保存图像。我找到了一种方法,可以使用“另存为”选项右键单击要保存的图像。但我遇到的问题是,在操作系统窗口询问文件保存位置后,我无法发送到所需的位置,因为我不知道如何发送。我在这个论坛上讨论了一些类似的问题,但到目前为止,没有一个得到了帮助 代码是- 对于Firefox- public class practice { public void pic() throws AWTException{ WebDriver driver;

我试图通过在特定文件夹中使用“另存为”选项来保存图像。我找到了一种方法,可以使用“另存为”选项右键单击要保存的图像。但我遇到的问题是,在操作系统窗口询问文件保存位置后,我无法发送到所需的位置,因为我不知道如何发送。我在这个论坛上讨论了一些类似的问题,但到目前为止,没有一个得到了帮助

代码是-

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

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


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

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

我遇到的问题是在获得操作系统窗口之后


Selenium是一个浏览器自动化工具-操作系统窗口不是浏览器!你需要用别的东西。根据您的需要,有很多选择:Sikuli、Robot、AutoIt等等。代码中有两个地方出错

对于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大写)

一旦实现了这两者,就可以使用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);
String downloadFilepath=“/path/to/download”;
HashMap chromePrefs=新HashMap();
chromePrefs.put(“profile.default\u content\u settings.popups”,0);
chromePrefs.put(“download.default\u目录”,downloadFilepath);
ChromeOptions选项=新的ChromeOptions();
选项。设置实验选项(“prefs”,chromePrefs);
DesiredCapabilities=DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);
上限设置能力(色度选项、能力、选项);
WebDriver=新的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);
    
    String downloadFilepath=“/path/to/download/directory/”;
    映射首选项=新哈希表();
    preferences.put(“profile.default\u content\u settings.popups”,0);
    preferences.put(“download.prompt_for_download”,“false”);
    preferences.put(“download.default_目录”,downloadFilepath);
    //禁用flash和PDF查看器
    preferences.put(“plugins.plugins_disabled”),新字符串[]{
    “Adobe Flash Player”、“Chrome PDF查看器”});
    ChromeOptions选项=新的ChromeOptions();
    选项。设置实验选项(“首选项”,首选项);
    DesiredCapabilities=DesiredCapabilities.chrome();
    Capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);
    能力。设置能力(ChromeOptions.CAPABILITY,选项);
    驱动程序=新的色度驱动程序(功能);
    

使用相同的机器人类,然后按enter键在Windows对话框中选择“保存”

机器人按键(KeyEvent.VK_ENTER); 机器人钥匙释放(钥匙事件.VK_输入)

如果需要重命名,请复制剪贴板中的文件名,并按如下方式传递

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);
String downloadFilepath=“/path/to/download”;
HashMap chromePrefs=新HashMap();
chromePrefs.put(“profile.default\u content\u settings.popups”,0);
chromePrefs.put(“download.default\u目录”,downloadFilepath);
ChromeOptions选项=新的ChromeOptions();
选项。设置实验选项(“prefs”,chromePrefs);
WebDriver=新的ChromeDriver(选项);

是否可以使用Robot更改目标文件夹位置?它现在在Firefox上工作,但在使用Chome驱动程序时如何更改?我是否可以使用AutoIT实现此目的?是否必须将AutoIT与TestNG一起使用,或者我可以将AutoIT与selenium webdriver一起使用aloneAutoIT,直到目前为止,这是最好的方法。机器人的问题是,你需要在执行系统时冻结系统。AutoIT消除了这个问题,但在使用AutoIT时,请记住系统不应转到挂起状态。是否使用testng或selenium webdriver进行AutoIT?AutoIt用作进程调用,因此它与testng或selenium没有关系。也许您想对路径中的斜杠使用File.separator?