单击classname selenium的元素

时间:2017-03-18 14:35:36

标签: python selenium

我正试图从网站上刮开酒吧的营业时间。有一个酒吧列表,如果您导航到您,可以打开营业时间。如果元素有类名,我在点击元素时遇到问题。

我已编写代码以便从一个静脉中获取时间,但是,我无法从第一个链接导航到每个地点。

当我获得一个场地的时间

时,此代码有效
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)

2 个答案:

答案 0 :(得分:4)

所有带有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)

答案 1 :(得分:0)

我觉得问题是驱动程序在DOM中可用之前尝试找到该元素。尝试等待元素存在,而不是直接尝试找到它。 你可以替换这一行:

venue = driver.find_element_by_xpath('//a[@class="ng-binding"]')

使用:

venue = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//a[@class="ng-binding"]')))

请注意,您需要执行一些导入才能执行此操作:

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

来源:http://selenium-python.readthedocs.io/waits.html