[英]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 :
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!世界!
World!世界!
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 从隐藏元素中获取文本的演示页面。
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.