为什么Webdriver.findElements缺少元素?

时间:2018-02-09 12:14:09

标签: selenium

我正在使用这一小段代码从屏幕返回值:

    // find label-placeholder-text by innerHTML
private static final String labelPlaceholderTextClass="label-placeholder-text";
// get 'for' value
// get field with that id
// print value from that field?
public static String returnValueFromPlaceholderText(WebDriver driver, String identifier) {
    myPrint(thisClass + " Find value using identifier = '" + identifier + "'");
    List<WebElement> placeholders = driver.findElements(By.className(labelPlaceholderTextClass));
    myPrint(thisClass + " placeholders count: " + placeholders.size());
    for (WebElement element : placeholders) {
        String text = element.getAttribute("innerHTML");
        if (text != null) {
            text = text.trim();
            myPrint(thisClass + " text: " + text);
            if (text.equals(identifier)) {
                myPrint(thisClass + " found: " + text);
                String forId = element.getAttribute("for");
                myPrint(thisClass + " id of input field =  " + forId);
                WebElement inputField = driver.findElement(By.id(forId));
                getAllAttributes(element, driver);
                text = inputField.getAttribute("value");
                myPrint(thisClass + " " + identifier + " value = " + text);
                return text;
            }
        }
    }
    return "";
}

在我的网页上,我有一些标签占位符文字&#39;字段。

它正在查找并成功返回值:

<input type="text" class="form-control text-ellipsis-global 
form-control-readonly" id="gwt-uid-2431" data-empty="false" readonly="">

<label class="label-placeholder-text" for="gwt-uid-2431"> Last activity:</label>

示例输出:

utils.Common                             Find value using identifier = 'Last activity:'
utils.Common                             placeholders count: 1
utils.Common                             text: Last activity:
utils.Common                             found: Last activity:
utils.Common                             id of input field =  gwt-uid-2431
utils.Common                            get all attributes.
{for=gwt-uid-2431, class=label-placeholder-text}
utils.Common                             Last activity: value = 19 hours ago

但它找不到这些:

<input type="text" class="form-control text-ellipsis-global 
form-control-readonly" id="gwt-uid-2473" data-empty="false" readonly="">
<label class="label-placeholder-text" for="gwt-uid-2473">Reported Date</label>

任何想法为什么它只找到1个元素?

0 个答案:

没有答案