0

Im using selenium in python in order to check if a text between font tags in contained and getting the tag tbody back if it is.

the html code:

<table width="100%" border="0" cellspacing="1" cellpadding="1" bgcolor="#000000" height="80">
    <tbody>
        <tr bgcolor="#666699">
            <td height="17" bgcolor="#CCCC99">
                <font size="2"><b><font face="Arial, Helvetica, sans-serif">PARTY 1</font></b></font>
            </td>
        </tr>
        <tr>
            some html code...
        </tr>
    </tbody>
</table>

As you can see the this is an example i need to check in the html page if the has PARTY 1 in it and if so getting the tbody element back. I should say that there is no way to get the element using id because there are other tables written in the same way. I'v already tried using //font[contains(.,'PARTY 1')] but as it didnt worked

what is the best way to use selenium and check if PARTY 1 is in the and get tbody elment back??

Elad Doocker
  • 197
  • 11
  • `//font[contains(.,'PARTY 1')]` should at least locate the `font` element correctly. Did you get any errors? – alecxe Jun 23 '16 at 15:23

2 Answers2

0

This xpath return TBODY tag which contains FONT tag with 'PARTY 1' text:

//tbody[.//font[contains(.,'PARTY 1')]]

kotoj
  • 769
  • 1
  • 6
  • 25
  • 2
    hey it doesnt work the text between the font tag start and end isnt recognize as text so it cant get it... – Elad Doocker Jun 23 '16 at 15:18
  • You might have to replace `contains(.,'PARTY 1')` with `contains(text(),'PARTY 1')`. Though I'm not entirely sure about that – RemcoW Jun 23 '16 at 15:22
  • well as i understand this text is a string in the tag and not a text this is why i cant get it when im using text() any way to check if a tag contains a string? – Elad Doocker Jun 23 '16 at 15:25
  • Man, this is html, this is only text/string/letters/words whatever you call it you have to use text() command ;) – kotoj Jun 23 '16 at 15:30
  • well i know but somehow it doesnt work when i use text() – Elad Doocker Jun 23 '16 at 16:10
  • ok, so lets try from the begginig. What tool do you use to find elements? (I recommend FirePath for FF or just Chrome developer tools). Can you find this font tag using xpath: `//table/tbody/tr/td/font` ? Maybe the table is inside iFrame? – kotoj Jun 23 '16 at 16:17
  • I just copied the html above into a file and used FirePath to check the path @kotoj provided above. It worked fine for me. Yeah, check if it's inside an iFrame. – Breaks Software Jun 23 '16 at 16:52
0

First get the font element using el = driver.find_element_by_xpath('insert xpath here') and then use el.text to get the text. You're code should look like this:

el = driver.find_element_by_xpath('insert xpath of font tag here')

if el.text == 'PARTY 1':
    return driver.find_element_by_xpath('insert xpath of tbody here')
metacore
  • 91
  • 1
  • 9