如何使用selenium webdriver处理Windows文件浏览窗口

时间:2013-10-01 12:37:57

标签: selenium

如何使用selenium webdriver处理文件窗口弹出窗口。

我点击了文件浏览按钮,新的弹出窗口已打开,我无法处理此窗口(因为我想选择文件)

 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");

3 个答案:

答案 0 :(得分:3)

我有一个示例代码:

driver.get("http://www.2shared.com/");
        driver.findElement(By.id("upField")).sendKeys("D:\\james.xls");
        driver.findElement(By.xpath("//input[@title='Upload file']")).click();
    }

答案 1 :(得分:1)

使用此方法进行文件处理:

我们需要:

jacob.jar Download

它将包含一个jar文件和2个.dll文件

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");


   }

谢谢...不要点击接受或Upvote直到它适合你。如果它不适合你,请评论..不要Downvote ......

答案 2 :(得分:1)

这段代码对我有用

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