3

How to handle file window popup using selenium webdriver.

I have clicked on file browse button , new pop up window has been opened and i am unabled to handle this window(as i want to select the file)

 WebElement browser=driver.findElement(By.name("uploadFile"));
browser.click();
driver.switchTo().window("File Upload");
WebElement el=driver.findElement(By.name("fileName"));
el.sendKeys("E:\\DVBScheduleEvent.xml");
1
  • Don't click the upload button. Just do driver.findElement(By.name("uploadFile")).sendKeys("E:\\DVBScheduleEvent.xml"); Commented Oct 1, 2013 at 13:34

3 Answers 3

3

I have a sample code for this:

driver.get("http://www.2shared.com/");
        driver.findElement(By.id("upField")).sendKeys("D:\\james.xls");
        driver.findElement(By.xpath("//input[@title='Upload file']")).click();
    }
Sign up to request clarification or add additional context in comments.

Comments

1

This code worked for me

WebElement filepath=driver.findElement(By.id("fileUploadId"));
filepath.sendKeys("C:\\TextFile.txt");

Comments

1

Use this Method for file handling:

We need :

jacob.jar Download

It will contain one jar file and 2 .dll files

AutoItX4Java.jar Download

public static void uploadFile(String path, String browser){
    
    if(browser.equalsIgnoreCase("chrome")){
    
        if(x.winWaitActive("Open", "", 10)){
            if(x.winExists("Open")){
                x.sleep(500);
                x.send(path);
                x.controlClick("Open", "", "Button2");
                
            }
        }
        
    }
    
    
    if(browser.equalsIgnoreCase("firefox")){
        
        if(x.winWaitActive("File Upload", "", 10)){
            if(x.winExists("File Upload")){
                x.sleep(500);
                x.send(path);
                x.controlClick("File Upload", "", "Button2");
                
            }
        }
    }
    
    if(browser.equalsIgnoreCase("InternetExplorer")){
        
        if(x.winWaitActive("Choose File to Upload", "", 10)){
            if(x.winExists("Choose File to Upload")){
                x.sleep(500);
                x.send(path);
                x.controlClick("Choose File to Upload", "", "Button2");
                
            }
        }
    }
    
    
  
}


   public void test(){
       //Click on the Select button of the file upload
       uploadFile("Path", "chrome");


   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.