2016-09-27 67 views
0

嗨今天我面臨的webdriver問題一個奇怪的行爲是很簡單的,我只是想打印在下拉菜單的文本名稱下來,但我不能這樣做,我已經想盡各種辦法
WAY 1:Using getText我的代碼片段無法使用Selenium webdriver獲取HTML標籤文本?

List<WebElement> dupmap = driver.findElements(By.xpath("//*[@class='dropdown-menu']/li/a")); 
System.out.println("Size of sub menu is : " + dupmap.size()); 
for(int j=0;j<dupmap.size();j++){ 
System.out.println("Sub menu options are : " + dupmap.get(i).getText()); 
} 

越來越null作爲輸出
WAY 2:Using .getAttribute("innerText")輸出Load saved data source

System.out.println("Sub menu options are : " + dupmap.get(i).getAttribute("innerText"));} 

我不知道爲什麼我收到這個Load saved data source作爲輸出

WAY 3:Using .getAttribute("innerHTML")輸出Load saved data source <span class="caret-right"></span>不知道這是什麼?

方式4:Using .getAttribute("textContent")輸出爲Load saved data source不知道這是什麼?

但是當我改變我的xpath窗體這個//*[@class='dropdown-menu']/li/a//*[@class='dropdown-menu'],然後使用.getText();我得到輸出

Size of sub menu is : 6 
Sub menu options are : Add map... 
Duplicate 
Rename 
Auto-align map objects... 
Analyze territory alignment... 
Optimize selected territories... 
Remove... 
Sub menu options are : Add map... 
Duplicate 
Rename 
Auto-align map objects... 
Analyze territory alignment... 
Optimize selected territories... 
Remove... 
Sub menu options are : Add map... 
Duplicate 
Rename 
Auto-align map objects... 
Analyze territory alignment... 
Optimize selected territories... 
Remove... 
Sub menu options are : Add map... 
Duplicate 
Rename 
Auto-align map objects... 
Analyze territory alignment... 
Optimize selected territories... 
Remove... 
Sub menu options are : Add map... 
Duplicate 
Rename 
Auto-align map objects... 
Analyze territory alignment... 
Optimize selected territories... 
Remove... 
Sub menu options are : Add map... 
Duplicate 
Rename 
Auto-align map objects... 
Analyze territory alignment... 
Optimize selected territories... 
Remove... 

所需的文本,但你可以看到所有菜單選項都在一起,在6不知道發生了什麼任何幫助迭代將不勝感激。我的菜單和它的源代碼是enter image description here,菜單是enter image description here

而且當我嘗試點擊我得到的一個選項錯誤的元素不可見

回答

0
for(int j=0;j<dupmap.size();j++){ 
    System.out.println("Sub menu options are : " + dupmap.get(i).getText()); 
} 

在代碼中,你使用dupmap.get(i).getText()。但在for循環中,您已使用j作爲索引變量。

+0

是的,賴特感謝您指出 –

+0

@rajNishKuMar歡迎:) –

相關問題