我试图通过在特定文件夹中使用另存为选项来保存图像。我找到了一种方法,通过另存为选项,我可以右键单击要保存的图像。但是我遇到的问题是在获得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
对于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
}
}
答案 0 :(得分:20)
代码中有两件事情出错。
对于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 (在文件夹列表中为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);
希望这会有所帮助。 :)
答案 1 :(得分:2)
我花了很多时间研究如何在没有另存为弹出窗口的情况下在firefox浏览器中下载pdf文件。它会帮助某人。
经过一些本地调查后,如何在没有任何另存为弹出窗口的情况下在Firefox中下载 pdf 文件,我在firefox配置文件中找到了所需的最低首选项:
profile.setPreference("pdfjs.disabled", true);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
当然,您可以添加一些其他偏好。
适用于Firefox 45-46版本。
答案 2 :(得分:2)
Chrome浏览器:
即使您可以使用以下代码段禁用Windows对话框(另存为对话框)。您需要在chromedriver首选项中进行以下设置:
接受浏览器中的任何证书
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);
答案 3 :(得分:0)
可能不是最好的解决方案,但您可以尝试使用sikuli api来确认显示的框的保存。
“另存为”框是一个操作系统窗口。
答案 4 :(得分:0)
您已经部分回答了自己的问题:
我遇到的问题是在获得操作系统窗口之后
Selenium是浏览器自动化工具 - 操作系统窗口不是浏览器!您将需要使用其他东西。根据您的需求,有很多选择:Sikuli,Robot,AutoIt,......
答案 5 :(得分:0)
使用相同的Robot类并按Enter键选择&#34; Save&#34;在Windows对话框中。
<强> robo.keyPress(KeyEvent.VK_ENTER); robo.keyRelease(KeyEvent.VK_ENTER); 强>
如果你需要重命名,请复制剪贴板中的文件名,然后传递如下
$dial->conference('My conference', array(
'startConferenceOnEnter' => True,
'endConferenceOnExit' => True
));
答案 6 :(得分:0)
对于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);