代码之家  ›  专栏  ›  技术社区  ›  Gaurav Thantry

如何在显示文本为动态的网页上定位元素

  •  1
  • Gaurav Thantry  · 技术社区  · 8 年前

    如何定位内容是动态的元素。元素是表中动态获取其值的单元格。请参考以下示例代码:

    <tr class="oddrow " style="visibility: visible;">
       <td align="center"><input type="checkbox" onclick="WebForm.markRowForSelection(event)" id="check_ProcessDefinitionTable" xformstype="checkbox" selectnotifier="true" databoundelement="true" style="visibility: visible;"></td>
       <td>
          <div class="fieldsbox" id="xfe2" style="visibility: visible;"><input readonly="" isoutputcontrol="true" xformstype="output" id="clmFolder" xql="tns:Folder" databoundelement="true" __parent="ProcessDefinitionTable" class="input output left_align" absolutexpath="tns:Folder" datatype="string" validate="true" doebivalidate="false" title="Value for Folder." style="" ref="tns:Folder" xmlns:wcpforms="http://xyz.xyz.com/wcp/xforms" _intable="true" maxlength="999999999"></div>
       </td>
       <td>
          <div class="fieldsbox" id="xfe4" style="visibility: visible;"><input readonly="" isoutputcontrol="true" xformstype="output" id="clmShortname" xql="tns:ShortName" databoundelement="true" __parent="ProcessDefinitionTable" class="input output left_align" absolutexpath="tns:ShortName" datatype="string" validate="true" doebivalidate="false" title="Value for Name." style="" ref="tns:ShortName" xmlns:wcpforms="http://xyz.xyz.com/wcp/xforms" _intable="true" maxlength="999999999"></div>
       </td>
       <td>
          <div class="fieldsbox" id="xfe6" style="visibility: visible;"><input readonly="" isoutputcontrol="true" xformstype="output" id="clmDescription" xql="tns:Description" databoundelement="true" __parent="ProcessDefinitionTable" class="input output left_align" absolutexpath="tns:Description" datatype="string" validate="true" doebivalidate="false" title="Value for Description." style="" ref="tns:Description" xmlns:wcpforms="http://xyz.xyz.com/wcp/xforms" _intable="true" maxlength="999999999"></div>
       </td>
       <td>
          <div class="fieldsbox" id="xfe8" style="visibility: visible;"><input readonly="" isoutputcontrol="true" xformstype="output" id="clmModelspace" xql="tns:ModelSpace" databoundelement="true" __parent="ProcessDefinitionTable" class="input output left_align" absolutexpath="tns:ModelSpace" validate="true" datatype="string" doebivalidate="false" displayformat="text" title="Value for Published To." style="" ref="tns:ModelSpace" _intable="true" maxlength="999999999"></div>
       </td>
    </tr>
    <tr class="evenrow highlight" style="visibility: visible;">
       <td align="center"><input type="checkbox" onclick="WebForm.markRowForSelection(event)" id="check_ProcessDefinitionTable" xformstype="checkbox" selectnotifier="true" databoundelement="true" style="visibility: visible;"></td>
       <td>
          <div class="fieldsbox" id="xfe2" style="visibility: visible;"><input readonly="" isoutputcontrol="true" xformstype="output" id="clmFolder" xql="tns:Folder" databoundelement="true" __parent="ProcessDefinitionTable" class="input output left_align" absolutexpath="tns:Folder" datatype="string" validate="true" doebivalidate="false" title="Value for Folder." style="" ref="tns:Folder" xmlns:wcpforms="http://xyz.xyz.com/wcp/xforms" _intable="true" maxlength="999999999"></div>
       </td>
       <td>
          <div class="fieldsbox" id="xfe4" style="visibility: visible;"><input readonly="" isoutputcontrol="true" xformstype="output" id="clmShortname" xql="tns:ShortName" databoundelement="true" __parent="ProcessDefinitionTable" class="input output left_align" absolutexpath="tns:ShortName" datatype="string" validate="true" doebivalidate="false" title="Value for Name." style="" ref="tns:ShortName" xmlns:wcpforms="http://xyz.xyz.com/wcp/xforms" _intable="true" maxlength="999999999"></div>
       </td>
       <td>
          <div class="fieldsbox" id="xfe6" style="visibility: visible;"><input readonly="" isoutputcontrol="true" xformstype="output" id="clmDescription" xql="tns:Description" databoundelement="true" __parent="ProcessDefinitionTable" class="input output left_align" absolutexpath="tns:Description" datatype="string" validate="true" doebivalidate="false" title="Value for Description." style="" ref="tns:Description" xmlns:wcpforms="http://xyz.xyz.com/wcp/xforms" _intable="true" maxlength="999999999"></div>
       </td>
       <td>
          <div class="fieldsbox" id="xfe8" style="visibility: visible;"><input readonly="" isoutputcontrol="true" xformstype="output" id="clmModelspace" xql="tns:ModelSpace" databoundelement="true" __parent="ProcessDefinitionTable" class="input output left_align" absolutexpath="tns:ModelSpace" validate="true" datatype="string" doebivalidate="false" displayformat="text" title="Value for Published To." style="" ref="tns:ModelSpace" _intable="true" maxlength="999999999"></div>
       </td>
    </tr>

    上面 <td> 是三分之一 <TD & GT; A中的S <tr> . 其他的 <TR & GT; <TD & GT; 具有相同ID的。换句话说,一行中的三个单元格将具有三个ID。但其他行的单元格具有相同的三个ID。

    我该如何定位 <div> <input>

    Dynamic display contents

    下面是上述HTML代码的快照,我想定位lisimport单元格。

    2 回复  |  直到 8 年前
        1
  •  0
  •   kripindas    8 年前

    如果你想找到任何人 div input ,可以使用下面的选择器,可以根据需要进行修改,

    WebElement target = driver.findElement(By.xpath("//div[@class='fieldsbox']//input[@id='clmFolder']"));
    

    你可以改变 id 从表中获取所需值的值。包括尝试 tr td 在选择器之前,以获得所需的行。示例如下(仅供参考,可能不正确)。

    WebElement target = driver.findElement(By.xpath("//tr[1]//td[1]//div[@class='fieldsbox']//input[@id='clmFolder']"));
    
    tr[1] --> First row
    td[1] --> First column
    
        2
  •  0
  •   Justin Pearce    8 年前

    对于HTML结构,您可能能够定位具有以下问题的字段:

    WebElement target = driver.findElement(By.cssSelector("input[value='ListImport']"));
    

    这将使用CSS属性选择器 input 按一个值定位 ListImport . 如果失败了,你可能需要做如下的事情:

    List<WebElement> inputs = driver.findElements(By.tagName('input'));
    

    然后循环遍历每个元素,评估字段的值以找到您要查找的元素。当页面上没有唯一的元素来标识它们时,目标元素就不是很简单了。