1

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()
  • What is the exact problem you are having/facing? What did you tried to fix your problem? Please mention all those things in question. Please [edit] your question with these things. – imxitiz Jul 16 '21 at 07:43
  • 1
    in the cart xpath you have mentioned buttons instead of that mention button – YaDav MaNish Jul 16 '21 at 07:47
  • @YaDavMaNish I tried not working . I copied the path from chrome inspect element – user3227620 Jul 16 '21 at 07:56
  • You are trying to click on the ADD TO CART button right? – YaDav MaNish Jul 16 '21 at 07:59
  • @YaDavMaNish yes but somehow not able to locate element . is it because new tab get open when we click on any item . because after searching mobile keyword on search bar I clicked on first item then used driver.close command to close the tab but only single tabs get closed first tab not the item which I selected tab . If it helps . – user3227620 Jul 16 '21 at 08:03

1 Answers1

0

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()
Prophet
  • 32,350
  • 22
  • 54
  • 79