div.find_element_by_xpath.text 值不存在

div.find_element_by_xpath.text value not coming

我正在尝试使用以下代码抓取 https://www.tiaa.org/public/offer/products/life-insurance 的评论内容

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 = ''
gender = ''
occupation = ''
reason = ''

driver.switch_to_frame(0)
for div in driver.find_elements_by_xpath('//*[@id="BVRRContainer"]/div/div/div/div/ol'):
    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')
    print(author.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')
    print (summary.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')
    print(product_family.text)
    gender = div.find_element_by_xpath('//*[@id="BVRRContainer"]/div/div/div/div/ol/li[1]/div[1]/div/dl/dd[3]/ul/li[5]/span[2]')
    print(gender.text)
    occupation = div.find_element_by_xpath('//*[@id="BVRRContainer"]/div/div/div/div/ol/li[1]/div[1]/div/dl/dd[3]/ul/li[4]/span[2]')
    print(occupation.text)
    reason = div.find_element_by_xpath('//*[@id="BVRRContainer"]/div/div/div/div/ol/li[1]/div[1]/div/dl/dd[3]/ul/li[2]/span[2]')
    print(reason.text)

我也试过 .getText() ..但没有成功..请指点...

getText()text 不起作用的原因是因为您试图访问的元素是隐藏的(我猜是 CSS)并且 getText() 只获取可见的 innerText。 DOM 上有 2 个元素(对于每个用户),其中包含有关作者姓名的信息。在这 2 个中,一个是隐藏的(您正在访问),一个是可见的(您应该使用)。

应该有效的作者姓名的 xpath 是:

//*[@id="BVRRContainer"]/div/div/div/div/ol/li[1]/div[1]/div/div/div/div/h3