如何定位内容动态的元素。元素是表中的一个单元格,它动态获取其值。请引用下面的示例代码:
<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>
有<td>
具有相同的 id。换句话说,一行中的三个单元格将具有三个 id。但其他行的单元格具有相同的三个 ID。
如何找到 <div>
中的任何一个或<input>
下面是上述 HTML 代码的快照,我想找到 listImport 单元格。
最佳答案
如果你想定位任何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
关于java - 如何在显示文本为动态的网页上定位元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50475869/