skip to Main Content

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. 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.

2

Answers


  1. Try to find elements with xPath.

    Below is an example of the first 2 full xPaths for the iPhone 8 found on eBay search results.

    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]

    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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search