IE运行时找不到Selenium元素

时间:2010-06-29 09:50:42

标签: c# selenium

Firefox通过的硒测试。 但是当我试图通过IE运行它时,我收到错误“Element xpath ... not found”

string xpath = string.Format("xpath=(//table[@id='{0}']/tbody/tr[not(contains(@class,'{1}'))])[{2}]",tableId, CLASS_HIDDENROW,rowNumber);
string rowid = Selenium.GetAttribute(string.Format("{0}/@id",xpath));

我调试了它,发现Selenium.GetXpathCount("//table[@id='328829']")返回0,这意味着IE甚至无法找到带有id的表。

但是当我在IE上使用“View Source”时,我找到了以下脚本

<table id="328829" class="scroll" cellpadding="0" cellspacing="0" style="z-index:1;"></table>

更多信息 源代码(* .aspx)

<div id="<%=gridDef.gridId%>Wrapper" class="<%If Model.gridDefinitions.Count = 1 Then %>SummaryDiv<%Else%>HiddenRow<%End If %>" class="HiddenRow" style="position:relative;overflow:auto;overflow-y:hidden;">
            <table width="100%">
                <tr>
                    <td><span style="font-weight:bold;font-size:14px;color:#38AEF2"><%=gridDef.caption%></span></td>
                    <td align="right"><a style="cursor:pointer" onclick="$('#<%=gridDef.gridId%>tableWrapper').toggle();return false;"><%="vbeiHide/Show".TranslateHTML()%></a></td>
                </tr>
            </table>
            <br />
            <div id="<%=gridDef.gridId%>tableWrapper" name="<%=gridDef.gridId%>tableWrapper">
                <table id="<%=gridDef.gridId%>" class="scroll" cellpadding="0" cellspacing="0" style="z-index:1;"></table>
                <input type="image" src='../../Images/add2.gif' style="width:1px;height:1px;"/>
                <input id="<%=gridDef.gridId%>_Create_Image" type="image" src="../../Images/add2.gif" title="<%="vbeiCreateANewSample".Translate() %>" onfocus="duplicateRow('#<%=gridDef.gridId%>','<%=gridDef.newTemplateId%>'); this.blur(); return false;" />
            </div>
        </div>

运行时的HTML

          <div id="328829Wrapper" class="HiddenRow" class="HiddenRow" style="position:relative;overflow:auto;overflow-y:hidden;">
            <table width="100%">
                <tr>
                    <td><span style="font-weight:bold;font-size:14px;color:#38AEF2">Polluted Soil</span></td>
                    <td align="right"><a style="cursor:pointer" onclick="$('#328829tableWrapper').toggle();return false;">Hide / Show</a></td>
                </tr>
            </table>
            <br />
            <div id="328829tableWrapper" name="328829tableWrapper">
                <table id="328829" class="scroll" cellpadding="0" cellspacing="0" style="z-index:1;"></table>
                <input type="image" src='../../Images/add2.gif' style="width:1px;height:1px;"/>
                <input id="328829_Create_Image" type="image" src="../../Images/add2.gif" title="Create a new sample" onfocus="duplicateRow('#328829','template_328829'); this.blur(); return false;" />
            </div>
        </div>
        <script type="text/javascript">
            jQuery("#328829").jqGrid({
                url: "/Ordering/LoadData?gridId=328829",
                editurl: "clientArray",
                datatype: "json",
                hoverrows : false,    
                mtype: 'POST',
                loadui : "disable",
                colNames: ['Options','Sample code (*)','Sample comments','SS Date Time Field (*)','IsTemplate','favourite name','favourite isShared','favourite sampleId'],
                colModel: [{name: 'Options', index: 'Options', editable: false, hidden: false, sortable: false, resizable: false, edittype: 'text', width: 90, formatter:optionsFormatter },{name: 'Sample_Name', index: 'Sample_Name', editable: true, hidden: false, sortable: false, resizable: false, edittype: 'text', width: 150, editoptions: {maxlength: 50} },{name: 'Sample_Comments', index: 'Sample_Comments', editable: false, hidden: true, sortable: false, resizable: false, edittype: 'text' },{name: '51455962', index: '51455962', editable: true, hidden: false, sortable: false, resizable: false, edittype: 'text', editoptions: { dataInit:function(elem){attachDateTimePicker(elem);} } },{name: 'IsTemplate', index: 'IsTemplate', editable: false, hidden: true, sortable: false, resizable: false, edittype: 'checkbox' },{name: 'favourite_name', index: 'favourite_name', editable: false, hidden: true, sortable: false, resizable: false, edittype: 'text' },{name: 'favourite_isShared', index: 'favourite_isShared', editable: false, hidden: true, sortable: false, resizable: false, edittype: 'checkbox' },{name: 'favourite_sampleId', index: 'favourite_sampleId', editable: false, hidden: true, sortable: false, resizable: false, edittype: 'text' }],
                imgpath: "/css/redmond/images",
                height: "100%",
                rowNum:-1,
                altRows: true,
                loadComplete: function() { loadComplete("#328829", 3);},
                afterInsertRow: function(rowid, rowdata, rowelem) { rowInserted("#328829", rowid, rowdata); }
            });

        </script>      

我还尝试在安装时添加以下脚本,但它不起作用。

if (browser == Browser.IE) {
    selenium.UseXpathLibrary("javascript-xpath");
    selenium.AllowNativeXpath("true");
}

有人能告诉我如何解决这个问题吗?顺便说一句,代码是C#。

1 个答案:

答案 0 :(得分:0)

你的调试行中有一个拼写错误,所以如果这是你使用的实际行,即使对于Firefox,它也会返回0。您可以在IE中尝试以下内容,看看是否可以看到表:selenium.isElementPresent("id=328829")

您可以尝试的另一件事是稍微改变您的XPath。如果没有看到更多的HTML,很难提出建议,但您可以尝试以下方法:

xpath=id('328829')/descendant::tr[not(contains(@class,'hidden'))][1]

根据所测试的浏览器,我发现tbody不存在的问题,因此使用/descendant::tr可能有所帮助。