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) {


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!!">
    <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>
    <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 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>
    <ul class="lvprices left space-zero" _sp="p2045573.m1686.l0">

        <li class="lvprice prc">
            <span class="bold">
        <li class="lvformat">
                    Buy It Now</span>
        <li class="lvshipping">
            <span class="ship">
        <span class="fee">
                                    +$45.97 shipping</span>
        <li class="lvextras" _sp="p2045573.m1686.l0">
            <div class="hotness bold">



    <ul class="lvdetails left space-zero full-width">
            From United States</li>
            <div class="epli">Customs services and international tracking provided</div>

How to fix this and print the product values?

  • Which webdriver are you using?
    – Alexey R.
    Sep 12, 2018 at 11:01

5 Answers 5


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


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


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



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

  • 1
    An alternative CSS selector would be li.sresult.lvresult.clearfix.li Sep 12, 2018 at 14:11

Your locator contains multiple class(sresult,lvresult,clearfix,li) is the reason of "invalid selector" exceptions.

You can use xpath :

//*[contains(@class,'sresult') and contains(@class,'lvresult') and contains(@class,'clearfix') and contains(@class,'li')]

that gives you the list of all products.


enter image description here


The issue is because of the way find by Class name works.

In your code class name is class="sresult lvresult clearfix li"

If the class name has space you'll get the above error. You can simply get rid of the issue by using Id, CSS, Xpath, regular expression or any other element finder method.

Below Xpath is working perfectly

java.util.List<WebElement> products = driver.findElements(By.xpath("//*[@class='sresult lvresult clearfix li']"));
  • with xpath error is not coming. function is passed but nothing is printed. Sep 12, 2018 at 7:13
  • Did you add System.out.println();
    – Sachintha
    Oct 4, 2018 at 6:20
package automation.selenium;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Print_allthe_product{

    public static void main(String[] args) throws Throwable {

        System.setProperty("webdriver.chrome.driver", "./Drivers/chromedriver.exe");
        WebDriver driver=new ChromeDriver();

        driver.findElement(By.xpath("//a[text()='New to Flipkart? Create an account']")).click();
        driver.findElement(By.xpath("//input[@class='_2zrpKA _1dBPDZ']")).sendKeys("9337276560");
        driver.findElement(By.name("q")).sendKeys("apple laptops",Keys.ENTER);

        List<WebElement> vs = driver.findElements(By.xpath("//div[@class='_1UoZlX']"));
        System.out.println("total no of value"+vs.size());
        for (WebElement product : vs) {
  • Welcome. Please also add some explanation why your code works and what the OP should do differently etc. It makes it more readable than just a chunk of code you post here.
    – pavelsaman
    Mar 16, 2020 at 17:28

If you want to go with the class name, and have the space in it, you can simply leave the part of the class name before or after the space and delete the rest, e.g.: findElement(By.className("js-general-error general-error cbt d-none")); will give you error, instead you just go with the part of class name not containing space findElement(By.className("js-general-error")); If you are looking for the stronger selector css and xpath are probably the most reliable ones aside id. Also found custom xpath explained in the previous post very useful.

//*[contains(@class,'sresult') and contains(@class,'lvresult') and contains(@class,'clearfix') and contains(@class,'li')]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.