无法使用Selenium单击Safari浏览器中iframe内的按钮

时间:2017-10-18 09:04:55

标签: macos selenium safari webdriver

操作系统:OSX macOS Sierra 10.12.6

Selenium版本:3.6.0

浏览器:Safari

浏览器版本:11.0.1(12604.3.2)

预期行为 - click()操作应该适用于Safari浏览器中iframe内的按钮

实际行为 - 在Safari浏览器上执行button.click()操作时无动作

重现的步骤 -

  1. 打开safari浏览器
  2. 访问网络应用程序登录屏幕
  3. 输入有效凭据并单击“登录”按钮,弹出
  4. 切换到iframe
  5. 点击iframe内的按钮
  6. 单击按钮时无法执行任何操作。相同的代码在其他浏览器(Chrome,Mozilla,IE11)中运行良好

    以下是代码:

    SafariOptions options = new SafariOptions();
    options.setUseCleanSession(true);
    driver = new SafariDriver(options); 
    driver.manage().window().maximize();
    driver.get("testurl");
    driver.findElement(By.id("UN")).sendKeys("admin");
    driver.findElement(By.id("UN")).sendKeys("admin$");
    driver.findElement(By.id("Login")).click();
    driver.switchTo().frame(driver.findElement(By.xpath("//*[starts-with(@class,'dijitDialog') and contains(@style,'z-index: 950;')]//*[starts-with(@id,'AxDojo_Dialog_frame')]")));
    driver.findElement(By.xpath("//*[@id='ButtonPlaceHolder']/button[2]")).click();
    

    执行最后一步时无动作。 随着解决方法找到一个替代执行单击按钮与下面的代码

    driver.findElement(By.xpath("//*@id='ButtonPlaceHolder']/button[2]")).sendKeys(Keys.ENTER);
    

    但是像这样我们需要在很多方面进行改变,Selenium是浏览器独立的,维护代码浏览器是我们的繁琐工作。请任何人都可以研究这个问题。

    请在iframe中找到HTML代码。

    <iframe id="AxDojo_Dialog_frame_1508320744505" name="AxDojo_Dialog_frame_1508320744505" style="border: 0px; width: 605px; height: 159px;" width="605" height="136px" scrolling="auto" frameborder="0" src="about:blank" axpageload="1"></iframe>
    #document
    <!DOCTYPE html>
    <html style="overflow-y: hidden"><head>
    <title>Login</title>
    <link href="../AxCommon/Styles/ECDefault/main.css" type="text/css" rel="stylesheet">
    <script language="javascript" type="text/javascript" src="../Packages/Scripts/JQuery/jQuery.js"></script>
    <script type="text/javascript" language="javascript" src="../AxCommon/Scripts/AxSystemBroadcast.js"></script>
    <script type="text/javascript" src="../AxCommon/Scripts/AxMyParent.js"></script>
    <script type="text/javascript" src="./Scripts/AxDialog.js?v=14082015"></script>
    <style type="text/css">
        .DialogBtn{width: 80px; font-size: 10pt;white-space:nowrap;}
        .DialogCustomBtn{font-size: 10pt;white-space:nowrap;}
        #popupMandatoryDiv { display:none;position:relative;height:auto;background:#FFFFFF;z-index:999999;overflow:auto;border:1px solid black;padding:5px 20px 10px 18px;font-size:12px;}
        li {list-style: none; }
        /*li:before {content:'\2022'; display: block; position: relative; max-width: 0px; max-height: 0px; left: -13px; top: -4px; color: red; font-size: 20px;padding-right: 5px;}*/
        li:before {content:'\2217'; display: block; position: relative; max-width: 0px; max-height: 0px; left: -13px; top: -2px; color: red;font-weight:bold}
    </style>
    </head>
    <body>
        <div id="divMask" style="height: auto; width: auto;">
            <table id="Table1" cellspacing="6" cellpadding="4" border="0">
                <tbody><tr>
                    <td rowspan="3" style="width: 3px; height: 5px" valign="top" align="center"></td>
                    <td colspan="2" valign="top" align="center" height="10"></td>
                </tr>
                <tr style="width:100%">
                    <td valign="top" align="center" width="10%">
                        <img id="IconImage" src="../AxCommon/Images/28 information.gif">
                    </td>
                    <td valign="middle" align="left" height="5" width="90%">
                        <div id="divPrompt" align="left" style="DISPLAY: block; FONT-SIZE: 9pt; OVERFLOW: hidden; DIRECTION: ltr;">You are already logged into Excelicare in other active session(s). Please choose one of the options below:</div>
                    </td>
                </tr>
                <tr style="width: 100%; display: none;">
                    <td valign="top" align="center" width="10%">
                    </td>
                    <td valign="top" align="left" height="5" width="90%">
                        <div id="popupMandatoryDiv" align="left" style="DISPLAY: block; FONT-SIZE: 9pt; OVERFLOW: auto; DIRECTION: ltr;display:none">
                        </div>
                    </td>
                </tr>
                <tr>
                    <td valign="middle" align="center" colspan="2" style="padding-top: 10px">
                        <div id="ButtonPlaceHolder"><button class="DialogCustomBtn" onclick="AxSysF_CloseWindow(6)">Open new session / <br>end other session(s)</button>&nbsp;&nbsp;
                        <button class="DialogCustomBtn" onclick="AxSysF_CloseWindow(7)">Open new session / <br>retain other session(s)</button>&nbsp;&nbsp;
                        <button class="DialogCustomBtn" onclick="AxSysF_CloseWindow(0)">Cancel login<br>&nbsp;</button>&nbsp;&nbsp;</div>
                    </td>
                </tr>
                <tr id="trchkPlaceHolder" style="visibility: hidden; display: none;">
                    <td>&nbsp;</td>
                    <td valign="middle" align="left" colspan="3" id="tdchkPlaceHolder">
                        <div id="checkPlaceHolder"></div>
                    </td>
                </tr>
            </tbody></table>
            <div>
                <br>
                <div id="lblInfo" style="display: block;"><b>Note : </b> If you choose to <i>Open new session/end other session(s)</i> any unsaved data in the other session(s) will be lost.</div>
            </div>
        </div>
        <input id="hdnTitle" type="hidden" value="Login">
    
    <script type="text/javascript">
        var strProductType = '<%=System.Configuration.ConfigurationSettings.AppSettings("ProductType")%>';
        strModulePageName = 'frmdialog';
        $(document).ready(function() { PageLoaded();setTimeout('self.focus()', 10);});
        function setCallback(CallbackFn) {
            _CallbackFn = CallbackFn;
        }
        document.oncontextmenu = function (event) {return false;};  
    </script>
    
    </body></html>
    

    Picture1

    Picture2

    JavaScript Console when application opened in Safari Private mode

