Jorge Gill
Jorge Gill

Reputation: 315

Selenium unable to click on elements while accessing a webpage using Selenium and Python

I'm trying to scrape this page. Before getting into the page listings, a Select Location window pops up, so I'm trying to tell selenium to click two buttons in order to access the product listings.

Problem is, Selenium is not able to locate the xpath I'm using to locate this two buttons!

Here's my code:

from selenium import webdriver
driver = webdriver.Chrome("webdriver/chromedriver.exe")
driver.implicitly_wait(30)
driver.get("https://www.indiacashandcarry.com/shop/HomestyleFood")

locationButton = driver.find_element_by_xpath('//*[@id="location-list"]/li[1]/h4/a')
groceriesButton = driver.find_element_by_xpath('//*[@id="price-list-0"]/ul/li[1]')
locationButton.click()
groceriesButton.click()

Here's the site: https://www.indiacashandcarry.com/shop/HomestyleFood

I'm thinking it is because this popup is on other type of frame, but I couldn't find any iframe index, so I'm a bit lost. Please help!

Upvotes: 0

Views: 57

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193108

On the website https://www.indiacashandcarry.com/shop/HomestyleFood first to click() on Select This Location associated with FREMONT and then click() on Groceries you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following solution:

  • Code Block:

    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    # options.add_argument('disable-infobars')
    options.add_argument('--disable-extensions')
    driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://www.indiacashandcarry.com/shop/HomestyleFood")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h4[contains(., 'Fremont')]/a"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h5[@class='mtopbot5 ng-binding' and contains(., 'Groceries')]"))).click()
    
  • Browser Snapshot:

Fremont_Groceries

Upvotes: 1

KunduK
KunduK

Reputation: 33384

Your xpath looks fine.Use Webdriverwait to handle dynamic element.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome("webdriver/chromedriver.exe")
driver.get("https://www.indiacashandcarry.com/shop/HomestyleFood")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="location-list"]/li[1]/h4/a'))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="price-list-0"]/ul/li[1]'))).click()

Upvotes: 2

Related Questions