0

I am trying to automate the Amazon website as i m selecting a product then that particular product is getting open in next tab and on the same tag there is button of Add to cart. selenium is not finding the add to cart button as i try many ways. my product is getting open in new tab so i used the code to switch in next tab but still its not finding an add to cart button, can anyone please help me to solve out this problem Thanks in Advance..!!

1
  • 1
    Could you please help us with your code and the error log. Nov 1, 2018 at 6:49

1 Answer 1

2

I tried the same thing and able to click on the cart button.

Please find the code.


package dropDowns;

import java.util.Iterator;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class Amazon {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.amazon.in");
        driver.findElement(By.cssSelector("input[id='twotabsearchtextbox']")).sendKeys("soap");
        driver.findElement(By.cssSelector("input[id='twotabsearchtextbox']")).sendKeys(Keys.ENTER);
        driver.findElement(By.xpath("//*[contains(text(),'Park Avenue Soap')]")).click();
        Set<String> ids = driver.getWindowHandles();
        Iterator<String> it = ids.iterator();
        String parentId = it.next();
        String childId = it.next();
        driver.switchTo().window(childId);
        driver.findElement(By.id("add-to-cart-button")).click();
    }

}

Hope this will be useful.

1
  • The above answer worked perfectly. Aug 3, 2021 at 5:03

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.