Home > Enterprise >  Message: element click intercepted
Message: element click intercepted

Time:05-12

I am trying to get the name of the restaurant and the address from this website: [https://www.kravekar.com/restaurants][webPage]

The issue is that each time I return to the main page, I get this error:

Element <div >...</div> is not clickable at point (1129, 435). Other element would receive the click: <i ></i>

I tried to implement a driver refresh, a time sleep but is not working. I got the same error in the third iteration.

So far this is my reproducible code:

driver.get('https://www.kravekar.com/restaurants')
comment_button = driver.find_elements(by =By.CSS_SELECTOR, value = "div.restaurant-card")

result = []

for btn in comment_button :
 btn.click()

 try:
    name = driver.find_element(by=By.XPATH, value = '//*. 
    [@id="restaurant_menu_head"]/div/div/div[2]/div[1]/div/div/h4')
    name = name.text
    print(name)
    
    address = driver.find_element(by = By.XPATH, value = '//* 
    [@id="restaurant_menu_head"]/div/div/div[2]/div[1]/div/div/div/span')
    address = address.text
    print(address)
except:
    print("No address or name")
driver.execute_script("window.history.go(-1)")

CodePudding user response:

When you do btn.click() or driver.execute_script("window.history.go(-1)") it might be possible that the reference to the correct webpage is lost. So it is better to store the url of all the restaurants right from the home page, and then loop over the stored urls.

driver.get('https://www.kravekar.com/restaurants')

cards = driver.find_elements(By.CSS_SELECTOR, ".restaurant-card-wrapper")

urls = [card.get_attribute('href') for card in cards]
names = [card.find_element(By.CSS_SELECTOR, ".restaurant-card-title").text for card in cards]

for idx,url in enumerate(urls):
    try:
        driver.get(url)
#         name = driver.find_element(By.CSS_SELECTOR, "#tab_menu_info h4.media-heading").text
        print(names[idx])
        address = driver.find_element(By.CSS_SELECTOR, "#tab_menu_info .restaurant_menu_info-addresss").text
        print(address)
    except:
        print("No address or name")

which outputs

Arby's
51171 Highway 6 & 24, Glenwood Springs, CO 81601
Springs Bar and Grill
722 Grand Ave, Glenwood Springs, CO 81601
etc.
  • Related