没有这样的元素:无法找到元素:

时间:2017-05-01 20:44:50

标签: python selenium

我正在尝试使用以下代码

来抓取https://www.tiaa.org/public/offer/products/life-insurance的评论内容
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.get(html_page)
driver.find_element_by_xpath("""//*[@id="bv-hero-ALL-LIFE-INSURANCE"]/span[2]/span[2]""").click()
driver.implicitly_wait(20)
reviews_list = driver.find_elements_by_css_selector('bv-content-item bv-content-top-review bv-content-review')
author = ''
summary = ''
product_family = ''

for div in driver.find_element_by_xpath('//*[@id="BVRRContainer"]/div/div/div/div/ol'):
    try:
        author = div.find_element_by_xpath('//*[@id="BVRRContainer"]/div/div/div/div/ol/li[1]/div[2]/div[1]/div/div[1]/div/div[1]/div/div/div/h3').text
        summary = div.find_element_by_xpath('//*[@id="BVRRContainer"]/div/div/div/div/ol/li[1]/div[2]/div[1]/div/div[2]/div/div/div[1]/p/text()').text
        product_family = div.find_element_by_xpath('//*[@id="BVRRContainer"]/div/div/div/div/ol/li[1]/div[2]/div[1]/div/div[2]/div/div/div[2]/div[3]/div/span/a').text
    except Exception:
        print(Exception)

if author and summary and product_family:
            single_item_info = {
                'author': author.encode('UTF-8'),
                'summary': summary.encode('UTF-8'),
                'insurance type': product_family.encode('UTF-8')
            }
            print(single_item_info)

driver.quit()

我收到以下错误。请指导或提供一些指示。

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="BVRRContainer"]/div/div/div/div/ol"}
  (Session info: chrome=57.0.2987.133)
  (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 6.3.9600 x86_64)

1 个答案:

答案 0 :(得分:0)

带有roles: %w{web app db}的{​​{1}}元素位于索引为0的iframe中。您需要将Selenium的焦点切换到此iframe,然后才能识别评论评分。

要将焦点切换到此iframe,请在div循环

之前执行此操作

id = BVRRContainer

相关问题