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

标签 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><td>具有相同的 id。换句话说,一行中的三个单元格将具有三个 id。但其他行的单元格具有相同的三个 ID。

如何找到 <div> 中的任何一个或<input>

Dynamic display contents

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

最佳答案

如果你想定位任何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

关于java - 如何在显示文本为动态的网页上定位元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50475869/

相关文章:

java - 如何使用selenium选择自定义下拉列表元素

selenium - 未创建 Webdriver.io session : Unable to create new service: GeckoDriverService

java - 如何为 stanford corenlp 获取文本的 xml 输出

java - 在Android应用程序中使用OpenCV人脸识别算法

java - 关于Java中使用加密库进行AES加密的问题

javascript - 使用 CSS 的逐个字母的文本外观动画

java - 标准化索引/搜索的字符串

javascript - 上传 Html 5 拖放文件的最简单方法

javascript - HTML 分页在第一页上搞砸了,然后就可以正常工作了吗?

java - Selenium InternetExplorerDriver 无法找到明显存在的元素