使用 Python Selenium 获取跨度文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14590341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:55:15  来源:igfitidea点击:

Use Python Selenium to get span text

pythonselenium

提问by doremi

This should be easy, but I can't get it to work. I'm running a little demo using the Google homepage as a test.

这应该很容易,但我无法让它工作。我正在使用 Google 主页运行一个小演示作为测试。

Here's my script:

这是我的脚本:

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()

It returns:

它返回:

None
None
None
None
None
None
None
None
None
None
None

So I think it's grabbing the right elements, but perhaps not the right attribute? Not sure. I also tried to print e.text() but that spit out:

所以我认为它抓取了正确的元素,但可能不是正确的属性?没有把握。我也尝试打印 e.text() 但它吐出来了:

Traceback (most recent call last):
  File "sample.py", line 14, in <module>
    print e.text()
TypeError: 'unicode' object is not callable

Any thoughts?

有什么想法吗?

*Edit - Possible Solution? *

*编辑 - 可能的解决方案?*

e.get_attribute('innerHTML') seems to work.

采纳答案by root

This should do it:

这应该这样做:

from selenium import webdriver
browser = webdriver.Firefox()
browser.get("http://www.google.com")
for elem in browser.find_elements_by_xpath('.//span[@class = "gbts"]'):
    print elem.text


textis a property of the WebElementclass, thus it is not callable.

textWebElement类的属性,因此不可调用。

class WebElement(object):
    """Represents an HTML element.       
    ...
    ...

    @property
    def text(self):
        """Gets the text of the element."""
        return self._execute(Command.GET_ELEMENT_TEXT)['value']


You have two alternatives to get the third match:

您有两种选择来获得第三场比赛:

#  1. Modify your xpath expression
browser.find_elements_by_xpath('(.//span[@class = "gbts"])[3]')[0].text

#  2. Access it by list index
browser.find_elements_by_xpath('.//span[@class = "gbts"])')[2].text

回答by user3252247

YES ! Solution was found (I`am using Python) For instanc: webelement is a p tag

是的 !找到了解决方案(我正在使用 Python)对于 instanc:webelement 是 ap 标签

webelement.text()  

from real situation, stacktrace:

从实际情况来看,堆栈跟踪:

print page_box_block.text() TypeError: 'unicode' object is not callable

打印 page_box_block.text() TypeError: 'unicode' 对象不可调用

it expect to be a html in stdout, but not !

它期望是标准输出中的 html,但不是!

sometimes could be a strange string "unicode object is not callable" or some type error solution is very easy:

有时可能是一个奇怪的字符串“unicode object is not callable”或者一些类型错误的解决方案很简单:

print element.get_attribute("innerHTML")

In java get_attribute("innerHTML") and text() are about to "same", if you need plain text from element In Python 2.7 for now text() sometimes fails.

在 java get_attribute("innerHTML") 和 text() 即将“相同”,如果您需要来自元素的纯文本 在 Python 2.7 现在 text() 有时会失败。