使用Python Selenium Chrome驱动程序点击Zomato上的元素

时间:2017-07-28 09:07:40

标签: python selenium selenium-chromedriver

我正在努力收集Zomato的所有提交评论时间。不是来自" Popular"选项卡,但来自"所有评论"标签

对我来说,我必须点击"所有评论"选项卡,但我很难做到这一点,我的代码继续打印来自" Popular"标签

输出:

  • 21天前
  • 一个月前
  • 一个月前
  • 2个月前
  • 2个月前
  • 2个月前

预期产出:

  • 昨天
  • 4天前
  • 4天前
  • 13天前
  • 14天前

注意:输出显示此问题发布之日代码的结果。

"Review Tabs"

from selenium import webdriver
import os

chromeDriver = "....\\chromedriver"
URL = "https://www.zomato.com/cebu/the-pyramid-lahug-cebu-city"
os.environ["webdriver.chrome.driver"] = chromeDriver
driver = webdriver.Chrome(chromeDriver)
driver.get(URL)

driver.find_elements_by_xpath("//a[@data-sort='reviews-dd']")[0].click()

timeList = driver.find_elements_by_tag_name('time')
for time in timeList:
    print(time.text)

driver.quit()

1 个答案:

答案 0 :(得分:1)

试试这段代码可能会有用。

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

browser=webdriver.Chrome()
browser.maximize_window()
browser.get("https://www.zomato.com/cebu/the-pyramid-lahug-cebu-city")
delay=15
#browser.execute_script("window.scrollTo(0,document.body.scrollHeight/2)")
time.sleep(2)

element = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.XPATH, ".//*[@id='selectors']/a[2]")))
element.click()
element = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".item.default-section-title.everyone.empty")))
element.click()
print "click done"
time= WebDriverWait(browser, delay).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".grey-text>time")))
for i in time:
    print i.text
browser.quit()