Selenium: test if element contains some text

2019-06-14 23:33发布

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)

6条回答
Animai°情兽
2楼-- · 2019-06-14 23:50

Are you able to use jQuery if so try something like

$("p#fred:contains('bcd')").css("text-decoration", "underline");
查看更多
该账号已被封号
3楼-- · 2019-06-14 23:56

You can also use:

assertElementPresent
css=p#fred:contains('bcd')
查看更多
做自己的国王
4楼-- · 2019-06-15 00:00

It can be done with a simple wildcard:

verifyText
id="fred"
*bcd*

See selenium IDE Doc

查看更多
叼着烟拽天下
5楼-- · 2019-06-15 00:14

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*.

查看更多
时光不老,我们不散
6楼-- · 2019-06-15 00:14

You can use the command assertTextPresent or verifyText

查看更多
我欲成王,谁敢阻挡
7楼-- · 2019-06-15 00:15

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.

查看更多
登录 后发表回答