How to extract dynamic text from multiple child no

2020-04-14 07:36发布

We have a Div containing some hard coded text and span containing some dynamic text values (refer below HTML code for more understanding). The resultant Text is: 1 Tasks to be updated from 'XYZ' to 'ABC'

but when I am retrieving using Selenium locator i.e.

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

Then the actual contains only "Tasks to be updated from to". All the dynamic text are missing.

HTML code is as below (browser used is 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条回答
家丑人穷心不美
2楼-- · 2020-04-14 08:12

You can use the Webelement.getText() method to extract text and you can get the expected result as below

String actual =driver.findElement(By.id("bulk_update_confirmation")).getText();
查看更多
太酷不给撩
3楼-- · 2020-04-14 08:29

Before you attempt to extract the total text e.g. 1 Tasks to be updated from 'XYZ' to 'ABC' you can induce WebDriverWait for all the three child elements to be visible and you can use the following solution:

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']"))
));
查看更多
登录 后发表回答