【发布时间】:2017-02-04 16:52:47
【问题描述】:
我在 webdriver 中遇到了一个奇怪的行为。问题很简单。我只想在下拉列表中打印文本的名称,但我无法做到,我尝试了各种方法。
方式 1: 将 getText 与我的代码 sn-p 一起使用
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 作为输出。
方式 2: 使用 .getAttribute("innerText") 输出为 Load saved data source
System.out.println("Sub menu options are : " + dupmap.get(i).getAttribute("innerText"));}
我不知道为什么我会得到这个 Load saved data source 作为输出。
方式 3: 使用.getAttribute("innerHTML") 输出为Load saved data source <span class="caret-right"></span>;不知道这是什么?
方式 4: 使用.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 的迭代中。我不确定发生了什么。我的菜单及其源代码是:
菜单是:
此外,当我尝试单击其中一个选项时,由于元素不可见而出现错误。
【问题讨论】:
标签: java html selenium selenium-webdriver webdriver