我们有一个包含一些硬编码文本的 Div 和包含一些动态文本值的 span(请参阅下面的 HTML 代码以获取更多理解)。 结果文本为:1 任务从“XYZ”更新为“ABC”
但是当我使用 Selenium 定位器检索时,即
final String actual = $("#bulk_update_confirmation").text();
那么实际只包含“Tasks to be update from to”。所有动态文本均丢失。
HTML代码如下(使用的浏览器是chrome)
<div id="bulk_update_confirmation" class="ui-dialog-content ui-widget-content" style="display: block; width: auto; min-height: 96.16px; height: auto;" scrolltop="0" scrollleft="0">
<span id="taskCountSpan">1</span> Tasks to be updated from <span id="oldStatusSpan">'XYZ'</span> to <span id="newStatusSpan">'ABC'</span> <span id="desciptionSpan"> </span> <br>
<hr>
<div class="ui-dialog-buttonset">
<button type="button" id="btn_bulk_update" onclick="updateBulkStatus()">Update</button>
<button type="button" onclick="closeBulkUpdateRequest()">Cancel</button>
</div>
</div>
最佳答案
在尝试提取完整文本之前,例如1 任务从“XYZ”更新为“ABC”,您可以引发 WebDriverWait 以使所有三个子元素可见,并且您可以使用以下解决方案:
new WebDriverWait(driver, 10).until(ExpectedConditions.and(
ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[@id='taskCountSpan']")),
ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[@id='oldStatusSpan']")),
ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[@id='newStatusSpan']"))
));
关于java - 如何通过getText()从html内的多个子节点中提取动态文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51034819/