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

时间:2022-02-05 07:08:23

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)

6 个解决方案

#1


22  

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

#2


4  

It can be done with a simple wildcard:

可以使用简单的通配符完成:

verifyText
id="fred"
*bcd*

See selenium IDE Doc

请参阅selenium IDE Doc

#3


3  

You can also use:

您还可以使用:

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

#4


1  

Are you able to use jQuery if so try something like

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

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

#5


1  

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. Or:

然后根据我的链接你的正则表达式是“bcd”或你在运行时构造的一些字符串。要么:

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

(Something like that anyway)

(无论如何都是这样的)

Hope it helped.

希望它有所帮助。

#6


0  

You can use the command assertTextPresent or verifyText

您可以使用命令assertTextPresent或verifyText

#1


22  

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

#2


4  

It can be done with a simple wildcard:

可以使用简单的通配符完成:

verifyText
id="fred"
*bcd*

See selenium IDE Doc

请参阅selenium IDE Doc

#3


3  

You can also use:

您还可以使用:

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

#4


1  

Are you able to use jQuery if so try something like

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

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

#5


1  

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. Or:

然后根据我的链接你的正则表达式是“bcd”或你在运行时构造的一些字符串。要么:

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

(Something like that anyway)

(无论如何都是这样的)

Hope it helped.

希望它有所帮助。

#6


0  

You can use the command assertTextPresent or verifyText

您可以使用命令assertTextPresent或verifyText