2

I was trying to automate add to cart functionality in the following website, but 'Add To Cart' button is not getting clicked, though element is identified and code has been written to click on the button using Actions class and Javascriptexecutor.

Site: https://redmart.com/sales Button: Add To Cart

Selenium Code:

WebElement element4 =   
driver.findElement(By.xpath("//article[@id='contentSection']   //div[@class='productShelf']//ul/li[1]"));
WebElement element5 = element4.findElement(By.xpath("div[3]/div/a/span"));

actions = new Actions(driver);
actions.moveToElement(element4).moveToElement(element5);
Thread.sleep(3000);

actions.click();
actions.build().perform();

Can someone please suggest a solution which will click on Add To Cart button and added element should be displayed in cart as well?

Arun
  • 67
  • 5

5 Answers5

0

I really think you are selecting the wrong element:

browser.find_element_by_xpath(".//*[@id='contentSection']/div/article/div[2]/div/div/ul/li[1]/div[3]/div/a").click()
browser.find_element_by_xpath(".//*[@id='contentSection']/div/article/div[2]/div/div/ul/li[2]/div[3]/div/a").click()
browser.find_element_by_xpath(".//*[@id='contentSection']/div/article/div[2]/div/div/ul/li[3]/div[3]/div/a").click()
browser.find_element_by_xpath(".//*[@id='contentSection']/div/article/div[2]/div/div/ul/li[4]/div[3]/div/a").click()

Anyway have a look on my answer in this post to ensure that you are getting the correct information ;) python selenium click on button

Carlo 1585
  • 1,455
  • 1
  • 22
  • 40
  • Thank you for the solution. Does the above worked for you? I mean does it clicked Add To Cart of say First Item and displayed in Cart? – Arun Aug 29 '18 at 10:10
  • I try it not because I don't have selenium on this PC, it don't work because it say there is an element in front; I suppose this UI is done in angular JS or similar, it mean that all the objects are on the same page but hide and overlapped, this create many problem with selenium (I can't just ensure that because I'm not sure in which languages is made the UI) – Carlo 1585 Aug 29 '18 at 10:43
0

Please try this xpath:

//ul[contains(@class,'productList')][1]//li[1]//a[contains(@class,'Button')]

It is for first ,,Add to Cart'' button on list. If you want to click other product, just change index of product list or li tags in xpath. Please also try clicking without using Actions.

driver.findElement(By.xpath("//ul[contains(@class,'productList')][1]//li[1]//a[contains(@class,'Button')]").click();

It can be an Actions issue.

I hope it helps!

Deryl
  • 66
  • 5
0

Your xpath is not correct because it doesn't select the button.
Try this xpath "//li[@data-id='88800134']/div[3]/div/a" and let me know if this solve the problem.

driver.findElement(By.xpath("//li[@data-id='88800134']/div[3]/div/a")).click();

Later Edit:

WebDriver driver = new ChromeDriver();
    driver.get("https://redmart.com/sales ");
    driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);

    WebElement element =driver.findElement(By.xpath("//li[@data-id='88800134']/div[3]/div/a"));

    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("arguments[0].click();", element);
BOB
  • 700
  • 2
  • 16
  • 35
  • Try to run instead of your above code only this line: `driver.findElement(By.xpath("//li[@data-id='88800134']/div[3]/div/a")).click();` It will definitely work – BOB Aug 30 '18 at 06:51
  • @Arun You were right. After some research I finally found the solution. See Later Edit of my answer. – BOB Aug 30 '18 at 18:59
  • I'm glad I could help – BOB Aug 31 '18 at 03:48
0

Try with the below xpaths :

//ul[contains(@class,'productList')][1]//li[1]//a[contains(@class,'Button')]/span

This one is for the first item. (//span[text()='Add to cart'])[1]

Shakir
  • 123
  • 1
  • 4
  • 14
0

Try changing replacing your WebElement element5 to the code below:

WebElement element5 = element4.findElement(By.xpath("/div[3]/div/a/span/parent::node()")); because it seems like you are trying to click a span element, that's why it's not doing anything