How to extract the text 73 from the text 1-16 of 73 results from the search result summary within https://www.amazon.com using Selenium and Java

ε祈祈猫儿з 提交于 2021-02-11 14:24:35

问题


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]):

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:


回答1:


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
    



回答2:


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.



来源:https://stackoverflow.com/questions/63058912/how-to-extract-the-text-73-from-the-text-1-16-of-73-results-from-the-search-resu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!