首页 > 解决方案 > 无法使用 selenium webdriver 提取跨度标记中的文本名称

问题描述

我有一张如下表:

在此处输入图像描述

我正在尝试使用下面的 Selenium Web 驱动程序代码提取标题名称(突出显示),但我得到的输出如下None

!pip install selenium
!apt-get update # to update ubuntu to correctly run apt install
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
import sys
sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')


wd = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
wd.get('https://www.deakin.edu.au/information-technology/staff-listing')
title = wd.find_elements_by_class_name('module__accordion--title')

for j in range(0,6): #Iterating through Titles i.e Profesor, Emeritus Professor....
  for i in title[j].find_elements_by_tag_name('span'): #Retrieving the title names
    i = i.get_attribute('text')
    print(i)

这导致以下输出:

在此处输入图像描述

所以问题是为什么我得到 None 而不是 text ?

该代码也存在于下面的 Google Colab 链接中。
谷歌 Colab 链接:https ://colab.research.google.com/drive/1R9-hzor-lDXddOsNzr3qFc_0qodrbU6g?usp=sharing

标签: pythonseleniumselenium-webdriver

解决方案


你可以使用i.texti.get_attribute('innerText')


推荐阅读