how to handle file downlaod for selenium webdriver

2019-02-16 02:54发布

问题:

I would like to know it there is any desired capability or options setting for Safari selenium webdriver to save file to specific location Similar as we do it for firefox driver.

Also want to disable the popup for file save dialog.

Regards, Himanshu

回答1:

You can check the below link. dataDir mar do you work but not sure

http://code.google.com/p/selenium/wiki/DesiredCapabilities#Safari_specific

Try this

Selenium sel = new DefaultSelenium("localhost", 4444, "*safari", baseURL);
CommandExecutor executor = new SeleneseCommandExecutor(sel);
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability("safari.options.dataDir", "your folder path");
WebDriver browser = new RemoteWebDriver(executor, dc);

NEW UPDATE : TO CLOSE THE SAVE DOWNLOAD WARNING

WebElement link = driver.findElement(By.xpath("myxpath"));
clickAndSaveFileIE(link);


public static void clickAndSaveFileIE(WebElement element) throws InterruptedException{
    try {
            Robot robot = new Robot();
             //get the focus on the element..don't use click since it stalls the driver          
            element.sendKeys("");
            //simulate pressing enter            
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
            //wait for the modal dialog to open            
            Thread.sleep(2000);
            //press s key to save            
            robot.keyPress(KeyEvent.VK_S);
            robot.keyRelease(KeyEvent.VK_S);
            Thread.sleep(2000);
            //press enter to save the file with default name and in default location
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
        } catch (AWTException e) {

            e.printStackTrace();
        }