使用不同的图标获取超链接的文本

Get the Text for the hyperlink with a different Icon

我正在尝试获取在 it.When 旁边有一个标签图标 "ins" 的超链接的文本 我写了下面的代码,它打印了“?”而不是 text.screen 为被测应用程序附上截图![屏幕截图]。请帮助我解决问题。

列出链接 = driver.findElements(By.tagName("ins"));

    System.out.println("the number of movie links in the site is "+links.size());

    for (int i = 1; i<=links.size(); i=i+1)

    {

        System.out.println(links.get(i).getText());

    }

所以你可以使用 //ins[@class='icon movie']/ancestor::a 到达 element/s 然后使用

for (int i = 0; i<links.size(); i++){
    System.out.println(links.get(i).getAttribute("href"));
}

同样在您的循环中,您忽略了列表中的第一个元素。是错误的还是需要的? 您也可以为此目的使用增强的 for 循环:

for (WebElement webelement : links){
    System.out.println(links.get(i).getAttribute("href"));
}

如果您使用的是新的 Java 8,您可以通过以下方式探索新功能:

links.forEach(new Consumer<WebElement>() {
    @Override
    public void accept(WebElement t) {
        System.out.println(t.getAttribute("href"));
    }
});