0

I'm trying to loop through a list of products and identify a product with discount/promotion. if product with discount is available I want to go inside that product clicking buy now and exist for loop. From what I have tried all the steps are executed but I was not navigated to product details page. Even though the first product doesn't have promotional text if block is executed.

 <div class="slick-track" style="opacity: 1; width: 2448px; transform: translate3d(0px, 0px, 0px);">
   <div class="views-row slick-slide slick-current slick-active" style="width: 286px;" tabindex="0" data-slick-index="0" aria-hidden="false">
      <article class="commerce-product--phones commerce-product--catalog">
         <div class="no-promotions-block"></div>
         <div class="product-catalog">
            <div class="field field--name-title field--type-string field--label-hidden field--item">SAMSUNG GALAXY A5</div>
            <div class="product--variation-field--variation_field_images__14 field field--name-field-images field--type-image field--label-hidden field--items">
               <div class="field--item">  <img loading="lazy" src="https://dialog-qa-bucket.s3.ap-southeast-1.amazonaws.com/s3fs-public/styles/product_thumbnail/public/2022-06/phone_Samsung_2.png?itok=4XnjLLPj" width="92" height="100" alt="Alternative_02" typeof="foaf:Image" class="image-style-product-thumbnail">
               </div>
            </div>
            <div class="product--variation-field--variation_price__14 field field--name-price field--type-commerce-price field--label-hidden field--item">Rs. 0.00</div>
            <div class="list-price">
               Original Price&nbsp;
               <div class="product--variation-field--variation_list_price__14 field field--name-list-price field--type-commerce-price field--label-hidden field--item">Rs. 165,000.00</div>
            </div>
            <div class="buy-now-button">
               <a href="/product/14" class="commerce-product--catalog__hover-text" tabindex="0">BUY NOW</a>
            </div>
         </div>
      </article>
   </div>
   <div class="views-row slick-slide slick-active" style="width: 286px;" tabindex="0" data-slick-index="1" aria-hidden="false">
      <article class="commerce-product--phones commerce-product--catalog">
         <div class="promotions-block">
            <div class="promotions-text">
               <div class="field field--name-field-offer-tag-line field--type-string field--label-hidden field--item">Special price 10% OFF</div>
            </div>
         </div>
         <div class="product-catalog">
            <div class="field field--name-title field--type-string field--label-hidden field--item">Samsung Note 10 - V3</div>
            <div class="product--variation-field--variation_field_images__10 field field--name-field-images field--type-image field--label-hidden field--items">
               <div class="field--item">  <img loading="lazy" src="https://dialog-qa-bucket.s3.ap-southeast-1.amazonaws.com/s3fs-public/styles/product_thumbnail/public/2022-04/gsmarena_005.jpg?itok=fgpdOvFL" width="96" height="100" alt="Samsung Galaxy S21 - V3" typeof="foaf:Image" class="image-style-product-thumbnail">
               </div>
            </div>
            <div class="product--variation-field--variation_price__10 field field--name-price field--type-commerce-price field--label-hidden field--item">Rs. 179,000.00</div>
            <div class="list-price">
               Original Price&nbsp;
               <div class="product--variation-field--variation_list_price__10 field field--name-list-price field--type-commerce-price field--label-hidden field--item">Rs. 179,000.00</div>
            </div>
            <div class="buy-now-button">
               <a href="/product/10" class="commerce-product--catalog__hover-text" tabindex="0">BUY NOW</a>
            </div>
         </div>
      </article>
   </div>
   <div class="views-row slick-slide slick-active" style="width: 286px;" tabindex="0" data-slick-index="2" aria-hidden="false">
      <article class="commerce-product--phones commerce-product--catalog">
         <div class="promotions-block">
            <div class="promotions-text">
               <div class="field field--name-field-offer-tag-line field--type-string field--label-hidden field--item">Special promotion 30% OFF</div>
            </div>
         </div>
         <div class="product-catalog">
            <div class="field field--name-title field--type-string field--label-hidden field--item">OPPO 60 SE - V4101</div>
            <div class="product--variation-field--variation_field_images__1 field field--name-field-images field--type-image field--label-hidden field--items">
               <div class="field--item">  <img loading="lazy" src="https://dialog-qa-bucket.s3.ap-southeast-1.amazonaws.com/s3fs-public/styles/product_thumbnail/public/2022-03/BE2011%201_0.jpg?itok=QOq2qFGz" width="79" height="100" alt="Front view" typeof="foaf:Image" class="image-style-product-thumbnail">
               </div>
            </div>
            <div class="product--variation-field--variation_price__1 field field--name-price field--type-commerce-price field--label-hidden field--item">Rs. 110,000.00</div>
            <div class="list-price">
               Original Price&nbsp;
               <div class="product--variation-field--variation_list_price__1 field field--name-list-price field--type-commerce-price field--label-hidden field--item">Rs. 110,000.00</div>
            </div>
            <div class="buy-now-button">
               <a href="/product/8" class="commerce-product--catalog__hover-text" tabindex="0">BUY NOW</a>
            </div>
         </div>
      </article>
   </div>
