Error 'Unable to locate element' when trying to locate 'Add to Cart' button on Flipkart
Asked Answered
P

7

0

I am trying to locate Add to Cart button on Flipkart but it does not work

I have tried below xpaths but none works

By AddToCart= By.xpath("//button[@class='_2AkmmA _2Npkh4 _2MWPVK'][text()='ADD TO CART']");

OR

By AddToCart= By.xpath("//button[text()='ADD TO CART']");

//error

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@class='_2AkmmA _2Npkh4 _2MWPVK'][text()='ADD TO CART']"}

Poseidon answered 8/1, 2020 at 17:3 Comment(5)
Add HTML code for this button, then we can help u. Also it can be a problem with wait.Mcglone
I am trying to automate an e-commerce site flipkart.com , search for iPhone6s plus, sort a 16gb iPhone and add to cart. 'ADD TO CART' ,html tag is <button class="_2AkmmA _2Npkh4 _2MWPVK"><svg class="_3oJBMI" width="16" height="16" viewBox="0 0 16 15" xmlns="w3.org/2000/svg"></svg> <!-- -->ADD TO CART</button>Poseidon
Did u try to use Thread.sleep and then click, so we can exclude wait problem? If u will be able to click, then its wait problem, if not then there is something with your code.Mcglone
Yes have used Thread.sleep. The xpath does inspect the button fine but does not work during code execution. I have also tried new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='ADD TO CART']"))).click(); But not sure how I would use this in Page Object ModelPoseidon
Please paste your code here, I'll try to execute it on my pc, see if it works.Mcglone
O
1

Try this one.

By CSS Selector -- .row .col > button

By XPath -- .//button[text()='ADD TO CART']

Osbert answered 9/1, 2020 at 6:3 Comment(0)
Y
0

Try this xpath:

.//ul[@class='row']/li/button
Yokum answered 8/1, 2020 at 17:7 Comment(0)
C
0

Try the following Xpath.

//button[@class='_2AkmmA _2Npkh4 _2MWPVK' and contains(.,'ADD TO CART')]

or

//button[contains(.,'ADD TO CART')]

By AddToCart= By.xpath("//button[@class='_2AkmmA _2Npkh4 _2MWPVK' and contains(.,'ADD TO CART')]");
Catechol answered 8/1, 2020 at 17:17 Comment(0)
D
0

This error message...

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@class='_2AkmmA _2Npkh4 _2MWPVK'][text()='ADD TO CART']"}

...implies that the WebDriver was unable to locate the element.

You were pretty close. To locate and click() on the element with text as ADD TO CART you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("ul.row>li>button"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='ADD TO CART']"))).click();
    
Derna answered 8/1, 2020 at 20:8 Comment(1)
Thanks.But this is still not working for me and throws the same error. I am using Page object model and also tried using JavascriptExecutor to scroll down to make 'ADD TO CART' clickable.Any suggestions?Poseidon
T
0

You try with the class also.

Something like this below.

//button[@class='_2AkmmA _2Npkh4 _2MWPVK']

Torray answered 9/1, 2020 at 6:30 Comment(0)
C
0

I was facing the exact same issue and what I noticed is when the product is selected, a new window opens up and selenium searches for the element in the old page resulting in the error message:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":

So the problem is that you don't switch to the opened window, and webdriver searches for elements in the old page instead of the newly opened one.

This issue got resolved when I tried the below code after clicking on the product and after applying the below, searched for 'Add To Cart' and carried on

Solution:

String winHandleBefore = driver.getWindowHandle();
for(String winHandle : driver.getWindowHandles()){
   driver.switchTo().window(winHandle);
}
Carmacarmack answered 20/5, 2020 at 3:24 Comment(0)
H
0

Selenium C# solution

Yes agree Nandu, new window gets open and focus is still on Old page. Element is not present on old window hence we need to switch to new window. Xpath can be: //button[@class='_2KpZ6l _2U9uOA _3v1-ww' and text()='ADD TO CART']

For Switch to new window use: _driver.SwitchTo().Window(_driver.WindowHandles.Last());

I used Explicit wait to get the element displyed. my code looks like this in method:

_driver.SwitchTo().Window(_driver.WindowHandles.Last());
    Actions action = new Actions(_driver);
    WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(45));
    wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.XPath("//button[text()='ADD TO CART']")));
    action.MoveToElement(additem).Perform();
    additem.Click();
Hirza answered 7/10, 2022 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.