如何使用selenium webdriver打印<nobr>标记中的值

时间:2015-07-09 15:23:18

标签: html selenium selenium-webdriver webdriver

如果有人能指出我正确的方向,那将会非常有帮助。 我正在尝试打印nobr标记中存在的值。

这是我的代码。

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class appannieuserrating {
    public static WebDriver driver;
  @Test
  public void f() throws Exception{
      driver.findElement(By.id("login-button")).click();
         driver.findElement(By.id("username")).sendKeys("sample.test@gmail.com");
         driver.findElement(By.id("password")).sendKeys("password");
         driver.findElement(By.id("login_submit_button")).click();
         Thread.sleep(5000);
         driver.navigate().to("https://www.appannie.com/apps/ios/app/storage-hunters-uk-the-game/reviews/?date=2015-07-06~2015-07-06");
         Thread.sleep(5000);

         String s1 = driver.findElement(By.cssSelector(".ngCellText.cell-inner.ng-scope")).findElement(By.tagName("img")).getAttribute("src");
         System.out.println(s1);

  }
  @BeforeTest
  public void beforeTest() {
      driver = new FirefoxDriver();
      driver.get("https://www.appannie.com/");
  }

  @AfterTest
  public void afterTest() {
  }

}



    <div class="ngCanvas" ng-style="canvasStyle()" style="height: 467px;">
<div class="ng-scope ngRow even" ng-row="" ng-class="row.alternatingRowClass()" ng-click="row.toggleSelected($event)" ng-repeat="row in renderedRows" ng-style="rowStyle(row)" style="top: 0px; height: 58px;">
<div class="ngCell col0 colt0" ng-class="{'sorted': col.sortDirection}" ng-repeat="col in renderedColumns" style="height: 58px;">
<div class="ngVerticalBar ngVerticalBarVisible" ng-class="{ ngVerticalBarVisible: !$last }" ng-style="{height: rowHeight}" style="height: 58px;"> </div>
<div ng-cell="">
<div class="ngCellText cell-inner ng-scope">
<span stars="row.getProperty(col.field)" aa-stars="">
<nobr>
<img alt="*" src="/media/pictures/star-whole.png">
<img alt="*" src="/media/pictures/star-whole.png">
<img alt="*" src="/media/pictures/star-whole.png">
<img alt="*" src="/media/pictures/star-whole.png">
<img alt="*" src="/media/pictures/star-whole.png">
</nobr>
</span>
</div>
</div>
</div>

这里我试图计算&#34; nobr&#34;中存在的星数。以某种方式标记其打印空值。

请我指出正确的方向。

2 个答案:

答案 0 :(得分:0)

我会这样做:

 List<WebElement> elements = driver.findElement(By.xpath("//span[@aa-stars]/nobr")).findElements(By.tagName("img"));
 int counter = 0;

 for(WebElement we: elements) {
     if("*".equals(we.getAttribute("alt"))) {
         counter++;
     }
 }

 System.out.println(counter);

不幸的是,你的密码不正确,所以我无法自己尝试:D:D在本地尝试使用相同的HTML - 应该可以。

<强> UPD:

我忘了告诉 - 在你试图找到img元素时 - 你忽略了 nobr 。是。它也在DOM中。如果你想找到下一个img - 就像这样:

.findElement(By.xpath("//img"));

如果您使用by tagName - 则假定您搜索直接子项。

UPD 2

如果你依赖src并相信这个img应该是一个明星,请考虑一下:

 for(WebElement we: elements) {

     String text = we.getAttribute("src");
     if(text!=null) {
         if(text.contains("star"){
              counter++;
         }
     }
 }

UPD 3

        List<WebElement> elements = driver.findElement(By.xpath("//span[@aa-dtars]/nobr")).findElements(
            By.tagName("‌​img"));

    int counter = 0;

    String starPicLink = "/media/pictures/star-whole.png";
    for (WebElement we : elements) {
        String text = we.getAttribute("src");

        if (starPicLink.equals(text)) {
           counter++;
        }
    }

    System.out.println(counter + " Star");

UPD 4

pastie.org/10292596#34 - 工作版

答案 1 :(得分:0)

我尝试了以下链接,以在nobr标签内标识输入标签:

xpath = // div [@ id ='div0_17'] / span [@ id ='outer0_366'] / nobr / input

相关问题