【问题标题】:multiple modal dialog boxes, unable to select the top most one多个模态对话框,无法选择最上面的一个
【发布时间】:2017-02-17 05:11:19
【问题描述】:

我正在运行测试,但在管理模式对话框时遇到了意外问题。

用户在模态对话框中上传文件,如果该文件已存在于系统中,则会打开另一个单独的模态对话框,询问用户是否要覆盖现有文件。

我在操作第二个对话框上的按钮时遇到了困难。

当我管理第一个对话框时,我会执行以下操作:

void switch_to_dialog_window(WebDriver driver){

    driver.switchTo().frame(driver.findElement(By.cssSelector("div.d2l-dialog>div>iframe")));

}

两个对话框都打开时的 html 如下所示:

<div class="d2l-dialog" style="top: 70px; width: 700px; height: 520px; left: 630px; z-index: 1002;">
    <div class="d2l-dialog-inner" style="height: 518px;">
       <iframe class="d2l-dialog-frame" src="/d2l/common/dialogs/file/main.d2l?ou=11346&af=MyComputer%2cOuFiles%2cSharedFiles%2cgooglefiledownloader%2coffice365filedownloader&am=1&fsc=1&asc=0&mfs=0&afid=0&uih=&area=MyComputer&f=&path=%2fcontent%2fenforced%2f11346-Gherkin_Cucumber%2f&d2l_body_type=2" name="d2l_c_10_968" allowfullscreen="" scrolling="no" style="width: 698px; height: 518px; overflow: hidden;" frameborder="0"/>
    </div>
</div>

<div class="d2l-dialog" style="top: 90px; width: 475px; height: 415px; left: 800px; z-index: 1004; display: block;">
    <div class="d2l-dialog-inner" style="height: 413px;">
       <iframe class="d2l-dialog-frame" src="/d2l/lp/fileinput/11346/Duplicates?files=photo.jpg" name="d2l_c_1_182" allowfullscreen="" scrolling="no" style="width: 473px; height: 413px; overflow: hidden;" frameborder="0"/>
    </div>
</div> 

我正在尝试控制提及重复项的对话框。

我尝试将 switch_to_dialog_window 的方法修改为更具体(作为识别第一个对话框来控制它的测试):

 driver.switchTo().frame(driver.findElement(By.cssSelector("div.d2l-dialog>div>iframe[src^='/d2l/common/dialogs/file/main.d2l']")));

这不起作用,所以我无法实现这种方式来管理 2 个对话框。

我尝试切换到默认内容,然后使用“switch_to_dialog_window”方法切换回对话框,但这也不起作用。我只是尝试直接访问对话框上的按钮,但这不起作用:

public void confirm_duplicate() {



    //driver.findElement(By.xpath("//iframe[starts-with(@src, '/d2l/lp/fileinput/11346/Duplicates')]"));

    //driver.switchTo().frame(driver.findElement(By.cssSelector("div>div>iframe[src^='/d2l/lp/fileinput/11346/Duplicates']")));
    try{
        //driver.switchTo().frame(driver.findElement(By.cssSelector("div>div>iframe[name^='d2l_c_1_']")));
        driver.findElement(By.linkText("Update")).click();
    }catch(Exception e)
    {
        System.out.println("could not click on the Update button on the top most dialog box");
        e.printStackTrace();
    }

}

我似乎在这个圈子里跑来跑去,筋疲力尽。有人能解释一下如何控制这个最顶层的对话框吗?

你也可以教我为什么下面的表达式不起作用:

driver.switchTo().frame(driver.findElement(By.cssSelector("div.d2l-dialog>div>iframe[src^='/d2l/common/dialogs/file/main.d2l']")));

【问题讨论】:

    标签: java selenium modal-dialog selenium-chromedriver


    【解决方案1】:

    如果在切换到框架时出现问题,可以使用机器人 API 来处理最顶层的模态框。

    要使用 Robot API,请给出以下代码行

    Robot key = new Robot();
    key.keyPress(KeyEvent.VK_ENTER); 
    key.keyRelease(KeyEvent.VK_ENTER);
    

    确保控件已经在预期的按钮上。如果焦点在 modal 上的另一个按钮上,则给 'tab' 或其他 keyevent 以获取更新按钮上的控件。

    【讨论】:

    • 感谢@neetigya 我曾希望避免将其用作解决方案。但是,最好有一个解决方案使我能够直接访问 iframe 上的元素。作为一种解决方法,这项工作虽然非常感谢,但我没想过使用机器人。
    【解决方案2】:

    我已经从自动化中休息了几个月。碰巧对自己的脚本进行了一些维护,发现了这个问题的根本原因。

    问题不在于我的代码,代码很好,只是我忘记了一个关键步骤。在业务流程中,打开了第二个需要用户输入的窗口。我用代码切换到这个弹窗。:

    public void get_window_ids(){
    
        //Get the handles for the main window and the popup window for the upload button
                try {
    
                    Set<String> AllWindowHandles = driver.getWindowHandles();
                    System.out.println(AllWindowHandles.size()+  " distinct windows: " + AllWindowHandles);
                    window1 = (String) AllWindowHandles.toArray()[0];
                    System.out.println("\nwindow 1 is " + window1+"\n");
                    window2 = (String) AllWindowHandles.toArray()[1];
                    System.out.println("\nwindow 2 is " + window2+"\n");
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    }
    
    public String getWindow1() {
        return window1;
    }
    
    public String getWindow2() {
        return window2;
    }
    

    当我切换到 window2 时,我忘记切换回第一个窗口。我只是错过了一个我忘记的弹出窗口的事实。因此,如果您使用这些模式弹出窗口,请记住将控制权返回到主窗口。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-14
      • 2021-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-02
      • 1970-01-01
      • 2011-09-22
      相关资源
      最近更新 更多