×

Selenium WebDriver-CSS Selector: Substring Matching

CSS Selector: Substring Matching Selenium WebDriver

In this tutorial, we will learn how to identify the web element using the substring method.

A Sub-string contains three methods for identifying the web element uniquely.

The sub-string methods are as follows:-

  • Contains (*)
  • Starts with (^)
  • Ends with ($)
Selector Substring Matching

Let us discuss these following method to give you a complete understanding:

Contains (*):

The Contains method is used to identify the web element using * (i.e., asterisk) symbol.

The Syntax of contains method:

Tagname[attribute*=’partial Attribute value’]

Where,

 [*]=the asterisk symbol is used to identify that the particular attribute contains any value or not.

Let us take one example to give you a better understanding of Contains() method using * symbol, and we created a sample test script which is as follows;

Steps Actions Input Expected Result
1. Open the Google Chrome browser.   The Google Chrome browser should be opened.
2. Navigate to the Rediff login page. https://mail.rediff.com/cgi-bin/login.cgi The Rediff login page must be displayed.
3. Identify the username text box and pass the value. xyz11@radiff.com The username text box should be identified, and the value should be entered.
4. Identify the password text box and pass the value. admin@12 The Password text box should be identified, and the value should be entered.
5. Identify and click on the go button.   The go button should be clicked.
6. Close the browser.   The browser should be closed.
  • Firstly, we have to Launch Eclipse and open the existing test suite new_test, which we have created in earlier session of the WebDriver tutorial.
  • Then, right-click on the new_test folder and create a new Class File from New à Class.
Selector Substring Matching 1
  • And give your Class name as Sub_string and click on the Finish button.
Selector Substring Matching 2

Step1:

  • To access the Google Chrome browser first, we need to download the chrome driver and set the system property for the chrome driver.
  • We have already discussed this in the previous sessions of the tutorial.
  •  And you can also refer to the given link "Working with Chrome browser” for a better understanding that how we downloaded it and set the System property for it.
//SystemPropertyforchromeDriver 
System.setProperty("webdriver.chrome.driver","C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe");
//create an object forChrome Driver
WebDriverdriver=newChromeDriver(); 

Step2:

  • After that, we will navigate to the given URL.

The sample code for the navigation of the desired URL:

