如何通过getText()从html中的多个子节点提取动态文本

时间:2018-06-26 04:16:42

标签: java selenium selenium-webdriver webdriver webdriverwait

我们有一个Div,其中包含一些硬编码文本,而span则包含一些动态文本值(请参阅下面的HTML代码以获取更多信息)。 结果文本为: 1个任务,将从“ XYZ”更新为“ ABC”

但是当我使用Selenium locator检索时

final String actual = $("#bulk_update_confirmation").text();

然后,实际值仅包含“要从更新到的任务”。所有动态文本都丢失了。

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>

2 个答案:

答案 0 :(得分:1)

在尝试提取总文本之前,例如 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']"))
));

答案 1 :(得分:0)

您可以使用Webelement.getText()方法提取文本,并获得如下所示的预期结果

String actual =driver.findElement(By.id("bulk_update_confirmation")).getText();
相关问题