1

So, I've got a Selenium webdriver running. I want to extract text from some nodes. I can find them:

In [73]: nodes = driver.find_elements_by_css_selector("rect > title")
In [74]: len(nodes)
Out[74]: 63

But they don't contain any text:

In [75]: for n in nodes:
    ...:     if n.text:
    ...:         print(n.text)
<prints nothing>

But if I open devtools in browser which is connected to this instance of driver, I find the same amount of nodes with this CSS selector - and they definitely contain some text: Devtools

So, I've got some nodes with text which is not retrivable by Selenium. Just to make sure, I created a BeautifulSoap from that page's source - and I was able to find elements in question that way and get their text.

In [86]: [a.text for a in s.select("rect > title")]
Out[86]: 
['Sometext',
 'Sometext',
 'Sometext',
 'Sometext',
 ...]

Getting "innerText" instead, as in this question, didn't help. Is that some kind of Selenium bug, or I am doing something wrong? Just in case, there is problematic html.

keddad
  • 1,398
  • 3
  • 14
  • 35

1 Answers1

1

Try using

n.getAttribute("textContent")

It's possible that the "title" tag is getting handled special because "it's not rendered": https://www.w3.org/TR/webdriver/#get-element-text

DMart
  • 2,401
  • 1
  • 14
  • 19
  • That worked! Weird, though, since they looked like they were rendered. Still, thanks – keddad Mar 04 '21 at 18:57
  • I bet it's special handling in the code for webelemetns that are "title" tags... Can't find 100% proof of that. – DMart Mar 04 '21 at 18:59