如何在使用chrome驱动程序/火狐驱动程序时更改Webdriver中的文件下载位置
2022-09-01 20:34:30
我正在尝试使用特定文件夹中的另存为选项来保存图像。我找到了一种方法,通过这种方法,我可以使用“另存为”选项右键单击要保存的图像。但是我陷入困境的问题是在获得os窗口后,该窗口询问保存文件的位置,我无法发送所需的位置,因为我不知道如何执行此操作。我浏览了在这个论坛上提出的类似问题,但到目前为止,它们都没有帮助。
代码是-
对于火狐浏览器-
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
}
}