2016-07-26 60 views
2

我想从左侧访问下面的URL的所有数据列表,但无法找到类和id的正确组合。请帮忙。以下是我的代码。Selenium代码没有从网页中提取faccet数据

网址:http://www.topshop.com/en/tsuk/category/clothing-427/jackets-coats-2390889/N-86tZdgl?No=0&Nrpp=20&siteId=%2F12556

代码

[![public void f() { 
     driver.get("http://www.topshop.com/en/tsuk/category/clothing-427/jackets-coats-2390889/N-86tZdgl?No=0&Nrpp=20&siteId=%2F12556"); 
     List<WebElement> faccet = driver.findElements(By.className("filter_wrapper").className("ce3_sleeve_type").tagName("li")); 
     System.out.println(faccet.size()); 
     for(WebElement a : faccet) 
     { 
     String b = a.getText(); 
     System.out.println(b); 
     } 
    }][1]][1] 

回答

1

请试试这个:

List<WebElement> faccet = driver.findElements(By.CssSelector("div[class='filter_wrapper'] div div span"); 
+0

您建议的代码给了我9个空值而不是我正在寻找的输出。 –

1

请使用下面的代码来获取所有在列表中的过滤器项目类别 我已经使用的XPath但是你总是可以将Xpath中使用的类更改为cssSelectors

public void f() 
{ 
List<WebElement> faccet = driver.findElements(By.xpath("//div[@class='filters jspScrollable']//div[@class='filter_group_label']/span[@class='lbl']")); 
      System.out.println(faccet.size()); 
      for(WebElement a : faccet) 
      { 
      //to get the text of each element and get into view for reading 
       ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", a);   
       String b = a.getText(); 
      System.out.println(b); 
      } 

    } 
+0

上面的代码你分享的是能够获得像颜色,大小等过滤器的值。但我正在寻找这些过滤器中的值,如颜色,它应该提取黑色,蓝色等。 –

+0

//使用这个文本的位置()String b = a.getAttribute(“title”); – Nagaraju

+0

列表 faccet = driver.findElements(By.xpath(“// div [@ class ='filters jspScrollable'] // ul/li/a”));对于内部列出的项目@Pankaj Sharma – Nagaraju

1

如果Selenium认为这些值在浏览器中不可见,它将不会打印这些值。为了让您打印所有方面及其值,您可能需要在打印各个元素之前滚动到每个元素。

我能够使用easytest框架(https://github.com/codezombies/easytest)和下面的代码进行描述。

public class TopShopTest { 

@Test 
public void test() throws Exception { 

    final EasyTestOptions options = new EasyTestOptions(); 
    options.setLogger(new NoOpLogger()); 
    try (EasyTest easy = new EasyTest(DriverType.CHROME, options)) { 

     easy.start("http://www.topshop.com/en/tsuk/category/clothing-427/jackets-coats-2390889/N-86tZdgl?No=0&Nrpp=20&siteId=%2F12556"); 

     easy.newPage(page -> { 
      this.openAndPrintValues(page, ".filter_group.ce3_sleeve_type"); 
      this.openAndPrintValues(page, ".filter_group.colour"); 
      this.openAndPrintValues(page, ".filter_group.size"); 
      this.openAndPrintValues(page, ".filter_group.prod_fit"); 
      this.openAndPrintValues(page, ".filter_group.ce3_jacket_style"); 
      this.openAndPrintValues(page, ".filter_group.ce3_fabric"); 
      this.openAndPrintValues(page, ".filter_group.ce3_collection"); 
      this.openAndPrintValues(page, ".filter_group.ce3_clothing_brands"); 
      this.openAndPrintValues(page, ".filter_group.accessories"); 
     }); 
    } 
} 

private void openAndPrintValues(final ActionablePage page, final String selector) { 
    // open 
    page.scrollTo(selector); 
    page.click(selector); 

    page.executeIn(selector + ":not(.collapsed)", container -> { 

     // get facet heading 
     final WebElement heading = container.getRaw(".filter_group_label span.lbl"); 
     page.scrollTo(heading); 
     System.out.println("Choices under selector: " + heading.getText()); 

     // get facet lists 
     container.executeIn("ul", list -> { 
      list.wait((Predicate<WebDriver>) t -> list.getRawList("li.refinement:not(.disabled) span.lbl").size() > 0);; 
      final List<WebElement> elements = list.getRawList("li.refinement:not(.disabled) span.lbl"); 
      // scroll to end of list, selenium will not display 'unseeen' elements 
      page.scrollTo(elements.get(elements.size() - 1)); 

      // pring heading and list 
      elements.stream().map(WebElement::getText).filter(StringUtils::isNotEmpty).forEach(System.out::println); 
      System.out.println(); //extra space 
     }); 
    }); 
} 

}

下面是对代码的输出。

 
Choices under selector: Sleeve Length 
3/4 Sleeve 
Long Sleeve 
Sleeveless 

Choices under selector: Colour 
Purple 
Red 
White 

Choices under selector: Size 
10 
12 
14 
16 
XS 
S 
S/M 
M 
M/L 
L 
XL 

Choices under selector: Fit 
Regular 
Petite 
Tall 
Maternity 

Choices under selector: Jacket & Coat Style 
Faux Fur Coats 
Gilets 
Kimono 
Parkas 
Shacket 
Shearling Coats 
Sleeveless Jackets 
Smart Coats 
Smart Jackets 
Trench Coats & Macs 
Wool Coats 

Choices under selector: Fabric 
Cord 
Denim 
Embroidered 
Faux Fur 
Faux Leather 
Lace 
Leather 
Sequin 
Suede 
Velvet 

Choices under selector: Collection 
Boutique 
Festival 
Going Out 
Sportswear 
Unique 
Workwear 

Choices under selector: Clothing Brands 
Adidas Originals 
Glamorous 
Glamorous Petites 
Goldie 

Choices under selector: Accessories 
One Size