简体   繁体   English

Selenium:测试元素是否包含一些文本

[英]Selenium: test if element contains some text

With Selenium IDE, how can I test if an element's inner text contains a specific string? 使用Selenium IDE,如何测试元素的内部文本是否包含特定字符串? For example: 例如:

<p id="fred">abcde</p>
'id=fred' contains "bcd" = true)

The Selenium-IDE documentation is helpful in this situation. Selenium-IDE文档在这种情况下很有用。

The command you are looking for is assertText , the locator would be id=fred and the text for example *bcd* . 您要查找的命令是assertText ,定位器将是id=fred ,文本例如是*bcd*

It can be done with a simple wildcard: 可以使用简单的通配符完成:

verifyText
id="fred"
*bcd*

See selenium IDE Doc 请参阅selenium IDE Doc

You can also use: 您还可以使用:

assertElementPresent
css=p#fred:contains('bcd')

你是否能够使用jQuery尝试类似的东西

$("p#fred:contains('bcd')").css("text-decoration", "underline");

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 ) (来自网站: 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): 如果您正在使用visual studio,则可以使用正则表达式(不仅仅包含)来评估字符串的功能:

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. 然后你的正则表达式根据我的链接是“bcd”或你在运行时构造的一些字符串。 Or: 要么:

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

(Something like that anyway) (无论如何都是这样的)

Hope it helped. 希望它有所帮助。

您可以使用命令assertTextPresentverifyText

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM