简体   繁体   中英

Selenium code not extracting faccet data from webpage

I am trying to access all the list of data from Left hand side for the below URL but unable to find right combination of classes and id. Please help. Below is my code.

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

Code

[![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]

请尝试以下方法:

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

Please use below code to get all the filter item categories in that list I have used the XPath but you can always change classes used in Xpath to 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);
             }

  }

Selenium won't print the values if it thinks those values are not visible on the browser. In order for you to print all the facets and their values, you might want to scroll to each element before printing them.

I was able to do what you describe using easytest framework ( https://github.com/codezombies/easytest ) and with the code below.

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
        });
    });
}

}

And here's the output for the code.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM