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 Answer
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.