这应该很容易,但我不能让它工作。我正在运行一个小演示使用谷歌主页作为测试。
这是我的剧本:
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.
这应该做到:
text
是WebElement
类的属性,因此不可调用。你有两个选择来获得第三场比赛:
是的!找到解决方案(我正在使用Python) 对于instanc:webelement是一个p标记
从实际情况来看,stacktrace:
打印页面框块。文本() TypeError:“unicode”对象不可调用
它应该是stdout中的html,但不是!
有时可能是一个奇怪的字符串“unicode对象不可调用”或某种类型错误 解决方法非常简单:
在java中,如果需要元素的纯文本,则get_attribute(“innerHTML”)和text()将“相同” 在Python2.7中,text()有时会失败。
相关问题 更多 >
编程相关推荐