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

时间:2018-05-22 20:20:33

标签: java html selenium-webdriver

如何找到内容为动态的元素。元素是表中的一个单元格,它动态获取其值。请参考以下示例代码:

<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>中三个<tr>中的一个。其他<tr>的{​​{1}}具有相同的ID。换句话说,一行中的三个单元格将具有三个ID。但其他行的单元格具有相同的三个ID。

如何找到<td><div>

中的任何一个

Dynamic display contents

以下是上述HTML代码的快照,我想找到listImport单元格。

2 个答案:

答案 0 :(得分:0)

鉴于HTML结构,您可以使用以下内容找到相关字段:

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

这是使用input的CSS属性选择器来定位它,其值设置为ListImport。如果失败,您可能需要执行以下操作:

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

然后循环遍历每个元素,评估字段的值以找到您要查找的元素。当在页面上识别它们没有什么独特之处时,定位元素并不是非常简单。

答案 1 :(得分:0)

如果您想找到任何人divinput,您可以使用以下选择器,然后根据您的要求进行修改,

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

您可以更改id值以从表中获取所需的值。包括在选择器前尝试trtd以获取所需的行。下面显示了一个示例(仅供参考,可能不正确),

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

tr[1] --> First row
td[1] --> First column
相关问题