proxy
proxy

Reputation: 936

webelement.text not work (browser = webdriver.Firefox)

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.

Upvotes: 3

Views: 3554

Answers (4)

Eug
Eug

Reputation: 1

I had same problem. I listed all elements for td and it finds 5 of them instead of 1. So I took text from the last one elem[-1].text and it workred for me

(Pdb) elem = self.wd.find_elements_by_xpath(".//*[@id='rf_version_details_info']/tbody/tr[3]/td[2]")
(Pdb) for i in elem: print i, i.text
<selenium.webdriver.remote.webelement.WebElement object at 0x036364D0>
<selenium.webdriver.remote.webelement.WebElement object at 0x03636610>
<selenium.webdriver.remote.webelement.WebElement object at 0x03636550>
<selenium.webdriver.remote.webelement.WebElement object at 0x03636650> Aug. 8, 2014 at 17:01 (UTC)
<selenium.webdriver.remote.webelement.WebElement object at 0x03636630> Aug. 8, 2014 at 17:01 (UTC)

Upvotes: 0

Steve Weaver Crawford
Steve Weaver Crawford

Reputation: 1059

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

Upvotes: 1

Mahesh Reddy Atla
Mahesh Reddy Atla

Reputation: 529

Try with the tag name table

 rows = table.find_elements_by_tag_name('Table')
 print rows
 for element in rows:
     print type(element)
     print element.text
     print type(element.text)

Upvotes: 0

Rafael I
Rafael I

Reputation: 66

It seems that you may be hitting some kind of bug. Here is a related post where they describe that Firefox returns empty text if the element is not visible at the time of the webelement.text invocation. They also propose a solution by doing a scroll to the element using javascript.

Check if your Firefox version is affected and try to update or implement the workaround.

Please let us know how you progress!

Link: WebElement getText() is an empty string in Firefox if element is not physically visible on the screen

Upvotes: 0

Related Questions