Reputation: 23
I'm trying to find the number of search results being displayed in (Flipkart e-commerce website) website using classname/xpath/cssselector
, but I'm unable to find the total number of results.
The total number is being displayed as a text:
"Showing 1 – 24 of 8,747 results for "mobile phones"
In the webpage. I'm also unable to identify the number of search items displayed inside each page which is 24 in this case.
The code that I used to find elements is:
List<WebElement> flipkartTotalItems = driver.findElements(By.cssSelector("#container > div > div:nth-child(2) > div > div._1XdvSH._17zsTh > div > div._2xw3j- > div > div:nth-child(3) > div._2SxMvQ > div > div:nth-child(1)"));
#container > div > div:nth-child(2) > div > div._1XdvSH._17zsTh > div > div._2xw3j- > div > div._15eYWX > div > div.KG9X1F > h1 > span
I added thread.sleep
method call for the page to load too.
HTML code for text webelement:
Upvotes: 2
Views: 1090
Reputation: 193108
As per the url you have shared to find the number of search results being displayed in Flipkart Search Result ecommerce website you have to induce WebDriverWait for the WebElement to be visible and then extract the text as follows :
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.flipkart.com/search?q=mobile%20phones&otracker=start&as-show=on&as=offFlipkart ecommerce website");
new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[@class='W-gt5y']")));
driver.findElement(By.xpath("//span[@class='W-gt5y']//ancestor::span")).getText();
System.out.println(driver.findElement(By.xpath("//span[@class='W-gt5y']//ancestor::span")).getText());
Console Output :
Showing 1 – 24 of 8,753 results for "mobile phones"
Upvotes: 0
Reputation: 871
The HTML is not very friendly for Browser Automation but the most stable xpath for getting the searchresults on a page I think is:
List<WebElement> flipkartTotalItems = driver.findElements(By.xPath("//div[@class = 'col col-7-12']"));
Getting the length or count of this list wil give you the number of results on the page.
Upvotes: 0
Reputation: 78
I think the best way to get the total number of results is to use this xpath
//span[contains(text(),'Showing')]
Then using "GetText()" get the result using string formatting
String result = driver.findElement(By.xpath("<xpath>")).getText();
Upvotes: 0
Reputation: 101
You can use below xpath to locate this "Showing 1 – 24 of 8,747 results for "mobile phones"
//*[contains(text(),'Showing 1 – 24 of 8,747 results for')]
Below for finding number search result show in a page.
//*[@class='_1UoZlX']
Upvotes: 1