【问题标题】: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如何使用 Selenium 和 Java 从 https://www.amazon.com 内的搜索结果摘要中的 73 个结果中的文本 1-16 中提取文本 73
【发布时间】:2020-11-13 10:54:19
【问题描述】:

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

我正在寻找 'samsung tv 55 inch' 在搜索框文本字段中设置它

然后我正在尝试提取(63 个结果 [见附图])的文本:

我找不到正确的定位器以及如何找到它,这是我的代码:

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");
    }


}

我正在努力寻找正确的定位器以获取结果文本

这是源代码:

【问题讨论】:

  • 您可能需要多个选择器,因为 AMZN 喜欢交替布局。

标签: java selenium xpath css-selectors webdriverwait


【解决方案1】:

要从文本 1-16 of 73 results for "samsung tv 55 inch" 中提取文本 73,您必须为 visibilityOfElementLocated() 诱导 WebDriverWait您可以使用以下任一Locator Strategies

  • 使用 cssSelectorsplit():

    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]);
    
  • 使用 xpathsplit():

    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]);
    
  • 控制台输出:

    72
    

完整的代码块

这里是完整的解决方案

  • 代码块:

    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]);
    
  • 控制台输出:

    75
    75
    

【讨论】:

  • 为什么在搜索框中设置了字符串后不起作用?它有什么问题?为什么我必须等待定位器的可见性?
  • 一旦您使用 webdriver click() 搜索按钮,应用程序本身需要时间将查询发送到数据库,并且当结果返回时,相同的结果会在浏览器客户端中呈现。因此,您需要将 webdriver 配置为等待结果到达,然后再尝试解析所需的字符串。因此ExpectedConditions.visibilityOfElementLocated()
  • 我试了一下,使用上面的逻辑按预期返回了
【解决方案2】:

尝试通过部分文本来识别对象。对我来说,这会返回一个独特的命中:

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

您可以从中获取文本,它应该返回完整的字符串。

【讨论】:

    猜你喜欢
    • 2019-07-27
    • 2018-02-05
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多