I want to extract text between <tag></tag> (In my case, <tr></tr>). so, I'm using webelement.text
self.browser = webdriver.Firefox()
table = self.browser.find_element_by_tag_name('table')
....
rows = table.find_elements_by_tag_name('tr')
print rows
for element in rows:
print type(element)
print element.text
print type(element.text)
and output is:
[<selenium.webdriver.remote.webelement.WebElement object at 0x0151E390>] # <-print rows
<class 'selenium.webdriver.remote.webelement.WebElement'> # <-print type(element)
# <-nothing from print element.text
<type 'unicode'> # <-print type(e.text)
So there is nothing from element.text, but the tags is not empty. <tr>blablabla</tr>
I got no possibility to check it on other browsers.
The problem is with <tr>
It dont see text inside <tr>blabla</tr>:
rows = table.find_elements_by_tag_name('tr') will be emplty.
But it see it inside <tr><td>blabla</td></tr>:
rows = table.find_elements_by_tag_name('tr')
for element in rows:
print element.text # <-blabla
Though, it doesnt work on any nested element:
<tr><h1>blabla</h1></tr>:
rows = table.find_elements_by_tag_name('tr') will be emplty.
The documentation on webelement.text says just
text
Gets the text of the element.Its just dont consider text inside
<tr>text</tr>as the text of the<tr>element, I suppose.
Only guessing, but is the text you're looking for actually in a child node (<td> maybe?)?
I'm not sure how pythons webelement.text works, but maybe you need to get the text of the child elements.
EDIT: I think your problem might actually be that having text between <tr> tags is invalid HTML, and is not being stored in the DOM as you might expect.
When I create a simple table with text in a row...
<body>
<table border="1">
<tr>
<td>Text in 1st cell</td>
</tr>
<tr>
<td>Text in 2nd cell</td>
</tr>
<tr>
Text in 3rd Row
</tr>
</table>
</body>
The resulting DOM actually looks like this...
<body>
Text in 3rd Row
<table border="1">
<tbody>
<tr>
<td>Text in 1st cell</td>
</tr>
<tr>
<td>Text in 2nd cell</td>
</tr>
<tr></tr>
</tbody>
</table>
</body>
So you can see there actually isn't any text in the third <tr>, which explains what you're seeing.
So please post your actual HTML/DOM, so that we can see if there actually is any text inside the tag you're expecting
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With