After selecting a product and opening it in another page Im not able to click on add to cart button in amazon using selenium.
Asked
Active
Viewed 2,031 times
0
-
1What is the Page URL ? Where you are stuck exactly ? – cruisepandey Aug 02 '21 at 04:46
2 Answers
0
If you are not switching to other page, Add to card
option will not be located.
Switch to the opened page and then try to click on the button.
handles = driver.window_handles
driver.switch_to.window(handles[1])
driver.find_element_by_id("add-to-cart-button").click()
If you have done this, share the code you have tried and error you get.

pmadhu
- 3,373
- 2
- 11
- 23
-
-
@cruisepandey - When a product is clicked in amazon, the product details opens up in a new window. And Option for "Add to cart" will in the 2nd window. – pmadhu Aug 02 '21 at 05:39
-
No, I don't think so, at least not for me. that's why we need more details from OP instead of just giving random answers. – cruisepandey Aug 02 '21 at 05:40
-
And my random answer has this too. `If you have done this, share the code you have tried and error you get.` – pmadhu Aug 02 '21 at 05:42
-
you have more than 75 reputation, that could have been a comment. – cruisepandey Aug 02 '21 at 05:56
-
refer to my code here please and please help me with this. https://i.stack.imgur.com/ZUhyl.png https://i.stack.imgur.com/NI9Ph.png https://i.stack.imgur.com/olyCc.png – Aparna Murali Aug 02 '21 at 13:15
-
How do we write code for window handles if we are using baseclass? Also Iam using Pojo class for finding element.Iam using cucumber bdd for writing feature file – Aparna Murali Aug 02 '21 at 13:24
-
@AparnaMurali - I am not proficient in JAVA, but you need incorporate a function in Baseclass to switch the windows. Refer the link. [Window handle question](https://stackoverflow.com/q/59291995/16452840) and also there is another answer in JAVA which is exactly what you are asking for. – pmadhu Aug 02 '21 at 13:55
-
0
Without HTML page you are trying to perform, it is difficult to pinpoint exactly. However, I would like to take a stab at it considering these:
- Click on product in home page
- Product opens in a new tab window
- Click on add to cart
P.S: I will write it using Java since you haven't mentioned on the language being used. Will be similar in others as well
Click on product in home page
List <Webelement> productCheck = driver.findElements(By.xpath("<xpath of the product>"))
if (productCheck.size() == 0){
// do something
else {
driver.findElement(By.xpath("<xpath of the product>")).click();
}
Product opens in a new tab window and Click on add to cart
String parent = driver.getWindowHandle();
List<String> windows = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(windows.get(1));
// Inside the tab window
List <WebElement> addtoCartButton = driver.findElements(By.xpath("<xpath of Add to cart button>"));
if (addtoCartButton.size() == 0 ) {
// do something
else {
driver.findElement(By.xpath("<xpath of Add to cart button>")).click();
}
// do whatever you want in the new tab and if you want to switch back then:
driver.switchTo().window(parent);

Adarsh Kumar GM
- 164
- 1
- 4
- 17