Regular expressions-
Sometimes in HTML, the developer defines more than one class name ( that’s class input has more
than one name
Here the email or phone number in Gmail has the following
HTML STRUCTURE
<input
type="email" class="whsOnd zHQkBf"
jsname="YPqjbf" autocomplete="username"
spellcheck="false" tabindex="0" aria-label="Email or
phone" name="identifier" autocapitalize="none"
id="identified" dir="ltr"
data-initial-dir="ltr" data-initial-value="" badinput="false"
aria-invalid="true">
You can see that the input class attribute is composed of more than one name - whsOnd zHQkBf
In order to frame the CSS
path for this, we can use regular expression as –
* à perform a
partial match with the class attribute value. The CSS value shall be input[class*= whsOnd]
This means the subtext whsOnd is present in the actual text whsOnd zHQkBf
^ à perform a match with the class. The CSS value shall be input[class^= ‘whsOnd’]. This means the actual text whsOnd zHQkBf starts with the subtext whsOnd.
$ àperform a match with the class. The CSS value
shall be input[class$=’zHQkBf’]. This means the actual text whsOnd zHQkBf ends with the subtext zHQkBf.
For each of these
three regular expression usages, the code will look as –
Using *
driver.get("https://accounts.google.com/");
driver.findElement(By.cssSelector("input[class*=
whsOnd]")).sendKeys("admin");
Using ^
driver.get("https://accounts.google.com/");
driver.findElement(By.cssSelector("input[class^='whsOnd']")).sendKeys("admin");
****************Code for regular Expression to identify UserName Field*******************
**
* @author Mann
*
*/
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
public class RegularExpressions_3 {
public static void main(String[] args) {
System.setProperty("webdriver.edge.driver", "C:/Users/Mann/SeleniumTutorials/msedgedriver.exe");
WebDriver driver= new EdgeDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
// using *
driver.get("https://accounts.google.com/");
driver.findElement(By.cssSelector("input[class*= whsOnd]")).sendKeys("admin");
// using $
//driver.findElement(By.cssSelector("input[class$='zHQkBf']. ")).sendKeys("admin");
// using ^
//driver.findElement(By.cssSelector("input[class^='whsOnd']")).sendKeys("admin");
//driver.findElement(By.name("user_name")).sendKeys("admin");
}}