找到元素的标题

时间:2017-12-12 10:48:14

标签: java xpath selenium-webdriver

请有人帮忙解决以下问题:

我有这个HTML

<div class="author medium-3 small-12 column padding-reset tip-list-row__author">
<a href="https://expert.com/user/profile/pulkovo124" class="author-rating left" data-rating="2" data-modal="user-modal" data-user- id="465043"><canvas width="66" height="66" class="author-rating-visual user-rating-visual"></canvas>
<abbr title="Rating 2/5">

使用此xpath找到元素:

//div[@class='author medium-3 small-12 column padding-reset tip-list-row__author']/a/abbr[@title]

但是当我想打印标题评分2/5 的文字时,请获取xpath而不是文字:

 xpath: //div[@class='author medium-3 small-12 column padding-reset tip-list-row__author']/a/abbr[@title]]

我做错了什么?提前谢谢

这是我的代码:

List<WebElement> allrates = driver.findElements(By.xpath("//div[@class='author medium-3 small-12 column padding-reset tip-list-row__author']/a/abbr[@title]"));
    List<WebElement> allusers = driver.findElements(By.xpath("//div[@class='author-name']/a"));

    for (int i = 0; i < alltips; i++) {
        String all = allrates.get(i).getText();
        String users = allusers.get(i).getText();
        System.out.println("User " + users + " has a rate " + all);
        Thread.sleep(500);

    }

1 个答案:

答案 0 :(得分:1)

要打印标题评分2/5 的文字,您可以使用以下代码块:

List<WebElement> allrates = driver.findElements(By.xpath("//div[@class='author medium-3 small-12 column padding-reset tip-list-row__author']/a//abbr"));
List<WebElement> allusers = driver.findElements(By.xpath("//div[@class='author-name']/a"));
for (int i = 0; i < alltips; i++) 
{
    String all = allrates.get(i).getAttribute("title");
    String users = allusers.get(i).getText();
    System.out.println("User " + users + " has a rate " + all);
}
  

注意:我保持其他代码行不受影响