×

Selenium WebDriver-CSS Selector: Tag and ID

Selenium WebDriver-CSS Selector: Tag and ID

We will locate the web element with the help of tag and id CSS selector in this section of the tutorials

Where Tag and id are used to find the web element by its tag name using the #symbol to locate the id attribute.

The Syntax for tag and id attribute is as below:

driver.findElement(By.css Selector(“tag#id”)); 

Where, tag=the html tag of the web element

#=the hash symbol is used when CSS selector contains its Id attribute.

Id = represents the value of its id attribute.

Let us take one example where we will perform tag and id attribute of CSS selector:

Steps Actions Input Expected Result
1. Open the Firefox browser.   The Firefox browser should be opened.
2. Navigate to the Gmail login page. www.gmail.com The Gmail login page must be displayed.
3. Identify the username text box and pass the value. Username=admin The username textbox value should be entered.
4. 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 sessions of the Selenium WebDriver tutorial.
  • Then right-click on the src folder and create a new Class File from New ? Class.
Selenium web driver-CSS Selector Tag and ID
  • And give your Class name as Tag_and_id and click on the Finish button.
Selenium web driver-CSS Selector Tag and ID 1

We are creating our test case step by step to give you a complete understanding of how to use tag and ID Locators to identify a particular web element.

Step1:

  • To access the Firefox browser first, we need to download the gecko driver and set the system property for the gecko driver.
  • We have already discussed this in previous sessions of the tutorial. And you can also refer given link "Working with Firefox browser” for a better understanding that how we download it and set System property for Firefox driver.
// System Property for gecko Driver   
System.setProperty("webdriver.gecko.driver","C:\Users\JTP\Downloads\geckodriver-v0.25.0-win64\geckodriver.exe");
// create an object for FirefoxDriver class.       
WebDriver driver=new FirefoxDriver();   

Step2:

After that, we will write the code to automate our second step, which navigates to the given URL.

The sample code for the navigation of the desired URL:

//navigate to the URL
driver.get("https://accounts.google.com/signin/v2/identifier?continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin”); 

Step3:

 Now, we are trying to locate the desired web element by using the value of its tag and id attribute.

  • Right-click on the username text box and clicks on the Inspect Element.
Selenium web driver-CSS Selector Tag and ID 2
  • It will launch a window that contains all the specific codes used in the development of the username text box.
Selenium web driver-CSS Selector Tag and ID 3

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

Here the sample code:

//Identify the username text box and pass the value.
driver.findElement(By.cssSelector("input#identifierId")).sendKeys("admin");
Thread.sleep(3000);
System.out.println("username value is entered"); 

Step4:

The last step of our sample test case is to close the browser.

Here the sample code for closing the browser,

//Close the browser
driver.close();   

Our final test script will look like this:

package testpackage;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Tag_and_id {
public static void main(String[] args)throws InterruptedException {
//set the system property
System.setProperty("webdriver.gecko.driver","C:\Users\JTP\Downloads\geckodriver-0.25.0-win64\geckodriver.exe");
// create driver object for gecko browser              
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize(); 
//Navigate to Gmail login page               driver.get("https://accounts.google.com/signin/v2/identifier?continue=https%3A%2F%2"+ "Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1&flowName="
+ "GlifWebSignIn&flowEntry=ServiceLogin");
// identify the username text box and pass the value.
driver.findElement(By.cssSelector("input#identifierId")).sendKeys("admin");
Thread.sleep(3000);
System.out.println("username value is entered");
 //close the browser
driver.close();
}
} 
  • To run the above code in the Eclipse, we have to right-click on the code and then select Run As ? Java Application.
Selenium web driver-CSS Selector Tag and ID 4
  • The above test script will launch the Firefox browser and automate all the test scenarios.
Selenium web driver-CSS Selector Tag and ID 5

 

 


Related Topics

Selenium WebDriver Characteristics

Characteristics of Selenium WebDriver Some of the essential characteristics of Selenium WebDriver are as follows: Multiple Operating System Support Selenium Webdriver supports multiple operating systems like: Desktop OS: Window, Linux, and macOS Mobile OS:...

2 minutes read.

Selenium Tutorial

What is Selenium? Selenium is a functional testing tool and compatible with the non-functional testing tools also. Selenium is free and does not require any licensing. Functional tests script executed by...

6 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.

WebDriver-Id Locator

WebDriver-Id Locator: The Id Locator is used to identify any web element in UI with the help of the ID attribute. The First preference is always goes to ID locator while inspecting...

4 minutes read.

Selenium WebDriver-CSS Selector: Tag and Attribute

Selenium WebDriver-CSS Selector: Tag and Attribute We will locate the web element with the help of Tag and attribute CSS selector in this section of the tutorial. Where Tag and attribute are...

5 minutes read.

CSS Selector Locator in Selenium IDE

It used to identify the web element based on HTML tag, id, class, and attributes. To describe the look and formatting of a document written in the markup language are known...

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.

CSS Selector: ID in Selenium IDE

CSS Selector: ID The id attribute is used to identify the web element uniquely in the web pages. Syntax of ID Attribute:  [HTML tag][#][Value of ID attribute] The hash sign (#): it is mandatory while using the...

1 minute read.

Selenium-IDE Locators

Selenium-IDE Locators: Before we start learning about Selenium –IDE Locators, firstly we have to learn about UI elements. UI Elements -UI elements are the elements which are displayed on the web application....

1 minute read.

Selenium vs QTP

In this section, we are comparing the most frequently used automation tool –Selenium and QTP. Selenium and QTP have relative advantages over each other- FeaturesSeleniumHP QTPLicensingSelenium is an open-source testing tool....

3 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.

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 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- Microsoft Edge Browser

Selenium WebDriver- Microsoft Edge Browser: In this tutorial, we will learn how to launch the Microsoft Edge browser in the Selenium WebDriver. Before we start automating our test script with Microsoft Edge...

4 minutes read.

Selenium Testing

Selenium is a functional testing tool and compatible with non–functional testing tool. Selenium is free and not required any licensing. There are specified types of testing that can be automated using Selenium:-   Functional Testing: Checking every component...

3 minutes 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.

WebDriver-Link Text locator

WebDriver-Link Text locator The Link text locator is used to identify only web link elements based on the value of the link. The Link text locators cannot be used for other than...

4 minutes 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.

First test case of Selenium-IDE

First Test Case of Selenium-IDE: In this part, we are discussing how to create a basic test case in Selenium-IDE: The entire test script execution process in Selenium -IDE can be divided...

4 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.