Skip to main content
 首页 » 编程设计

python之单击具有类名 selenium 的元素

2025年05月04日23linjiqin

我正在尝试从网站上抓取酒吧的营业时间。有一个酒吧列表,如果您导航到您所在的位置,则可以找到营业时间。我在点击具有类名的元素时遇到问题。

我已经编写了从一个地点获取营业时间的代码,但是,我无法从第一个链接导航到每个地点。

当我得到一个场所的营业时间时,此代码有效

from selenium import webdriver 
 
driver = webdriver.Firefox() 
 
driver.get('https://www.designmynight.com/london/bars/soho/six-storeys') 
 
hours = driver.find_element_by_xpath('//li[@id="hours"]') 
hours.click() 
 
hoursTable = driver.find_elements_by_css_selector("table.opening-times tr") 
for row in hoursTable: 
    print(row.text) 

问题是当我尝试 navigate to this page我无法点击进入每个链接。谁能看出我做错了什么?

from selenium import webdriver 
 
driver = webdriver.Firefox() 
 
driver.get('https://www.designmynight.com/london/search-results#!?type_of_venue=512b2019d5d190d2978c9ea9&type_of_venue=512b2019d5d190d2978c9ea8&type_of_venue=512b2019d5d190d2978c9ead&type_of_venue=512b2019d5d190d2978c9eaa&type_of_venue=512b2019d5d190d2978c9eab&type=&q=&radius=') 
 
venue = driver.find_element_by_xpath('//a[@class="ng-binding"]') 
venue.click() 
 
//this should then lead me to the following link -> 
driver.get('https://www.designmynight.com/london/bars/soho/six-storeys') 
 
hours = driver.find_element_by_xpath('//li[@id="hours"]') 
hours.click() 
 
hoursTable = driver.find_elements_by_css_selector("table.opening-times tr") 
for row in hoursTable: 
    print(row.text) 

请您参考如下方法:

所有带有 ng-binding 类名的链接都是动态生成的,所以你需要等到链接出现在 DOM 中并且它也是可点击的:

from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.support.ui import WebDriverWait as wait 
from selenium.webdriver.common.by import By 
from selenium import webdriver 
 
driver = webdriver.Firefox() 
 
driver.get('https://www.designmynight.com/london/search-results#!?type_of_venue=512b2019d5d190d2978c9ea9&type_of_venue=512b2019d5d190d2978c9ea8&type_of_venue=512b2019d5d190d2978c9ead&type_of_venue=512b2019d5d190d2978c9eaa&type_of_venue=512b2019d5d190d2978c9eab&type=&q=&radius=') 
 
venue = wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//a[@class="ng-binding"]'))) 
venue.click() 

但是如果你想点击每个链接,我建议你不要点击这些链接,而是获取引用列表,然后按如下所示打开每个链接:

xpath = '//a[@class="ng-binding"]' 
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, xpath))) 
links = [venue.get_attribute('href') for venue in driver.find_elements_by_xpath(xpath)] 
 
for link in links: 
    driver.get(link) 
    hours = driver.find_element_by_xpath('//li[@id="hours"]') 
    hours.click() 
    hoursTable = driver.find_elements_by_css_selector("table.opening-times tr") 
    for row in hoursTable: 
        print(row.text)