无法从嵌套对象获取属性

时间:2020-01-31 18:02:50

标签: java html selenium

我花了很多时间试图解决这个问题,但是我找不到经过几个小时研究的可行解决方案。

我目前正在使用硒。页面本身是有角度的,但是在这种情况下应该没有问题。

我试图将嵌套的类存储到列表中,但是我无法使用许多不同的方法(例如xPath,cssselector和innerHTML)来获取我一直在尝试的属性。

下面是整个对象的图片

enter image description here

我认为这种结构上下是这样的:

<div _ngcontent-sc11="" class="Stock-busses"> 

<div _ngcontent-sc11="" class="Stock-bussesList ng-tns-c11-6 isCards ng-star-inserted" style="">

<mysite-truck _ngcontent-sc11="" class="Stock-truck ng-tns-c11-6 ng-trigger ng-trigger-listItem ng-
star-inserted" _nghost-sc17="" style="">

<a _ngcontent-sc17="" class="Truck ng-star-inserted" href="null" style=""> (HERE IS WHERE the object begins)

这是我的无辜者:


@Test
public void Search_certain_truck() {

  List < WebElement > elements = driver.findElements(By.className("Stock-trucks"));
  java.util.Iterator < WebElement > program = elements.iterator();
  while (program.hasNext()) {
      String values = program.next().getText();

      if (!values.equals("null")) {

          List < WebElement > elem = driver.findElements(By.xpath("//div[@class='class=\"Stock-trucksList ng-tns-c11-6 isCards ng-star-inserted"));

          java.util.Iterator < WebElement > progz = elem.iterator();

          while (program.hasNext()) {
              String Truck_title = program.next().getText();

              if (!Truck_title.equals("null")) {
                  System.out.println(Truck_title);
              } else {

              }
          }

          //System.out.println(values); //THIS WORKS TO ALERT LINE 666

      } else {

      }
  }
}

我可以提醒666行,但是当我尝试抓住嵌套的东西时却一无所获

这里是豁免2:

 @Test
 public static void myatempt2() {
     List < String > TruckList = new ArrayList < String > ();
     List < String > TruckHref = new ArrayList < String > ();
     List < WebElement > x = driver.findElements(By.xpath("//div[@class='Stock-trucks']"));

     Loc.NavXPath(Our_Truck);
     Sa.WaitForBrowser(2000);

     // List<WebElement> x= driver.findElements(By.cssSelector("volvo-truck"));
     // List<WebElement> x= driver.findElements(By.className("volvo-truck"));
     driver.findElement(By.xpath("//div[@class='Stock-trucks ui-draggable ui-draggable-handle']")).getAttribute("href");
     for (WebElement webElement: x) {

         // TruckList.add(webElement.getText());
         //         TruckList.add(webElement.getAttribute("innerHTML"));
         System.out.println(webElement.getAttribute("innerHTML"));
         // System.out.println(webElement.getText());

     }

     for (int counter = 0; counter < TruckList.size(); counter++) {
         // System.out.println(TruckList.get(counter));
     }

     String FirstEl = TruckList.get(0);
 }

我仍然无法抓取嵌套的对象

这是页面链接:Pastebin

1 个答案:

答案 0 :(得分:0)

我已经解决了它,但是我并不真的在乎如何存储各种东西:)

@Test
public void Verify_a_Specific_Inquiry() throws Exception {


    // Loc.NavXPath(Our_Stock);

    List<String> TruckList = new ArrayList<String>();
    List<String> TruckListHref=new ArrayList<String>();
    List<WebElement> Truck_List_Element = driver.findElements(By.xpath("//div[@class='Stock-trucksList ng-tns-c11-6 isCards ng-star-inserted']"));

    for (WebElement x : Truck_List_Element) {

        // System.out.println(x.getText());

        // findElements(By.className("Truck ng-star-inserted"));

        // List<WebElement> in= driver.findElements(By.xpath("//body//volvo-truck[2]"));
        // //GET ONE TRUCK

        List<WebElement> in = driver.findElements(By.tagName("volvo-truck"));

        for (WebElement z : in) {

            // System.out.println(z.getText());

            TruckList.add(z.getText());

            //System.out.print(z.getClass("Truck ng-star-inserted").)

            TruckListHref.add(z.findElement(By.tagName("a")).getAttribute("href"));

            //System.out.print(z.findElement(By.tagName("a")).getAttribute("href"));

        }

    }

    String FirstTruck = TruckList.get(0);
    String FirstHref = TruckListHref.get(0);

    System.out.println(FirstTruck);
    System.out.println(FirstHref);

    driver.quit();

}