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

2024-05-07 03:42:32 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用SeleniumWebDriver处理文件窗口弹出

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

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

Tags: 文件namebrowserbydriverwindow按钮el
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();
    }

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

我们需要:

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


   }

谢谢。。。不要单击“接受”或“向上投票”,直到它对您有效。如果它不适用于您的意思,请评论。。不要投反对票

这个代码对我有用

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

相关问题 更多 >