I want to get values of items like a discount, original price, discounted price, and then I retrieve the discounted price with calculation and compare with discounted price on the website I get. But the problem I face is my code only picks the values of the first item of the product page.
new WebDriverWait(driver,10).until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div[3]/div[1]/div/div[2]/div/div/div/nav/ul/li[1]/ul/li[6]/a"))).click(); Thread.sleep(1000); ArrayList<WebElement> a = (ArrayList<WebElement>) driver.findElements(By.xpath("//div[@class='price']/div[@class='onsale']")); ArrayList<WebElement> b = (ArrayList<WebElement>) driver .findElements(By.className("was")); ArrayList<WebElement> c = (ArrayList<WebElement>) driver .findElements(By.xpath("//div[@class = 'sale-item icn']")); List<Float> aaaa = new ArrayList<Float>(); List<Float> bbbb = new ArrayList<Float>(); List<Float> cccc = new ArrayList<Float>(); System.out.println(a.size() + " " + b.size() + " " + c.size()); for (int i = 0; i < a.size(); i++) { String[] aaa = a.get(i).getText().replaceAll(",", "").split("\."); System.out.println(aaa[1] + "." + aaa[2]); aaaa.add(Float.parseFloat(aaa[1] + "." + aaa[2])); System.out.println(aaaa.get(i)); String[] bbb = b.get(i).getText().replaceAll(",", "").split("\."); System.out.println(bbb[1] + "." + bbb[2]); bbbb.add(Float.parseFloat(bbb[1] + "." + bbb[2])); System.out.println(bbbb.get(i)); String[] ccc = c.get(i).getText().replaceAll("NEW ARRIVAL", "20").split("\%"); System.out.println(ccc[0]); cccc.add(Float.parseFloat(ccc[0])); System.out.println(cccc.get(i)); float off_cal = bbbb.get(i) - (bbbb.get(i) * cccc.get(i)) / 100; System.out.println(off_cal); WebElement selitem = wait.until(ExpectedConditions .elementToBeClickable(By.xpath("/html/body/div[3]/div[2]/div/div/div[8]/div[1]/div[2]"))); selitem.click(); Thread.sleep(2000); if (off_cal == aaaa.get(i)) { System.out.println("matched"); WebElement addcart1 = wait.until(ExpectedConditions.elementToBeClickable( By.xpath("/html/body/div[3]/div[2]/div[1]/div/section[1]/div[2]/div/form/div[10]/input[2]"))); addcart1.click(); Thread.sleep(2000); System.out.println("Product added to cart"); WebElement notification_close = wait .until(ExpectedConditions.elementToBeClickable(By.id("pn-optin-disallow-btn-text"))); notification_close.click();
Advertisement
Answer
I updated your code a bit:
package selenium; import java.util.ArrayList; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; public class Zaman10 extends WebDriverBase { public static void main(String[] args) throws InterruptedException { WebDriver driver = startChromeDriver(); // standard chromedriver setup driver.get("https://focusclothing.pk/collections/men-gym-wear"); dismissSubPopup(); List<WebElement> a = driver.findElements(By.xpath("//div[@class='price']/div[@class='onsale']")); List<WebElement> b = driver.findElements(By.className("was")); List<WebElement> c = driver.findElements(By.xpath("//div[@class = 'sale-item icn']")); List<Float> aaaa = new ArrayList<Float>(); List<Float> bbbb = new ArrayList<Float>(); List<Float> cccc = new ArrayList<Float>(); System.out.println(a.size() + " " + b.size() + " " + c.size()); for (int i = 0; i < a.size(); i++) { String[] aaa = driver.findElements(By.xpath("//div[@class='price']/div[@class='onsale']")).get(i).getText().replaceAll(",", "").split("\."); String aa = aaa[1] + "." + aaa[2]; System.out.println(aa); aaaa.add(Float.parseFloat(aa)); System.out.println(aaaa.get(i)); String[] bbb = driver.findElements(By.className("was")).get(i).getText().replaceAll(",", "").split("\."); String bb = bbb[1] + "." + bbb[2]; System.out.println(bb); bbbb.add(Float.parseFloat(bb)); System.out.println(bbbb.get(i)); String[] ccc = driver.findElements(By.xpath("//div[@class = 'sale-item icn']")).get(i).getText().replaceAll("NEW ARRIVAL", "20").split("\%"); System.out.println(ccc[0]); cccc.add(Float.parseFloat(ccc[0])); System.out.println(cccc.get(i)); float off_cal = bbbb.get(i) - (bbbb.get(i) * cccc.get(i)) / 100; System.out.println(off_cal); WebElement productLoop = driver.findElement(By.id("product-loop")); List<WebElement> products = productLoop.findElements(By.xpath(".//div[@class='reveal vertical-images']")); products.get(i).click(); WebElement addToCart = driver.findElement(By.xpath("//*[@id="AddToCartForm"]/div[10]/input[2]")); if (off_cal == aaaa.get(i)) { System.out.println("matched"); addToCart.click(); System.out.println("Product added to cart"); } driver.get("https://focusclothing.pk/collections/men-gym-wear"); dismissSubPopup(); } driver.quit(); } public static void dismissSubPopup() throws InterruptedException { Thread.sleep(8000); List<WebElement> closeButtons = driver.findElements(By.xpath("/html/body/div[4]/div/div[2]/div[4]/div/div/button")); if (closeButtons.size() > 0) { closeButtons.get(0).click(); } } }
and got this output:
Starting ChromeDriver 91.0.4472.101 (af52a90bf87030dd1523486a1cd3ae25c5d76c9b-refs/branch-heads/4472@{#1462}) on port 12891 Only local connections are allowed. Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe. ChromeDriver was started successfully. Čvc 14, 2021 9:50:31 DOP. org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: W3C 17 17 17 976.50 976.5 1395.00 1395.0 30 30.0 976.5 matched Product added to cart 836.50 836.5 1195.00 1195.0 30 30.0 836.5 matched Product added to cart 836.50 836.5 1195.00 1195.0 30 30.0 836.5 matched Product added to cart 1256.50 1256.5 1795.00 1795.0 30 30.0 1256.5 matched Product added to cart 836.50 836.5 1195.00 1195.0 30 30.0 836.5 matched Product added to cart 976.50 976.5 1395.00 1395.0 30 30.0 976.5 matched Product added to cart 976.50 976.5 1395.00 1395.0 30 30.0 976.5 matched Product added to cart 847.50 847.5 1695.00 1695.0 50 50.0 847.5 matched Product added to cart 836.50 836.5 1195.00 1195.0 30 30.0 836.5 matched Product added to cart 836.50 836.5 1195.00 1195.0 30 30.0 836.5 matched Product added to cart 836.50 836.5 1195.00 1195.0 30 30.0 836.5 matched Product added to cart 847.50 847.5 1695.00 1695.0 50 50.0 847.5 matched Product added to cart 847.50 847.5 1695.00 1695.0 50 50.0 847.5 matched Product added to cart 976.50 976.5 1395.00 1395.0 30 30.0 976.5 matched Product added to cart 976.50 976.5 1395.00 1395.0 30 30.0 976.5 matched Product added to cart 847.50 847.5 1695.00 1695.0 50 50.0 847.5 matched Product added to cart 1186.50 1186.5 1695.00 1695.0 30 30.0 1186.5 matched Product added to cart