0

I am navigating to https://www.amazon.com/

there I am looking for 'samsung tv 55 inch' setting it in the search box text field

then I am trying to extract the text of (63 results [see image attached]):

to

I can't find the correct locator and how to find it, this is my code:

package com.bottomline.automation.tests.ui;

import com.bottomline.automation.pageobjects.model.AmazonWebPage;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class AmazonTest extends BaseTest {
    AmazonWebPage amazonWebPage;

    @BeforeClass
    public void setup() {
        amazonWebPage = new AmazonWebPage(driverWrapper);
    }

    @Test(priority = 10)
    public void navigateToAmazonWebPage(){
        amazonWebPage.navigateAndVerify();
    }

    @Test(priority = 20)
    public void searchForHarryPotter(){
        amazonWebPage.setSearchTextSearchBox("samsung tv 55 inch");
    }


}

I am struggling in finding the correct locator in order to get the result text

this is the source html: enter image description here

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
kim el
  • 39
  • 9

2 Answers2

2

To extract the text 73 from the text 1-16 of 73 results for "samsung tv 55 inch" you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:

  • Using cssSelector and split():

    String[] cssParts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("h1.s-desktop-toolbar div.sg-col-inner div.a-section>span"))).getText().split(" ");
    System.out.println(cssParts[2]);
    
  • Using xpath and split():

    String[] xpathParts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h1[contains(@class, 's-desktop-toolbar')]//div[@class='sg-col-inner']//div[contains(@class, 'a-section')]/span"))).getText().split(" ");
    System.out.println(xpathParts[2]);
    
  • Console Output:

    72
    

Complete code block

Here is the complete solution

  • Code Block:

    driver.get("https://www.amazon.com/");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.nav-input#twotabsearchtextbox"))).sendKeys("samsung tv 55 inch");
    driver.findElement(By.cssSelector("input.nav-input[value='Go']")).click();
    String[] cssParts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("h1.s-desktop-toolbar div.sg-col-inner div.a-section>span"))).getText().split(" ");
    System.out.println(cssParts[2]);
    String[] xpathParts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h1[contains(@class, 's-desktop-toolbar')]//div[@class='sg-col-inner']//div[contains(@class, 'a-section')]/span"))).getText().split(" ");
    System.out.println(xpathParts[2]);
    
  • Console Output:

    75
    75
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • why it won't work after setting the string in the search box? what is the problem about it? why do I have to wait for the visiblity of the locator? – kim el Jul 24 '20 at 07:23
  • 1
    As soon as you `click()` the search button using webdriver, the application itself needs time to send the query to the database and when the results comes back the same are rendered within the browser client. So you need to configure the webdriver to wait for the result to arrive before trying to parse the desired string. Hence `ExpectedConditions.visibilityOfElementLocated()` – undetected Selenium Jul 24 '20 at 08:44
  • I tried it out and it returned as expected with using the above logic – kim el Jul 24 '20 at 09:52
1

Try identifying the object by partial text. For me, this returns a unique hit:

//span[contains(text(), 'results for')] 

From that you can get text and it should return the full string.

RichEdwards
  • 3,423
  • 2
  • 6
  • 22