如何使用Selenium和Java从https://www.amazon.com内搜索结果摘要中的73个结果的文本1-16中提取文本73

如何使用Selenium和Java从https://www.amazon.com内搜索结果摘要中的73个结果的文本1-16中提取文本73

问题描述:

我正在导航至 https://www.amazon.com/

我正在寻找在搜索框文本字段中设置的三星电视55英寸"

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

然后,我尝试提取(63个结果的文本[参见图像]):

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

这是源html:

要从文本 1-16的三星电视55英寸"的73个结果中提取文本 73 ,您必须诱使定位器策略:

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:

  • 使用 cssSelector 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]);

  • 使用 xpath 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
    

  • 这是完整的解决方案

    • 代码块:

    • 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