小编典典

无法遍历Selenium python中的元素

selenium

我是一个selenium菜鸟,一直在努力用python完成事情。我试图从此页面迭代所有用户评论(“
partial_entry”类)https://www.tripadvisor.com/Airline_Review-d8729164-Reviews-
Cheap-Flights-or560-TAP-
Portugal#REVIEWS

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

即使Im每次都在for循环中选择一个不同的元素,但它会一次又一次地打印相同的元素。(我必须保留最后一个if块,并且不能删除它,因此无论采取什么解决方案,它都必须包含if块)

======编辑===================

即使这样也不起作用(根据http://selenium-python.readthedocs.io/locating-
elements.html,它实际上应该起作用)。我不知道selenium是怎么回事!!!!!

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

输出:

NoSuchElementException:

阅读 427

收藏
2020-06-26

共1个答案

小编典典

1.i.find_element(By.XPATH, '//p[@class="partial_entry"]')在第二个循环中进行迭代时,不断重复获取第一个元素的原因是,开始处//尝试从根/顶层定位元素,
而不是 作为的后代元素i。这样,p.partial_entry每次外循环的每次迭代都将继续返回第一个元素。

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

2. 对于该行print i.find_element(By.CSS_SELECTOR, 'p.partial_entry')
单身汉find_element返回第一个找到的元素,如果找不到则抛出错误。有些’div.wrap’没有那个后代元素,所以您会得到NoSuchElementException

find_elements(注意“S”)方法返回元素的列表或一个空列表,如果没有找到,而不是一个错误。

因此,将所有这些放在一起:

>>> 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('...')在一起find_element(By.XPATH, '...')呢?坚持一种模式。

2020-06-26