【问题标题】:Not able to get HTML TAG TEXT using Selenium webdriver无法使用 Selenium webdriver 获取 HTML TAG TEXT
【发布时间】: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 &lt;span class="caret-right"&gt;&lt;/span&gt;;不知道这是什么?

方式 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


    【解决方案1】:
    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 作为索引变量。

    【讨论】:

    • @rajNishKuMar 欢迎:)
    猜你喜欢
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    • 2022-11-29
    • 2013-10-14
    • 2016-04-29
    • 1970-01-01
    相关资源
    最近更新 更多