-1

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.

thanks for all the helpers, I'm using python here.

my code:

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

from the html :

The HTML

Guy
  • 46,488
  • 10
  • 44
  • 88
user3652695
  • 11
  • 1
  • 4
  • What code are you using to get the element's text? The code you posted just finds the element itself. – John Gordon Feb 10 '16 at 17:56
  • elem=driver.findElement(By.XPATH("//span[@class='ytp-menu-label-secondary']")).text I want to get the element and then extract the text there. – user3652695 Feb 10 '16 at 17:57
  • So when you try that code, what happens? Do you get different text than you expected, or no text at all, or an error? – John Gordon Feb 10 '16 at 18:08
  • See: http://stackoverflow.com/q/28022764/954442 – Andrew Regan Feb 10 '16 at 18:15
  • Please post the HTML as text in the question instead of linking an image. At some point the image no longer be available and this question will be less useful to others. – JeffC Feb 10 '16 at 19:24

2 Answers2

2

In python it should be

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
Guy
  • 46,488
  • 10
  • 44
  • 88
0

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.

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.

innerHTML will return the inner HTML of this element, which contains all HTML tags inside. For example, innerHTML for Hello

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 is a W3C-compliant textContent property[1], but sadly is not supported by IE[2]. innerText is not part of the W3C DOM specification and not supported by Firefox. Here is a brief demonstration on how to get text from hidden elements using Selenium WebDriver .NET, Ruby and Python bindings.

from selenium import webdriver

DEMO_PAGE = '''data:text/html,

Demo page for how to get text from hidden elements using Selenium WebDriver.

Demo div with a hidden paragraph inside.

'''
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
Howard
  • 31
  • 1