//navigate to the URL
driver.get(“https://mail.rediff.com/cgi-bin/login.cgi”); 

Step3:

 For locating the username text box of the Rediff login page,

  • First, right-click on the username text box and select the Inspect Element.
Selector Substring Matching 3
  • The developer tool window will be launched, with all the specific codes used in the development of the username text box.
Selector Substring Matching 4

Where the tag name value is input, and the value of its id attribute is login1.

While using the Contains() method, we will take the partial value of the id attribute, i.e.,“logi.”               

Here the sample code,

driver.findElement(By.cssSelector("input[id*='logi']")).sendKeys("admin");        

Step4:

For locating the password text box of the Rediff login page,
 

  • Right-click on the password text box, and select the Inspect Element field.
Selector Substring Matching 5
  • The developer tool window will be launched with all the specific codes used in the development of the password text box.
Selector Substring Matching 6

Where the tag name value is input, and the value of its id attribute is password.

While using the Contains() method,we will take the partial value of the id attribute, i.e., “passwo.”

Here the sample code,

driver.findElement(By.cssSelector("input[id*='passwo]")).sendKeys("admin");

Step5:

To identify the Go button of the Rediff login page,

  • First, we will right-click on the Go button and select the Inspect Element.
Selector Substring Matching 7
  • The developer tool window will be launched with all the specific codes used in the development of the Go button.
Selector Substring Matching 8

Where the tag name value is input, and the value of its name attribute is proceed.

While using the Contains() method,we will take the partial value of its name attribute, i.e., “proc.”

Here the sample code,

driver.findElement(By.cssSelector("input[name*='proc']")).click();

Step6:

 In the last step, we are closing the existing browser.

//Close the browser
driver.close();

Our final test script look like this:

package testpackage;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Sub_string {
public static void main(String[] args) {
//set the system property for chrome driver
System.setPropert("webdriver.chrome.driver", "C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe");
//creating the object for chrome driver 
WebDriver driver = new ChromeDriver();
//maximize the browser size 
driver.manage().window().maximize(); 
//navigate to the URL
driver.get("https://mail.rediff.com/cgi-bin/login.cgi");
//Identify the username textbox and pass the value
driver.findElement(By.cssSelector("input[id*='logi']")). sendKeys("admin11@rediff.com");
System.out.println("username value is entered");
//Identify the password text box and pass the value
driver.findElement(By.cssSelector("input[id*='passwo']")).sendKeys("admin");
System.out.println("password value is entered");
//identify the go button
driver.findElement(By.cssSelector("input[name*='proc']")).click();
System.out.println("go button is cliked");
//close the browser
driver.close();
} 
} 

To run the test script in Eclipse, right-click on the code and then select Run as ? Java Application.

The above test script will launch the Google Chrome browser and automate all the test scenarios, as shown below:

Selector Substring Matching 9

Starts with (^):

The Starts-with is used to identify the web element using ^ (i.e., carat) symbol.

The Syntax of starts-with:

Tagname[attribute^ =’Attribute starting value’]

Where

 [^]=the carat symbol is used to identify the web element with its starting value.

Let us take one example to give you a better understanding of the starts-with method using ^ symbol, we created a sample test script which is as follows;

Steps Actions Input Expected Result
1. Open the Google Chrome browser.   The Google Chrome browser should be opened.
2. Navigate to the Facebook login page. https://www.facebook.com The Facebook login page must be displayed.
3. Identify the First-name text box and pass the value. First-name=Hello The First-name text box should be identified, and the value should be entered.
4. Identify the Surname text box and pass the value. Surename=world The Surname text box should be identified, and the value should be entered.
5. Close the browser.   The browser should be closed.

Follow the same Step1 as we did in the Contains() method.

Step2:

After that, we will navigate to the given URL.

The sample code for the navigation of the desired URL:

//navigate to the URL
driver.get(“https://www.facebook.com”); 

Step3:

 For locating the First-name text box of the Facebook login page,

  • First, right-click on the First-name text box and select the Inspect Element field.
Selector Substring Matching 10
  • The developer tool window will be launched with all the specific codes used in the development of the First-name text box.
Selector Substring Matching 11

Where the tag name value is input and the value of its name attribute is firstname.

While using the starts-with method, we will take the starting value of the name attribute, i.e., “first.” 

Here the sample code,

driver.findElement(By.cssSelector("input[name^='first']")).sendKeys("hello"); 

Step4:

For locating the surname text box of the Facebook login page,

  • Right-click on the surname text box and select the Inspect Element field.
Selector Substring Matching 12
  • The developer tool window will be launched with all the specific codes used in the development of the surname text box.
Selector Substring Matching 14

Where the tag name value is input and the value of its name attribute is lastname.

While using the starts-with method,we will take the starting value of the id attribute, i.e., “last.”

Here the sample code,

driver.findElement(By.cssSelector("input[name^='last']")).sendKeys("world");

Step5:

 In the last step, we are closing the existing browser.

//Close the browser
driver.close(); 

Our final test script look like this:

package testpackage;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Sub_string {
public static void main(String[] args) throws InterruptedException {
//set the system property for chrome driver
System.setProperty("webdriver.chrome.driver", "C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe");
//creating the object for chrome driver 
WebDriver driver = new ChromeDriver();
//maximize the browser size 
driver.manage().window().maximize(); 
//navigate to the url 
driver.get("https://www.facebook.com");
//Identify the first name textbox and pass the value
driver.findElement(By.cssSelector("input[name^='first']")).sendKeys("hello");
Thread.sleep(3000);
System.out.println("firstname value is 
entered");  
//Identify the surname text box and pass the value
driver.findElement(By.cssSelector("input[name^='last']")).sendKeys("world");
Thread.sleep(3000);
System.out.println("surname value is 
entered");  
//close the browser
driver.close();
} 
}

To run the test script in Eclipse, right-click on the code, and then select Run as ? Java Application.

The above test script will launch the Google Chrome browser, and automate all the test scenarios, as shown below:

Selector Substring Matching 15

Ends with ($):

The ends-with is used to identify the web element using $ (i.e., dollar) symbol.

The Syntax for ends with:

Tagname[attribute$ =’value’]

Where

[$]=the dollar symbol is used to identify the web element with its ending value.

To give you a better understanding of ends-with method using $ symbol, we created a sample test script which is as follows;

Steps Actions Input Expected Result
1. Open the Google Chrome browser.   The Google Chrome browser should be opened.
2. Navigate to the Google search home page. https://www.google.com The Google search home page must be displayed.
3. Identify the Google search text box and pass the value. Tutorialandexample.com The Google search text box should be identified, and the value should be entered.
4. Identify and click on the google search button.   The Google search button should be identified, and the button should be clicked.
5. Close the browser.   The browser should be closed.

Follow the same step1 of contains() method.

Step2:

  • After that, we will navigate to the given URL.

The sample code for the navigation of the desired URL:

//navigate to the URL
driver.get(“https://www.google.com”); 

Step3:

 For locating the Google search text box of the Google home page,

  • First, right-click on the Google search text box and select the Inspect Element field.
Selector Substring Matching 16
  • The developer tool window will be launched with all the specific codes used in the development of the Google search text box.
Selector Substring Matching 17

Where the tag name value is input, and the value of its class attribute is gLFyf gsfi.

While using the ends-with method, we will take the ending value of the class attribute, i.e., “fi.

Here the sample code,

driver.findElement(By.cssSelector("input[class$='fi']")).sendKeys("tutorialandexample.com"); 

Step4:

For locating the Google Search button of the Google home page,

  • Right-click on the Google search button and select the Inspect Element field.
Selector Substring Matching 18
  • The developer tool window will be launched with all the specific codes used in the development of the Google search button.
Selector Substring Matching 19

Where the tag name value is input, and the value of its name attribute is btnK.

While using the ends-with method,we will take the ending value of the name attribute, i.e.,“nK.”

Here the sample code,

driver.findElement(By.cssSelector("input[name$='nK']")).click();

Step5:

 In the last step, we are closing the existing browser.

//Close the browser
driver.close(); 

Our final test script look like this:

package testpackage;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Sub_string {
public static void main(String[] args) throws InterruptedException {
//set the system property for chrome driver
System.setProperty("webdriver.chrome.driver", "C:\Users\JTP\Downloads\chromedriver_win32\chromedriver.exe");
//creating the object for chrome driver 
WebDriver driver = new ChromeDriver();
//maximize the browser size 
driver.manage().window().maximize(); 
//navigate to the url 
driver.get("https://www.google.com"); 
//Identify the google search text box and pass the value
driver.findElement(By.cssSelector("input[class$='fi']")).sendKeys("tutorialandexample.com");
Thread.sleep(3000);
System.out.println(" the value is passed"); 
//Identify and click on the Google search button 
driver.findElement(By.cssSelector("input[name$='nK']")).click();
Thread.sleep(3000);
System.out.println(" search button is clicked ");  
//close the browser
driver.close();
} 
} 

To run the test script in Eclipse, right-click on the code and then select Run as ? Java Application.

The above test script will launch the Google Chrome browser and automate all the test scenarios, as shown below,

Selector Substring Matching 20

Related Topics

WebDriver-Partial Link Text locator

WebDriver-Partial Link Text locator In selenium WebDriver, there is an additional way to find a web link element with the help of partial link text locator. The partial link text is used...

4 minutes read.

Data-Capture Controls Methods in Selenium WebDriver

Data-Capture Controls Methods: The data-capture controls are used to determine the text, size, location, and the CSS value of the web element, etc. Some of the most commonly used data-capture controls are...

7 minutes read.

Selenium WebDriver - Navigation Controls Commands

Navigation Controls Commands in Selenium WebDriver WebDriver is used to hold the browser history and perform browser navigation operations like, back, forward, and refresh. Whenever a WebDriver launches a browser, all...

5 minutes read.

CSS Selector: Inner text in Selenium IDE

CSS Selector: Inner text The inner text is used to identify and create a CSS Selector using a string pattern of the HTML tag which is displayed on the web page. Syntax...

1 minute read.

Selenium WebDriver – Window handling

Selenium WebDriver – Window handling In this tutorial, we will understand how we can handle the window, multiple windows, and pop-ups in selenium WebDriver. We have different types of window, and pop-ups...

7 minutes read.

Operational Controls Methods in Selenium WebDriver

Operational Controls Methods The Operational controls are used to perform all the actions on the web elements like click on the button, pass the value in the text box, and click...

4 minutes read.

Name Locator in Selenium IDE

NAME LOCATOR: It is used to find the name in the web element. If we have multiple web elements of the same name, then the first matched element would be returned. Open the...

3 minutes read.

CSS Selector: Sub String in Selenium IDE

CSS Selector: Sub String CSS Selector Sub-string is used to match a partial string to locate a particular web element. There are three ways to match a substring using a CSS Selector. Match...

2 minutes read.

Selenium WebDriver Commands

Selenium WebDriver Commands: As we learn previously in the Selenium-IDE tutorial, Selenium commands are the set of commands that are used to run a Selenium test script. But whereas, Selenium WebDriver had...

2 minutes read.

Selenium WebDriver - Firefox or Gecko (Marionette) browser

Selenium web driver-Firefox or Gecko (Marionette) browser In this tutorial, we are going to learn how to run the Selenium WebDriver test script in the Firefox Browser using the Gecko Driver. Before going...

4 minutes read.

Selenium Tool Suite

The selenium tool suite is a combination of multiple software tools, and the entire tools having a different approach to support automation testing. Selenium test tool suites are as follows- Selenium...

2 minutes read.

Advantages and Disadvantages of Selenium-IDE

Advantages and Disadvantages of Selenium-IDE Advantages of Selenium IDE: UI based record and playback tool. Easy to understand & developed a test script. Selenium-IDE test script export into web driver code & Selenium–RC It is...

1 minute read.

Selenium WebDriver - Google Chrome Browser

Selenium WebDriver - Google Chrome Browser In this tutorial, we will learn how to run our Selenium Test Scripts in the Google Chrome Browser. To implement the WebDriver protocol with the help...

3 minutes read.

Selenium WebDriver-CSS Selector locator

Selenium WebDriver-CSS Selector locator A CSS selector is used to identify the web element based on their “# id” and “. Class”, Html tags, and attributes. A CSS stands for Cascading Style...

1 minute read.

Mouse and Keyboard Controls in Selenium WebDriver

Mouse and Keyboard Controls in Selenium WebDriver In this tutorial, we will learn about how to handle mouse and keyboard event using action class in selenium WebDriver. Action Class in Selenium WebDriver: Action...

13 minutes read.

Selenium WebDriver-CSS Selector: Tag and Class

Selenium WebDriver-CSS Selector: Tag and Class We will locate the web element with the help of tag and class CSS selector in this section of the tutorials Where Tag and class are...

4 minutes read.

Selenium Wait

In this tutorial, we will understand how we can avoid synchronization issues while executing the test script in a selenium WebDriver. Before going further in this tutorial, first, we will understand that...

10 minutes read.

Selenium IDE Tutorial for Beginners

Selenium IDE Introduction Selenium IDE stands for Integrated Development Environment. Selenium-IDE is released in 2006 and developed by SHINYA [JAPAN] THOUGHTWORK Company. Selenium IDE implemented as a Firefox plug-in, and concedes the record, edit, and debug tests....

2 minutes read.

Verification Controls Methods in Selenium WebDriver

Verification Controls Methods  The verification controls methods are used to check whether the web element is displayed, enabled, and selected. Some of the most commonly used verification controls are as follow: Verification controls Description Return...

4 minutes read.

CSS Selector: ID/Class and Attribute in Selenium IDE

CSS SELECTOR: ID/Class and Attribute The Id, class, and attribute of CSS selector are used to identify the web element to access the web pages.  Syntax for Id/Class and Attribute: css= <./#> {[attribute=Value of...

2 minutes read.