【问题标题】:Select second product from product list in amazon using Selenium -JAVA使用 Selenium -JAVA 从亚马逊的产品列表中选择第二个产品
【发布时间】:2021-03-19 21:44:21
【问题描述】:

我正在尝试使用 selenium 抓取亚马逊。

我已经编写了一个脚本,但我无法从列表中选择产品。每次使用selenium检索到的list长度都不一样,是不是xpath有问题?

任何帮助将不胜感激。

步骤:

  1. 获取网址 (amazon.com)
  2. 搜索充电器
  3. 从列表中选择第 10 个产品。

到目前为止我的尝试如下:

public class AmazonSearch {

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "E:\\geckodriver-v0.29.0-win64\\chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.amazon.com");
        driver.findElement(By.id("twotabsearchtextbox")).click();
        driver.findElement(By.id("twotabsearchtextbox")).clear();
        System.out.println("Hello world!");
        driver.findElement(By.id("twotabsearchtextbox")).sendKeys("charger");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElement(By.id("nav-search-bar-form")).submit();
        driver.findElement(By.id("a-autoid-0-announce")).click();
        System.out.println("Hello world!");

        driver.findElement(By.id("s-result-sort-select_2")).click();
        List<WebElement> resultsList = driver.findElements(By.xpath(".//span[@class='a-size-medium a-color-base a-text-normal']"));
        int size = resultsList.size();
        System.out.println("Size of list = " + size);
        resultsList.get(2).click();
    }

【问题讨论】:

  • 选择第二个产品有什么问题?为什么总是使用隐式等待?你听说过明确或流畅的等待吗?
  • 我正在测试隐式等待,将其删除。问题是我无法找到正确的路径,我可以将其填充到列表中,或者将产品选择为类似于 .get(int) 的第 2 或第 3 或第 4 个产品。希望你明白吗?
  • 我已经编辑了问题

标签: java selenium selenium-webdriver selenium-chromedriver


【解决方案1】:
  • //span[text()='相关搜索'] xpath 查找包含“相关搜索”跨度文本的元素。在搜索结果中搜索 (ctrl + f) '相关搜索'。为确保以一种方式显示页面加载 - 有多种不同的方式。
  • 增加等待时间以查找元素并处理超时异常 - 执行以下操作:

公共类 AmazonSearch {

public static void main(String[] args)
{
    try
    {
        System.setProperty("webdriver.chrome.driver", "E:\\geckodriver-v0.29.0-win64\\chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.amazon.com");
        driver.findElement(By.id("twotabsearchtextbox")).click();
        driver.findElement(By.id("twotabsearchtextbox")).clear();
        System.out.println("Hello world!");
        driver.findElement(By.id("twotabsearchtextbox")).sendKeys("charger");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElement(By.id("nav-search-bar-form")).submit();
        driver.findElement(By.id("a-autoid-0-announce")).click();
        System.out.println("Hello world!");

        driver.findElement(By.id("s-result-sort-select_2")).click();
        WebElement we = new WebDriverWait(driver, 60).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Related searches']")));
        List<WebElement> resultsList = driver.findElements(By.xpath(".//span[@class='a-size-medium a-color-base a-text-normal']"));
        int size = resultsList.size();
        System.out.println("Size of list = " + size);
        resultsList.get(2).click();
    }catch(TimeoutException e) 
    {
        System.out.println(e);
        }   
}

}

【讨论】:

    【解决方案2】:

    有时WebElements 插入resultsList 是在加载页面中的所有充电器项目之前完成的,有时是精确的。首先确保页面已加载,然后插入项目。您可以使用“显式等待”。请尝试以下操作:

    WebElement we = new WebDriverWait(driver, 25)
     .until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Related searches']")));
    
    List<WebElement> resultsList = driver.findElements(By.xpath(".//span[@class='a-size-medium a-color-base a-text-normal']"));
    

    【讨论】:

    • 它实际上正在工作,我正在测试它,但如果它在任何时候崩溃。你能解释一下//span[text()='Related searches']这是怎么找到的吗?会很好。我注意到有时它会崩溃并显示无法找到 xpath 的错误消息。
    猜你喜欢
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多