【问题标题】:Handle dialog box in Selenium Web Driver [duplicate]Selenium Web 驱动程序中的句柄对话框 [重复]
【发布时间】:2015-08-18 11:07:22
【问题描述】:

我正在尝试在我的应用程序中自动执行下载过程。

最后一步是一个对话框。

我希望Selenium 在对话框中选择“保存文件”单选按钮,然后单击“确定”按钮。

这在 Selenium Webdriver 中可行吗?

注意:“警报”不起作用。

它既不是窗口也不是警报,所以我无法切换到这个对话框。

任何帮助将不胜感激。

我看到的对话框和

一样

WebDriver driver = new FirefoxDriver(); driver.get("http://selenium-release.storage.googleapis.com/2.47/selenium-java-2.47.1.zip");

我已经尝试过 getWindowHandle() 方法来切换到对话框。但是没有用

【问题讨论】:

  • 分享你迄今为止尝试过的代码。
  • 发送键应该与对话盒数组一起使用,但共享代码是最佳做法。
  • 对话框是系统对话框还是 HTML 对话框?您能否右键单击对话框并查看右键单击网页时会找到的典型菜单项?如果是这样,它就是一个 HTML 对话框,可以像任何其他页面一样进行交互。我们需要更多信息来帮助您。您能否分享一个指向该页面的链接或启动该对话框的 HTML?
  • @JeffC,该对话框是基于 Windows 的对话框,而不是 HTML 对话框。我看到的对话框与 WebDriver driver = new FirefoxDriver(); 相同driver.get("selenium-release.storage.googleapis.com/2.47/…);
  • @Musakkhir Sayyed 我尝试使用代码切换到对话框 (String childWindow1 : driver.getWindowHandles()) { driver.switchTo().window(childWindow1); } 。但这不起作用,因为对话框不是单独的窗口。我看到的对话框和我看到的对话框一样 WebDriver driver = new FirefoxDriver(); driver.get("selenium-release.storage.googleapis.com/2.47/…);

标签: java selenium-webdriver


【解决方案1】:

您可以使用此代码下载文件,此处设置了所有弹出窗口和警报,以便您可以自动化下载过程:

public class fileUse {

    public static String downloadPath = "D:\\downloads";

    @Test
    public void testDownload() throws Exception {
        WebDriver driver = new FirefoxDriver(FirefoxDriverProfile());   
        driver.manage().window().maximize();
        driver.get("FILE URL");
    }

    public static FirefoxProfile FirefoxDriverProfile() throws Exception {
        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference("browser.download.folderList", 2);
        profile.setPreference("browser.download.manager.showWhenStarting", false);
        profile.setPreference("browser.download.dir", downloadPath);
        profile.setPreference("browser.helperApps.neverAsk.openFile",
                "text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
        profile.setPreference("browser.helperApps.alwaysAsk.force", false);
        profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
        profile.setPreference("browser.download.manager.focusWhenStarting", false);
        profile.setPreference("browser.download.manager.useWindow", false);
        profile.setPreference("browser.download.manager.showAlertOnComplete", false);
        profile.setPreference("browser.download.manager.closeWhenDone", false);
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");
            return profile;
        }
    }

不要忘记导入org.testng.annotations.Test

【讨论】:

  • 您好 Aakash,单选按钮位于基于 Windows 的对话框中,类似于“打印”对话框。所以这不是一个 web 元素,不能被 driver.findElement() 方法访问
  • 是的,您可以使用它.. 只需添加切换窗口查询就完成了.. 您可以添加切换当前执行窗口的切换窗口,然后您可以使用此 driver.findElement() 方法。 .检查我已经更新的答案]
  • 它抛出 NoSuchWindowException 因为这里的对话框不是一个单独的窗口。如果您运行我在更新问题中给出的代码,您可以看到该对话框
  • 这不适用于我的应用程序。我仍然得到相同的对话框。我在另一个网站上测试了代码,它工作正常。但这对我不起作用! :(
  • 把详细信息发邮件给我,我会尽力解决你的问题
【解决方案2】:

其实它是依赖于浏览器的,你使用的是 Firefox

使用 Firefox 配置文件下载您的文件。此配置文件跳过了 Firefox 的对话框。 在线:-

   pro.setPreference("browser.downLoad.folderList", 0);

browser.download.folderList 的值可以设置为 0、1 或 2。当设置为 0 时,Firefox 会将通过浏览器下载的所有文件保存在用户桌面上。设置为 1 时,这些下载内容存储在 Downloads 文件夹中。设置为 2 时,将再次使用为最近下载指定的位置。

您需要实现的 Firefox 配置文件代码:-

        FirefoxProfile pro=new FirefoxProfile();
        pro.setPreference("browser.downLoad.folderList", 0);
        pro.setPreference("browser.helperApps.neverAsk.saveToDisk", "Applications/zip");
        WebDriver driver=new FirefoxDriver(pro);
        driver.get("http://selenium-release.storage.googleapis.com/2.47/selenium-java-2.47.1.zip");

希望对你有帮助:)

【讨论】:

    【解决方案3】:

    下载警报是基于 Windows 的警报。它不是由浏览器放置的。因此,您无法使用 Selenium WebDriver 将其自动化。您可以使用 AutoIT 或 Robot 框架来自动化基于窗口的弹出窗口。

    【讨论】:

      【解决方案4】:

      请使用 Auto IT 工具并通过它自动化下载窗口部分。 使用 javaScriptExecutor 在您的脚本中调用 AutoIT Sciprt。

      希望它会起作用!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-03
        • 2016-10-24
        • 2013-04-07
        • 1970-01-01
        • 2013-10-16
        • 1970-01-01
        相关资源
        最近更新 更多