这应该很容易,但我无法让它工作。我正在使用 Google 主页作为测试运行一个小演示。
这是我的脚本:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Chrome()
browser.get("http://www.google.com") # Load page
time.sleep(0.2)
#top nav elements
elems = browser.find_elements_by_xpath("//span[contains(@class, 'gbts')]")
for e in elems:
print e.get_attribute('text')
browser.close()
它返回:
None
None
None
None
None
None
None
None
None
None
None
所以我认为它抓住了正确的元素,但也许不是正确的属性?没有把握。我也尝试打印 e.text() 但这吐出来:
Traceback (most recent call last):
File "sample.py", line 14, in <module>
print e.text()
TypeError: 'unicode' object is not callable
有什么想法吗?
*编辑 - 可能的解决方案?*
e.get_attribute('innerHTML') seems to work.