使用Selenium提取span文本内容

Use Selenium to extract span text content

无法弄清楚如何在 python 脚本中使用 selenium 从显示的 html 中提取跨度文本内容:RSB - 12498。请帮忙。谢谢

<div _ngcontent-pfw-c3="" class="strip_list wow fadeIn animated" style="visibility: visible; animation-name: fadeIn;"><figure _ngcontent-pfw-c3=""><a _ngcontent-pfw-c3="" href="detail-page.html"><img _ngcontent-pfw-c3="" alt="" src="http://via.placeholder.com/565x565.jpg"></a></figure><small _ngcontent-pfw-c3=""><!----><!----><span _ngcontent-pfw-c3=""> RSB - 12498</span>

如果您想使用 XPath,定位此类 span 元素的定位器将是 //small/span
CSS 选择器将是 small span
因此,获取此类单个元素文本的 Selenium 命令将是:

goal = driver.find_element_by_css_selector('small span').text
print(goal)

goal = driver.find_element_by_xpath('//small/span').text
print(goal)

如果有多个搜索结果,可以从结果元素列表中接收文本:

elements = driver.find_elements_by_css_selector('small span')
for element in elements:
    print(element.text)

elements = driver.find_elements_by_xpath('//small/span')
for element in elements:
    print(element.text)