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>
</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">
</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 5G
mmWave and sub-6GHz 5G
mmWave and sub-6GHz 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?