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']"}

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • 1
    Add HTML code for this button, then we can help u. Also it can be a problem with wait. – IPolnik Jan 08 '20 at 17:16
  • I am trying to automate an e-commerce site https://www.flipkart.com/ , search for iPhone6s plus, sort a 16gb iPhone and add to cart. 'ADD TO CART' ,html tag is – pri_test Jan 14 '20 at 09:31
  • 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. – IPolnik Jan 14 '20 at 14:21
  • 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 Model – pri_test Jan 15 '20 at 10:00
  • Please paste your code here, I'll try to execute it on my pc, see if it works. – IPolnik Jan 16 '20 at 13:52

6 Answers6

1

Try this one.

By CSS Selector -- .row .col > button

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

Indrajeet
  • 46
  • 2
0

Try this xpath:

.//ul[@class='row']/li/button
Pratik
  • 337
  • 1
  • 9
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')]");
KunduK
  • 26,790
  • 2
  • 10
  • 32
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();
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • 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? – pri_test Jan 12 '20 at 18:15
0

You try with the class also.

Something like this below.

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

Rakesh S M
  • 41
  • 1
  • 7
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);
}
Nandu Raj
  • 1,979
  • 7
  • 19