</div>

Code of the page class.

public WithDiscountNavigationPage navigateToDiscountProduct() {

//product article
List<WebElement> article = driver.findElements(By.xpath("//article[@class='commerce-product--phones commerce-product--catalog']"));

for(WebElement product: article) {
    
    //locate promotional element
    WebElement discountText = product.findElement(By.xpath("//div[@class='promotions-text']"));

    if(discountText.isDisplayed()) {

        //locate buy now button
        WebElement buyNow = product.findElement(By.xpath("//a[text()='BUY NOW']"));
        
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("arguments[0].click();", buyNow);
        
        System.out.println("clicked buy now");
        
        break;
                
    }
    
}

        
// return class object
return new WithDiscountNavigationPage();        

}

I'm not getting any error and test case execution passed.

What is wrong with my code?

0

2 Answers 2

2

The following piece of code worked for me.

// product article
List<WebElement> article = driver
        .findElements(By.xpath("//article[@class='commerce-product--phones commerce-product--catalog']"));
for (WebElement product : article) {
    // locate promotional element
    WebElement discountText = product.findElement(By.xpath("//div[@class='promotions-text']"));
    if (discountText.isDisplayed()) {
        // locate buy now button
        product.findElement(By.xpath("//div[@class='promotions-text']/parent::div[@class='promotions-block']/following-sibling::div[@class='product-catalog']/div[@class='buy-now-button']/a[text()='BUY NOW']")).click();
        System.out.println("clicked buy now");
        break;
    }
}
Thread.sleep(2000);
driver.navigate().back();

What I did is,

  1. Fetched all products.
  2. Then in a for loop checked if the elements have a promotion/discount.
  3. First element didn't have a discount, so it was skipped.
  4. Second element has discount, so I looked up the parent of the promotion text. then followed to the parent's sibling and then through to the link (Buy Now).
  5. I found the link and clicked it. I was redirected to product page for second product.
  6. After this the for loop was exited with the break statement. So 3 product was not checked.
0

My observation based on details & code you shared :

  • You should NOT use //locate promotional element because if promotional discount is available it will work but what if product/article is not having promotional discount?

WebElement discountText = product.findElement(By.xpath("//div[@class='promotions-text']"));

  • Script will keep looking for above discountText & it will fail. Even try to avoid discountText.isDisplayed()

  • Instead you should try

product.findElements(By.xpath("//div[@class='promotions-text']")).isEmpty()

  • If its empty then neglect and proceed [But it will not get fail while execution]
  • If its NOT empty then

product.findElement(By.xpath("//a[text()='BUY NOW']")).click()

  • Why specifically you are using JavascriptExecutor ?

  • Note : Please check keeping product NOT having promotional discount at the start of the product list and one with promotional discount.

  • Understand, I suggested is just a one way of doing/implementing it. There will be other ways to achieve this.

6
  • Using isEmpty() also not worked. Because it returns false for every product article even it should be true. Aug 4, 2022 at 13:13
  • I see. This means we need to change the identified element. We are using incorrect element. Aug 4, 2022 at 14:02
  • @DarshaniKaushalya Is it working now for you? Highly appreciate if you respond quickly Aug 5, 2022 at 4:48
  • No there is no other element to identify whether product has discount. I'm still trying to find a way Aug 5, 2022 at 5:21
  • I see. Do not think of directly changing element. You can try to change locator may be it will work Aug 5, 2022 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.