消息:元素点击被拦截

Message: element click intercepted

我正在尝试从该网站获取餐厅名称和地址:[https://www.kravekar.com/restaurants][webPage]

问题 是每次我 return 到主页时,我都会得到这个错误:

Element <div class="restaurant-card">...</div> is not clickable at point (1129, 435). Other element would receive the click: <i class="fa fa-spinner fa-pulse"></i>

我尝试执行驱动程序刷新,但睡眠时间不工作。我在第三次迭代中遇到了同样的错误。

到目前为止,这是我的可重现代码:

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)")

当您执行 btn.click()driver.execute_script("window.history.go(-1)") 时,可能会丢失对正确网页的引用。所以最好直接从首页存储所有餐厅的url,然后循环存储的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")

输出

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