pythonseleniumselenium-webdriverselenium-chromedriverflipkart-api

selenium in python not able to locate add to cart element on Amazon/Flipkart


I am new to this,trying to make first automated test case by searching on flipkart website then search mobiles and click on the specific mobile and then add to cart but it is not working getting this error selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element ... is not clickable at point (728, 232). and tried by finding css selector/xpath/id/name none of them are working but when I call dirct link page using get method its working. Any help would be be appreciated.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from time import sleep

driver = webdriver.Chrome('/usr/local/bin/chromedriver')
driver.get("https://www.flipkart.com/")
sleep(1)

print(" site title : "+driver.title)
btn=driver.find_element_by_xpath('//*[@class="_2KpZ6l _2doB4z"]')
btn.click()

search_bar = driver.find_element_by_name("q")
search_bar.clear()
search_bar.send_keys("mobiles")
search_bar.send_keys(Keys.RETURN)
print(" listing page ::")
sleep(1)

a_link = driver.find_element_by_xpath('//*[@id="container"]/div/div[3]/div[1]/div[2]/div[2]/div/div/div/a/div[2]/div[1]/div[1]')
a_link.click()
print(" main page : ")
sleep(3)

cart = driver.find_element_by_xpath('//*[@id="container"]/div/div[3]/div[1]/div[1]/div[2]/div/ul/li[1]/buttons')
cart.click()

Solution

  • There are several issues here:

    1. You should use explicit waits of webdriverwait expected conditions instead of hardcoded sleeps.
    2. Should not use automatically generated locators like this //*[@id="container"]/div/div[3]/div[1]/div[2]/div[2]/div/div/div/a/div[2]/div[1]/div[1]
    3. By clicking on search result link a_link.click() the new window is opened. You need to switch there.
      After all these changes your code should look like this and I hope it will work better:
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from time import sleep
    
    driver = webdriver.Chrome('/usr/local/bin/chromedriver')
    wait = WebDriverWait(driver, 20)
    
    driver.get("https://www.flipkart.com/")
    sleep(1)
    
    print(" site title : "+driver.title)
    wait.until(EC.visibility_of_element_located((By.XPATH, "//*[@class="_2KpZ6l _2doB4z"]"))).click()
    
    search_bar = wait.until(EC.visibility_of_element_located((By.NAME, "q")))
    search_bar.clear()
    search_bar.send_keys("mobiles")
    search_bar.send_keys(Keys.RETURN)
    print(" listing page ::")
    
    
    a_link = wait.until(EC.visibility_of_element_located((By.XPATH, "(//div[@class='_4rR01T'])[1]")))
    a_link.click()
    print(" main page : ")
    
    sleep(0.4)
    
    driver.switch_to.window(driver.window_handles[-1])
    
    wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button._3v1-ww"))).click()