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

问题描述 投票:0回答:1

操作系统:OSX macOS Sierra 10.12.6

硒版本:3.6.0

浏览器:Safari

浏览器版本:11.0.1(12604.3.2)

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

实际行为 - 在 Safari 浏览器上执行 button.click() 操作时没有任何操作

重现步骤 -

  1. 打开 safari 浏览器
  2. 访问网络应用程序登录屏幕
  3. 输入有效凭据并单击登录按钮,出现弹出窗口
  4. 切换到iframe
  5. 单击放置在 iframe 内的按钮

单击按钮时无任何操作。相同的代码在其他浏览器(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>

macos selenium-webdriver safari webdriver
1个回答
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')]")));
    

虽然外部 HTML 元素确实具有

class
属性(如
dijitDialogPaneContent
),但没有
style
属性(如
z-index: 950;
)。

  1. 线路:

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

当您使用

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

如果这个

<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();
© www.soinside.com 2019 - 2024. All rights reserved.