简体   繁体   English

如何使用硒从跨度获取文本

[英]How to get text from span using selenium

I'm trying to get text from span (the quality in the picture), but I haven't find the reason why it's not getting it.我正在尝试从 span 中获取文本(图片中的质量),但我还没有找到它没有得到它的原因。

thanks for all the helpers, I'm using python here.感谢所有帮助者,我在这里使用 python。

my code:我的代码:

  elem=driver.findElement(By.XPATH("//span[@class='ytp-menu-label-secondary']"));

from the html :从 html :

HTML

In python it should be在python中它应该是

element = driver.find_element_by_xpath("//span[@class='ytp-menu-label-secondary']")

Or by class name if you like或者如果你愿意,可以按班级名称

element = driver.find_element_by_class_name('ytp-menu-label-secondary')

And for the text而对于文字

elementText = element.text

http://yizeng.me/2014/04/08/get-text-from-hidden-elements-using-selenium-webdriver/ http://yizeng.me/2014/04/08/get-text-from-hidden-elements-using-selenium-webdriver/

excerpt from this link: As defined in WebDriver spec, Selenium WebDriver will only interact with visible elements, therefore the text of an invisible element will always be returned as an empty string.此链接的摘录:如 WebDriver 规范中所定义,Selenium WebDriver 将仅与可见元素交互,因此不可见元素的文本将始终作为空字符串返回。

However, in some cases, one may find it useful to get the hidden text, which can be retrieved from element's textContent, innerText or innerHTML attribute, by calling element.attribute('attributeName') or injecting JavaScript like return arguments[0].attributeName.但是,在某些情况下,通过调用 element.attribute('attributeName') 或像 return arguments[0] 注入 JavaScript,可以从元素的 textContent、innerText 或 innerHTML 属性中检索隐藏文本,这可能会很有用。属性名。

innerHTML will return the inner HTML of this element, which contains all HTML tags inside. innerHTML 将返回此元素的内部 HTML,其中包含内部的所有 HTML 标签。 For example, innerHTML for Hello例如,你好的innerHTML

World!世界!

would be Hello 你好

World!世界!

instead of Hello World!. 而不是你好世界!。 textContent and innerText will only retrieve all text content of its descendants without any HTML tags. textContent 和 innerText 将只检索其后代的所有文本内容,没有任何 HTML 标签。 textContent is a W3C-compliant textContent property[1], but sadly is not supported by IE[2]. textContent 是符合 W3C 的 textContent 属性 [1],但遗憾的是 IE[2] 不支持。 innerText is not part of the W3C DOM specification and not supported by Firefox. innerText 不是 W3C DOM 规范的一部分,Firefox 不支持。 Here is a brief demonstration on how to get text from hidden elements using Selenium WebDriver .NET, Ruby and Python bindings. 这里简要演示了如何使用 Selenium WebDriver .NET、Ruby 和 Python 绑定从隐藏元素中获取文本。

from selenium import webdriver从硒导入网络驱动程序

DEMO_PAGE = '''data:text/html, DEMO_PAGE = '''data:text/html,

Demo page for how to get text from hidden elements using Selenium WebDriver.有关如何使用 Selenium WebDriver 从隐藏元素中获取文本的演示页面。

Demo div with a hidden paragraph inside. 演示 div 里面有一个隐藏的段落。



''' '''

 driver = webdriver.PhantomJS() driver.get(DEMO_PAGE) demo_div = driver.find_element_by_id("demo-div") print demo_div.get_attribute('innerHTML') print driver.execute_script("return arguments[0].innerHTML", demo_div) print demo_div.get_attribute('textContent') print driver.execute_script("return arguments[0].textContent", demo_div) driver.quit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM