Print all product details in search results using Selenium Java



  • I am trying to print product details from searched results from this page.

    This is the function I used for that.

    @Test(priority = 1)
    public void printSearchItem() {
    
        // print the product details
        java.util.List<WebElement> products = driver.findElements(By.className("sresult lvresult clearfix li"));
    
        for(WebElement product:products) {
    
            System.out.println(product.getText());      
        }   
    }
    

    But I am getting below error:

    org.openqa.selenium.InvalidSelectorException: invalid selector: Compound class names not permitted

    This is the html code for one product.

    <li id="item3f91166db6" _sp="p2045573.m1686.l0" listingid="273017105846" class="sresult lvresult clearfix li" r="1">
    
        <div class="lvpic pic img left" iid="273017105846">
            <div class="lvpicinner full-width picW">
    
                <a href="https://www.ebay.com/itm/Apple-Watch-series-3-38mm-Sp-Gray-dark-Olive-Sp-loop-GPS-Cellular-Brand-New/273017105846?epid=15012720917&amp;hash=item3f91166db6:g:SsMAAOSwEUdaVBxw" class="img imgWr2">
                    <img src="https://i.ebayimg.com/thumbs/images/g/SsMAAOSwEUdaVBxw/s-l225.jpg" class="img" alt="Apple Watch series 3 38mm Sp Gray, dark Olive Sp loop (GPS+Cellular) Brand New!!">
                </a>
            </div>
        </div>
        <h3 class="lvtitle"><a href="https://www.ebay.com/itm/Apple-Watch-series-3-38mm-Sp-Gray-dark-Olive-Sp-loop-GPS-Cellular-Brand-New/273017105846?epid=15012720917&amp;hash=item3f91166db6:g:SsMAAOSwEUdaVBxw" class="vip" title="Click this link to access Apple Watch series 3 38mm Sp Gray, dark Olive Sp loop (GPS+Cellular) Brand New!!">Apple Watch series 3 38mm Sp Gray, dark Olive Sp loop (GPS+Cellular) Brand New!!</a>
                </h3>
        <div class="lvsubtitle">
            Brand New</div>
        <div class="star-ratings ">
            <a href="https://www.ebay.com/p/Apple%2DWatch%2DSeries%2D3%2D38mm%2DCellular%2DGPS/15012720917?iid=273017105846#UserReviews" _sp="p2045573.m1686.l8597" aria-label="Rating: 5.0 out of 5, Click to read reviews">
                <i class="star-ratings__star star-ratings__star--full"></i>
                <i class="star-ratings__star star-ratings__star--full"></i>
                <i class="star-ratings__star star-ratings__star--full"></i>
                <i class="star-ratings__star star-ratings__star--full"></i>
                <i class="star-ratings__star star-ratings__star--full"></i>
            </a>
            <a href="https://www.ebay.com/p/Apple%2DWatch%2DSeries%2D3%2D38mm%2DCellular%2DGPS/15012720917?iid=273017105846#UserReviews" class="star-ratings__review-num" _sp="p2045573.m1686.l8597" aria-label="4 product ratings, Click to read reviews">4 product ratings</a>
        </div>
        <ul class="lvprices left space-zero" _sp="p2045573.m1686.l0">
    
            <li class="lvprice prc">
                <span class="bold">
                            $480.00</span>
            </li>
            <li class="lvformat">
                <span>
                        Buy It Now</span>
            </li>
            <li class="lvshipping">
                <span class="ship">
            <span class="fee">
                                        +$45.97 shipping</span>
                </span>
            </li>
            <li class="lvextras" _sp="p2045573.m1686.l0">
                <div class="hotness bold">
    
                </div>
    
            </li>
    
        </ul>
        <ul class="lvdetails left space-zero full-width">
            <li>
                From United States</li>
            <li>
                <div class="epli">Customs services and international tracking provided</div>
            </li>
        </ul>
    </li>
    

    How to fix this and print the product values?



  • The error says Compound class names not permitted .In your code sresult lvresult clearfix li are 4 different classes and you are using multiple classes with in className Selector which Selenium doesn't allow.

    To used compound classes you can use xpath or CSS selector as following

    xpath

    driver.findElements(By.xpath("//li[@class='sresult lvresult clearfix li']"));
    

    cssSelector

    driver.findElements(By.cssSelector("li[class='sresult lvresult clearfix li']"));
    

    OR

    driver.findElements(By.cssSelector(".sresult.lvresult.clearfix.li"));
    

    Note: make sure element is unique while locating using class name



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2