1 个答案:

答案 0 :(得分:0)

根据您提供的屏幕截图/ HTML,您需要对代码块进行一些修改,如下所示:

  1. 该行:

    driver.switchTo().frame(driver.findElement(By.xpath("//*[starts-with(@class,'dijitDialog') and contains(@style,'z-index: 950;')]//*[starts-with(@id,'AxDojo_Dialog_frame')]")));
    
  2. 虽然外部HTML元素的class属性确实为dijitDialogPaneContent,但style属性没有z-index: 950;

    1. 该行:

      driver.findElement(By.xpath("//*[@id='ButtonPlaceHolder']/button[2]")).click();
      
    2. 当您使用带有索引的xpath时,例如button[2] xpath变得脆弱且易受攻击。我希望扩展<button>代码并使用<span>或其中的任何其他代码/文字来构建唯一的css / xpath

      1. 根据这两个观察结果,您可以对代码进行一些小的更改,如下所示:

        driver.switchTo().frame(driver.findElement(By.xpath("//div[@class='dijitDialogPaneContent')/iframe[starts-with(@id, 'AxDojo_Dialog_frame')]")));
        //clicking the first button
        driver.findElement(By.xpath("//button[@class='DialogCustomBtn' and contains(.,'end other session(s)')]")).click();
        
      2.   

        如果此<iframe>的加载顺序不变,我们的xpath将更加通用,如下所示:

            driver.switchTo().frame(driver.findElement(By.xpath("//div[@class='dijitDialogPaneContent')//following::iframe[1]")));
            //clicking the first button
            driver.findElement(By.xpath("//button[@class='DialogCustomBtn' and contains(.,'end other session(s)')]")).click();
        
相关问题