无法迭代selenium python

时间:2016-10-16 04:02:29

标签: python selenium web-scraping

我是一个硒菜鸟,并一直在努力用python完成任务。 我试图从这个页面https://www.tripadvisor.com/Airline_Review-d8729164-Reviews-Cheap-Flights-or560-TAP-Portugal#REVIEWS

迭代所有用户评论(" partial_entry"类)
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome("C:\Users\shalini\Downloads\chromedriver_win32\chromedriver.exe")
driver.maximize_window()
url="https://www.tripadvisor.com/Airline_Review-d8729164-Reviews-Cheap-Flights-or560-TAP-Portugal#REVIEWS"
driver.get(url)

for i in driver.find_elements_by_xpath("//div[@class='wrap']"):
    print i.find_element(By.XPATH, '//p[@class="partial_entry"]')
        print i.text
    print "=============================================="
    # THIS IF BLOCK IS NECESSARY, I CANT DO AWAY WITH THIS ONE
    if i.find_elements(By.CSS_SELECTOR,"#REVIEWS .googleTranslation>.link"):
        print "======YES TRANSLATION AVAILABLE========"

即使我每次在for循环中选择一个不同的元素,但它一遍又一遍地打印相同的元素。 (我必须保留最后一个if块并且不能取消它,所以无论解决方案是什么,它必须包括if if block)

== EDIT ===================

即使这不起作用(根据http://selenium-python.readthedocs.io/locating-elements.html,这实际上应该有效)。我不知道硒会发生什么!!!!!

print i.find_element(By.CSS_SELECTOR, 'p.partial_entry')

输出:

NoSuchElementException:

1 个答案:

答案 0 :(得分:3)

1。在第二个循环中迭代i.find_element(By.XPATH, '//p[@class="partial_entry"]')时反复获取第一个元素的原因是,开始//尝试从根目录中找到元素/顶级,作为i的后代元素。因此,只需为外循环的每次迭代返回第一个p.partial_entry元素。

要搜索匹配i的{​​{1}}的后代元素,xpath应以p[@class="partial_entry"]开头。这就是点的作用。

2。对于行.//
print i.find_element(By.CSS_SELECTOR, 'p.partial_entry')返回第一个找到的元素,如果没有找到则抛出错误。有一些“div.wrap”没有该后代元素,因此您将获得find_element

NoSuchElementException(请注意' s')方法返回元素列表或空列表(如果没有找到),而不是错误。

所以把所有这些放在一起:

find_elements

顺便问一下,为什么要将>>> for i in driver.find_elements_by_xpath("//div[@class='wrap']"): ... for ent in i.find_elements_by_xpath('.//p[@class="partial_entry"]'): ... print ent.text ... if i.find_elements_by_css_selector('#REVIEWS .googleTranslation>.link'): ... print 'translation available' ... print # output clarity ... find_elements_by_xpath('...')等内容混合在一起?坚持一种模式。

相关问题