Html Selenium:测试元素是否包含一些文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9922054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium: test if element contains some text
提问by Mark W
With Selenium IDE, how can I test if an element's inner text contains a specific string? For example:
使用 Selenium IDE,如何测试元素的内部文本是否包含特定字符串?例如:
<p id="fred">abcde</p>
'id=fred' contains "bcd" = true)
回答by Petr Jane?ek
The Selenium-IDE documentationis helpful in this situation.
该硒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*
。
回答by Aleh Douhi
You can also use:
您还可以使用:
assertElementPresent
css=p#fred:contains('bcd')
回答by Mark W
It can be done with a simple wildcard:
它可以通过一个简单的通配符来完成:
verifyText
id="fred"
*bcd*
See selenium IDE Doc
回答by The Angry Saxon
Are you able to use jQuery if so try something like
如果可以,您可以使用 jQuery 吗?
$("p#fred:contains('bcd')").css("text-decoration", "underline");
回答by Martin Clemens Bloch
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.grymtheitroade.com/Unix/Regular.html)
(来自网站:http: //www.grymtheitroade.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. Or:
根据我的链接,您的正则表达式将是“bcd”或您在运行时构造的某个字符串。或者:
Regex.IsMatch("YourInnerText", @"bcd");
(Something like that anyway)
(反正就是这样)
Hope it helped.
希望它有所帮助。
回答by Joand
You can use the command assertTextPresent
or verifyText
您可以使用命令assertTextPresent
或verifyText