0

I have a product list and some products have discount available. I want to click buy if discount price text is available. Below is a product with discount

<article
    class="commerce-product--phones--teaser commerce-product--teaser">
    <div class="no-promotions-block"></div>
    <div class="product-taeser-view">
        <div class="product-taeser-block1">
            <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-11/Screenshot%202022-11-03%20131435.png?itok=4ZWrQCJM"
                        width="97" height="100" alt="Alternative_02" typeof="foaf:Image"
                        class="image-style-product-thumbnail">
                </div>
            </div>
            <div class="product-taeser-block2">
                <div
                    class="field field--name-title field--type-string field--label-hidden field--item">SAMSUNG GALAXY A5 (EN)</div>
                <div class="price-label">Discount Price</div>
                <div
                    class="product--variation-field--variation_price__14 field field--name-price field--type-commerce-price field--label-hidden field--item">Rs. 50,990.00</div>
                <span class="list-price">Rs. 51,010.49</span>
                <span class="discount-text">0% OFF</span>
            </div>
        </div>
        <div
            class="clearfix text-formatted field field--name-field-mini-specification field--type-text-long field--label-hidden field--item">
            <p>&nbsp;&nbsp;
            </p>
        </div>
        <div class="buy-now-button">
            <a href="/product/14" class="commerce-product--catalog__hover-text">BUY NOW</a>
        </div>
    </div>
</article>

Below is product without discount

<article
    class="commerce-product--phones--teaser commerce-product--teaser">
    <div class="no-promotions-block"></div>
    <div class="product-taeser-view">
        <div class="product-taeser-block1">

            <div
                class="product--variation-field--variation_field_images__5 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/Black%201.jpg?itok=hUs-qGRy"
                        width="55" height="100" alt="Front view" typeof="foaf:Image"
                        class="image-style-product-thumbnail">


                </div>
            </div>

            <div class="product-taeser-block2">

                <div
                    class="field field--name-title field--type-string field--label-hidden field--item">iPhone 12</div>

                <div class="price-label">Price</div>

                <div
                    class="product--variation-field--variation_price__5 field field--name-price field--type-commerce-price field--label-hidden field--item">Rs. 3,000.00</div>

                <span class="list-price"></span>
                <span class="discount-text">&nbsp;
                </span>
            </div>
        </div>

        <div
            class="clearfix text-formatted field field--name-field-mini-specification field--type-text-long field--label-hidden field--item">
            <ul>
                <li>6.1 inch Super Retina XDR display</li>
                <li>5G
                    mmWave and sub-6GHz&nbsp;5G
                    mmWave and sub-6GHz&nbsp;5G
                    mmWave and sub-6GHz&nbsp;5G
                    mmWave and sub-6GHz
                </li>
                <li>5G mmWave and sub-6GHz</li>
            </ul>
        </div>

        <div class="buy-now-button">
            <a href="/product/5" class="commerce-product--catalog__hover-text">BUY NOW</a>
        </div>
    </div>
</article>

below is the code that I have written to click buy now in discounted product

public WithDiscountNavigationPage navigateToDiscountProduct() {
        
        List<WebElement> articles = driver.findElements(By.xpath("//article[@class='commerce-product--phones--teaser commerce-product--teaser']"));
        
        for(WebElement article: articles) {
            
            Boolean discount = article.findElement(By.xpath("//div[text()='Discount Price']")).isDisplayed();
            
            System.out.println(discount);
            
            if(discount == true) {  
                
                String productName = article.findElement(By.xpath("//div[@class='field field--name-title field--type-string field--label-hidden field--item']")).getText();
                
                System.out.println(productName);
                
                WebElement buy = driver.findElement(By.xpath("//a[text()='BUY NOW']"));
                
                JavascriptExecutor js = (JavascriptExecutor) driver;
                js.executeScript("arguments[0].click();", buy);
                
                Waiting_Time_MEDIUM();
                
                String pdpName = driver.findElement(By.xpath("//div[@class='field field--name-title field--type-string field--label-hidden field--item']")).getText();
                
                System.out.println(pdpName);
                
                Assert.assertEquals(productName, pdpName);
                
                break;
                
            }
        }
        
        return new WithDiscountNavigationPage();
    }

The problem is that it identifies the discount price text but clicking buy now in a different product in loop. How could I change this code to click buy now in discounted product?

2 Answers 2

1

Your code as written will find the first "Buy Now" button on the page.

What you want is:

WebElement buy = article.findElement(By.xpath("//a[text()='BUY NOW']"));
1
  • I have tried this as well. but still clicking the first buy now button Commented Dec 12, 2022 at 4:55
0

In software testing company, These are the commom issues we face in our automation. In your scenario looks like there are multiple products and each product has 'Buy Now' button. As per my observation the element 'WebElement buy = driver.findElement(By.xpath("//a[text()='BUY NOW']"));' count should be more than 1. So, we need to create a dynamic locator w.r.t the product.

Can you try below code which gives list of 'Buy Now' elements of discounted products and can click w.r.t index:

List element_BuyNow = driver.findElements(By.xpath("//div[@class='product-taeser-block2']//span[contains(text(), 'OFF')]/../..//a[text()='BUY NOW']"));

element_BuyNow[0].click();

Your Answer

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

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