【问题标题】:How to handle Windows file browse window using selenium webdriver如何使用 selenium webdriver 处理 Windows 文件浏览窗口
【发布时间】:2021-07-12 21:24:28
【问题描述】:

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

【问题讨论】:

  • 不要点击上传按钮。就做driver.findElement(By.name("uploadFile")).sendKeys("E:\\DVBScheduleEvent.xml");

标签: selenium


【解决方案1】:

我有一个示例代码:

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

【讨论】:

    【解决方案2】:

    使用此方法处理文件:

    我们需要:

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

    谢谢...在对您有用之前,请勿单击接受或支持。如果它不适合您的意思,请发表评论.. 不要投反对票...

    【讨论】:

      【解决方案3】:

      这段代码对我有用

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-24
        • 1970-01-01
        • 2015-07-09
        • 1970-01-01
        相关资源
        最近更新 更多