22

With Selenium IDE, how can I test if an element's inner text contains a specific string? For example:

<p id="fred">abcde</p>
'id=fred' contains "bcd" = true)
meetar
  • 7,443
  • 8
  • 42
  • 73
Mark W
  • 5,824
  • 15
  • 59
  • 97

7 Answers7

24

The Selenium-IDE documentation is helpful in this situation.

The command you are looking for is assertText, the locator would be id=fred and the text for example *bcd*.

Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
5

It can be done with a simple wildcard:

verifyText
id="fred"
*bcd*

See selenium IDE Doc

Mark W
  • 5,824
  • 15
  • 59
  • 97
4

You can also use:

assertElementPresent
css=p#fred:contains('bcd')
Aleh Douhi
  • 1,958
  • 1
  • 14
  • 13
  • Thank you, this was what I needed: `css=p:contains('foo')`. However, I'm told that it's deprecated. – LarsH Sep 13 '12 at 20:49
3

A solution with XPath:

Command: verify element present

Target: xpath=//div[@id='fred' and contains(.,'bcd')]

Karima Rafes
  • 1,016
  • 10
  • 19
0

Are you able to use jQuery if so try something like

$("p#fred:contains('bcd')").css("text-decoration", "underline");
The Angry Saxon
  • 792
  • 2
  • 7
  • 24
0

It seems regular expressions might work:

"The simplest character set is a character. The regular expression "the" contains three
character sets: "t," "h" and "e". It will match any line with the string "the" inside it.
This would also match the word "other". "

(From site: http://www.grymoire.com/Unix/Regular.html)

If you are using visual studio there is functionality for evaluating strings with regular expressions of ALL kinds (not just contains):

using System.Text.RegularExpressions;

Regex.IsMatch("YourInnerText", @"^[a-zA-Z]+$");

The expression I posted will check if the string contains ONLY letters.

Your regular expression would then according to my link be "bcd" or some string you construct at runtime. Or:

Regex.IsMatch("YourInnerText", @"bcd");

(Something like that anyway)

Hope it helped.

Martin Clemens Bloch
  • 1,047
  • 1
  • 12
  • 28
-1

You can use the command assertTextPresent or verifyText

Joand
  • 330
  • 4
  • 13
  • yes but I was trying to get a partial match, not full. Also it's not just that command that can be used. – Mark W Mar 29 '12 at 09:35