Selenium Pattern Matching using Regular expressions

Selenium Pattern Matching using Regular expressions

 

Selenium Regular Expression patterns are the most frequenty used and the most useful.

The regular expression pattern in Selenium is similar to that offered by Javascript. In order to use the regular expression, you will need to prefix each of these with regexp: or regexpi:

 

To test if a particular string contains our text, we can do so in the following manner.

E.g.

let’s verify if an input field contains the string ‘Abhilash Gopi‘.

 

The input field is identified as strName

Command Target Value
ClickAndWait Link=Search  
verifyValue Id=strName Regexp:[Aa]bhilash [Gg]opi

 

What does the value regexp indicate?

You will notice here that the regexp is given as [Aa]bhilash [Gg]opi

It means that the resultant text could be found as “abhilash gopi” or “Abhilash Gopi” or “Abhilash gopi” or “abhilash Gopi”, in whatever form it could be with either A and/ or G in uppercase or lowercase.

I hope you get this well.

Your typical regular expression for email verification would be given as

Regexp:^[0-9a-zA-Z+_-.]+@[0-9a-zA-Z.-]+$

Another one would go like this

Regexp: ^([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$

Regular expression to verify generic URL

regexp:^(https?|ftp|file)://.+$

Advertisements

2 thoughts on “Selenium Pattern Matching using Regular expressions”

  1. Please help me to write one regexp:

    driver.findElement(By.name(“null”)).click();
    here, can I use – (By.name(“nu*”)).click(); – I added a star next to the letter ‘u’.
    For fields identified by css, it seems the * regexp not working. Am I correct?

    ^([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$
    How can I use the regexp above in the code below:
    driver.findElement(By.name(“null_123478”)).click();

    Regards,
    Suresh

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s