简体   繁体   English

无法使用Selenium Webdriver获取HTML TAG TEXT

[英]Not able to get HTML TAG TEXT using Selenium webdriver

I am facing a strange behavior in webdriver. 我在webdriver中遇到了奇怪的行为。 The problem is very simple. 问题很简单。 I just want to print the names of the text in the drop down but I am not able to do that, I have tried various ways. 我只想在下拉列表中打印文本的名称,但是我无法做到这一点,我尝试了各种方法。

WAY 1: Using getText with my code snippet 方法1:getText与我的代码段一起使用

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());
}

Getting null as output. 获取null作为输出。

WAY 2: Using .getAttribute("innerText") output as Load saved data source 方式2:.getAttribute("innerText")输出用作Load saved data source

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

I am not sure why I am getting this Load saved data source as output. 我不确定为什么要获取此Load saved data source作为输出。

WAY 3: Using .getAttribute("innerHTML") output as Load saved data source <span class="caret-right"></span> ; 方式3:.getAttribute("innerHTML")输出用作Load saved data source <span class="caret-right"></span> not sure what is this? 不知道这是什么吗?

WAY 4: Using .getAttribute("textContent") output as Load saved data source ; 方式4:使用.getAttribute("textContent")输出作为Load saved data source not sure what is this? 不知道这是什么吗?

But when I change my xpath from this //*[@class='dropdown-menu']/li/a to //*[@class='dropdown-menu'] and then use .getText(); 但是,当我将xpath从//*[@class='dropdown-menu']/li/a更改为//*[@class='dropdown-menu']然后使用.getText(); I am getting output as 我正在输出为

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...

Desired text but as you can see all menu options are together and in iteration of 6. I am not sure what is happening. 所需的文本,但是如您所见,所有菜单选项都在一起并且在6的迭代中。我不确定发生了什么。 My menu and its source code are: 我的菜单及其源代码为:

在此处输入图片说明

and the menu is: 菜单是:

在此处输入图片说明

Also when I try to click one of the options I am getting error as element not visible. 另外,当我尝试单击选项之一时,由于元素不可见而出现错误。

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

In your code, you used dupmap.get(i).getText() . 在代码中,您使用了dupmap.get(i).getText() but inside for loop you have used j as index variable. 但是在for循环中,您已将j用作索引变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM