我的頁面中有以下HTML。Selenium Webdriver gettext()返回空字符串
<div dojoattachpoint="rowNode" class="dijitTreeRow dijitTreeRowSelected" wairole="presentation" dojoattachevent="onmouseenter:_onMouseEnter, onmouseleave:_onMouseLeave, onclick:_onClick, ondblclick:_onDblClick" role="presentation" title="" style="padding-left: 19px; ">
<img src="/runtime/explorer.sharedresources/javascript-1.7.22.268034/release/dojo/resources/blank.gif" alt="" dojoattachpoint="expandoNode" class="dijitTreeExpando dijitTreeExpandoLeaf" wairole="presentation" role="presentation"/>
<span dojoattachpoint="expandoNodeText" class="dijitExpandoText" wairole="presentation" role="presentation">*</span>
<span dojoattachpoint="contentNode" class="dijitTreeContent" wairole="presentation" role="presentation">
<img src="/runtime/explorer.sharedresources/javascript-1.7.22.268034/release/dojo/resources/blank.gif" alt="" dojoattachpoint="iconNode" class="dijitIcon dijitTreeIcon criterionFolder" wairole="presentation" role="presentation"/>
<span dojoattachpoint="labelNode" class="dijitTreeLabel" wairole="treeitem" tabindex="-1" waistate="selected-false" dojoattachevent="onfocus:_onLabelFocus" role="treeitem" aria-selected="true">**My TEXT**</span>
</span>
</div>
這是正確標識元素的代碼。
List<WebElement> RowNodesList = elementPopup.findElements(By.xpath("//div[@dojoattachpoint =\"rowNode\"]"));
for(WebElement RowNode:RowNodesList)
{
System.out.println("Left Padding: " + RowNode.getAttribute("style"));
System.out.println("Is Row Node Displayed: " + RowNode.isDisplayed());
WebElement labelNode = RowNode.findElement(By.xpath("//span[@dojoattachpoint=\"labelNode\"]"));
boolean IsNodeDisplayed = labelNode.isDisplayed();
System.out.println("Is Label Node Displayed: " + IsNodeDisplayed);
String NodeLabel = (String) ((JavascriptExecutor) _driver).executeScript("return arguments[0].firstChild.nodeValue",labelNode);
System.out.println("Row label From javaScript: " + NodeLabel);
System.out.println("Row label: " + labelNode.getText());
System.out.println("Row Class: " + labelNode.getAttribute("class"));
}
這是從代碼生成的輸出。
Left Padding: PADDING-LEFT: 19px
Is Row Node Displayed: true
Is Label Node Displayed: false
Row label From javaScript: undefined
Row label:
Row Class: dijitTreeLabel
出於某種原因,由.getText()返回一個空字符串。有任何想法嗎? 在此先感謝。
編輯: 奇怪的是,labelNode.isdisplayed()返回false,即使它在頁面上可見。
編輯2:我已經編輯我的OP到上面,使問題更清楚。所以,我想要的文本駐留在標籤節點的SPAN標籤中,該標籤節點是RowNode的子節點。據說RowNode顯示,但標籤節點不是。我認爲這就是爲什麼LabelNode的.getText()返回空字符串的原因。
有沒有其他辦法可以讓我的文字?
編輯3:我想出了問題。這是xpath搜索字符串。 而不是
WebElement labelNode = RowNode.findElement(By.xpath("//span[@dojoattachpoint=\"labelNode\"]"));
中的XPath需要改變,以
WebElement labelNode = RowNode.findElement(By.xpath(".//span[@dojoattachpoint=\"labelNode\"]"));
我曾以爲,「//」會指示webdriver的當前上下文中的樣子。但顯然,它在整個身體內搜索(這不直觀),並返回找到的第一個被隱藏的元素。 添加「.//」指示它在當前上下文(RowNode)內進行搜索。
感謝大家的幫助。
我試着用JavaScript來返回文本,但是如果有空字符串,它會返回「undefined」。
String NodeLabel = (String) ((JavascriptExecutor) _driver).executeScript("return arguments[0].firstChild.nodeValue",contentNode); \t \t \t System.out.println("Row label From javaScript: " + NodeLabel);
– Vic11 2012-07-24 19:46:30如果它返回undefined,那麼它找不到你的元素,並且可以肯定地說你的選擇器是錯誤的。 – 2012-07-24 20:42:14
@RoddyoftheFrozenPeas,你是對的。 OP已更新。 – Vic11 2012-07-25 17:05:14