3

如何使用 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");
4

3 回答 3

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();
    }
于 2013-10-01T13:21:43.727 回答
1

这段代码对我有用

WebElement filepath=driver.findElement(By.id("fileUploadId"));
filepath.sendKeys("C:\\TextFile.txt");
于 2014-06-02T07:16:33.420 回答
1

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

我们需要 :

jacob.jar下载

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

AutoItX4Java.jar下载

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


   }

谢谢...不要单击接受或赞成,直到它适合您。如果它不适合您的意思,请发表评论..不要投反对票...

于 2014-02-06T11:07:21.113 回答