0

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?

  • Which webdriver are you using? – Alexey R. Sep 12 '18 at 11:01
2

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

  • 1
    An alternative CSS selector would be li.sresult.lvresult.clearfix.li – João Farias Sep 12 '18 at 14:11
1

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.

Cheers!!!

enter image description here

1

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. – Darshani Kaushalya Sep 12 '18 at 7:13
  • Did you add System.out.println(); – Sachintha Oct 4 '18 at 6:20
0
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.get("https://www.flipkart.com");
        System.out.println(driver.getTitle());
        System.out.println(driver.getCurrentUrl());
        driver.manage().deleteAllCookies();

        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.xpath("//span[text()='CONTINUE']")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath("//button[text()='✕']")).click();
        driver.findElement(By.name("q")).sendKeys("apple laptops",Keys.ENTER);
        Thread.sleep(2000);

        List<WebElement> vs = driver.findElements(By.xpath("//div[@class='_1UoZlX']"));
        System.out.println("total no of value"+vs.size());
        for (WebElement product : vs) {
            System.out.println(product.getText());
            System.out.println("<======================================>");
        }
    }
}
  • 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 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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