简体   繁体   English

如何 select 产品评论最多的产品

[英]How to select the product with the most product reviews

The actual test is to go onto ebay, click on the searchbox, type in "iphone 8", then enter, and then click on the product with the highest amount of product ratings.实际测试是把go放到ebay上,点击搜索框,输入“iphone 8”,然后回车,然后点击商品评分最高的商品。 Here is my code so far到目前为止,这是我的代码

`WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
        driver.get("https://www.ebay.com/");
        driver.manage().window().maximize();
        driver.findElement(By.id("gh-ac")).sendKeys("iphone 8", Keys.ENTER);`

The issue here is that the webelement that shows the product reviews isn't the same webelement as the one we need to click on to get on to the product page.这里的问题是显示产品评论的网络元素与我们需要单击以进入产品页面的网络元素不同。

Try to find elements with xPath.尝试查找带有 xPath 的元素。

Below is an example of the first 2 full xPaths for the iPhone 8 found on eBay search results.以下是在 eBay 搜索结果中找到的前 2 个 iPhone 8 完整 xPath 的示例。

Product Review:
    
        /html/body/div[5]/div[5]/div[2]/div[1]/div[2]/ul/li[1]/div/div[2]/div[3]/a/span/span[1]
        
Image:
        /html/body/div[5]/div[5]/div[2]/div[1]/div[2]/ul/li[1]/div/div[1]/div/a/div/img
        
        
Product Review:       
        /html/body/div[5]/div[5]/div[2]/div[1]/div[2]/ul/li[2]/div/div[2]/div[3]/a/span/span[1]

Image:  /html/body/div[5]/div[5]/div[2]/div[1]/div[2]/ul/li[2]/div/div[1]/div/a/div/img

I am seeing a pattern up to the /li[x]我看到了/li[x]之前的模式

You can iterate through the whole list of products on that page and set each product into an object with a link and product review attribute so you can then further process the data.您可以遍历该页面上的整个产品列表,并将每个产品设置为带有链接和产品评论属性的 object,以便您可以进一步处理数据。

Please see the below code and let me know if you face any problem.请查看下面的代码,如果您遇到任何问题,请告诉我。

     WebDriver driver = new ChromeDriver();
     WebDriverWait wait = new WebDriverWait(driver, 30);

     driver.get("https://www.ebay.com/");

    // Enter value in Search textbox and press enter
    WebElement SearchTextbox = wait.until(ExpectedConditions.presenceOfElementLocated(
        (By.xpath("//input[contains(@id,\"gh-\") and @type=\"text\"]"))));
    SearchTextbox.click();
    SearchTextbox.sendKeys("iphone 8");
    SearchTextbox.sendKeys(Keys.ENTER);

    // Collected all the product review WebElements
    List<WebElement> ProductReviews = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy
        (By.xpath("//span[@class=\"s-item__reviews-count\"]/span[1]")));

    // Made a list to accumulate all products number.
    ArrayList<Integer> mylist = new ArrayList<>();

    // Loop through all the WebElement and Extract number from String i.e. "70 product ratings"
    // (Extracting 70 in above case)
    for (WebElement ProdInfo : ProductReviews) {
      try {
        mylist.add(Integer.parseInt(ProdInfo.getText().split("product")[0].trim()));
      } catch (Exception ignored) {
      }
    }

    // Printed mylist to verify
    // and Extracted max number so that we can use that in xpath to search exact element and click on it.
    System.out.println(mylist);
    Integer max_number = Collections.max(mylist);

    driver.findElement(By.xpath(String.format("//span[text()=\"%s product ratings\"]", max_number))).click();

Please do let me now if you face any issue executing the above code.如果您在执行上述代码时遇到任何问题,请立即告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM