Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检查selenium中包含特定字符的文本_C#_Selenium - Fatal编程技术网

C# 如何检查selenium中包含特定字符的文本

C# 如何检查selenium中包含特定字符的文本,c#,selenium,C#,Selenium,我在下面有一个检索HTML标记的方法: public void CheckEmailDisplayed() { var email = _driver.FindElement(ConfirmationResponsiveElements.ViewEmail); } 查看电子邮件如下: public static By ViewEmail => By.ClassName("confirmation-banner__text"); 它对应的HTML

我在下面有一个检索HTML标记的方法:

    public void CheckEmailDisplayed()
    {
        var email = _driver.FindElement(ConfirmationResponsiveElements.ViewEmail);

    }
查看电子邮件如下:

public static By ViewEmail => By.ClassName("confirmation-banner__text");
它对应的HTML是:

<div class="confirmation-banner__text firefinder-match">
<p>
 We've sent an email to
<strong>firstname@xxx.com</strong>
</p>
<p>
</div>


我们已经给你发了一封电子邮件
firstname@xxx.com

我想做的是能够使用变量
email
检查文本是否包含@。这有助于确定显示的电子邮件地址。如何做到这一点


谢谢选项1:检查@符号

        string email = "test@domain.com";
        if (email.Contains("@"))
        {
            // code here
        }
选项2:验证电子邮件地址

public static bool IsEmail(string emailToValidate)
{
    if (string.IsNullOrEmpty(emailToValidate)) return true;

    return Regex.IsMatch(emailToValidate, @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
}
如何使用选项2:

        string email = "test@domain.com";            
        if (IsEmail(email))
        {
            // valid email address
        }
        else
        {
            // not a valid email address
        }

您可以添加另一个定义

public static By ViewEmailAddress => By.TagName("strong");
然后使用

var emailAddress = emai.FindElement(ConfirmationResponsiveElements.ViewEmailAddress);
var emailAddressText = emailAddress.Text;

然后,您可以在
emailAddressText
上执行所需的不同操作。像用
@
验证它,或者做更复杂的验证,比如电子邮件模式检查,你可以使用IndexOf方法

bool found = Value1.IndexOf("abc", 0, 7) != -1;

也可以使用正则表达式(但可读性较差)

资料来源:-

请参见:SO的期望是,用户提出问题时不仅要研究如何回答自己的问题,还要分享研究、代码尝试和结果。这表明你花了时间来帮助自己,它使我们避免重复显而易见的答案,最重要的是,它帮助你得到一个更具体和相关的答案!另见:
string regex = "^.{0,7}abc";

System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(regex);
string Value1 = "sssddabcgghh";

Console.WriteLine(reg.Match(Value1).Success);