×

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

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-IDE Login test

Login test for Selenium-IDE Now we are learning, how to create a Login/sign-up Test case in Selenium IDE. For our testing purpose, we are going to test the login page provided by...

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

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

Selenium WebDriver-Locators

Selenium WebDriver-Locators The WebDriver uses the same set of locating strategies like selenium -IDE for specifying the location of the web element. Each locating strategy has its command in Java to locate...

1 minute read.

WebDriver-Name Locator:

WebDriver-Name Locator: The name locator is used to identify any element in UI with the help of the name backend attribute. The Syntax of the name attribute is as below: driver.findElement(By.name(“value of the...

4 minutes read.

Selenium WebDriver-CSS Selector: Tag Class and Attribute

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

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

9 minutes read.

Selenium WebDriver-XPath Locator

Selenium WebDriver-XPath Locator The locators can make the object identification of all the selenium tools. There are eight locators available in selenium web driver, but most of the time we go for...

14 minutes read.

Selenium IDE Commands

Selenium –IDE Commands (SELENESE): Selenese commands are the set of commands used in Selenium IDE. Selenese command has two parameters target and value. Selenese command tests the existence of UI elements based on their HTML tags,...

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

Drag and Drop Event Handling

Drag and Drop Event Handling: In this tutorial, we will learn how to handle the drag and drop event in selenium WebDriver using the action class. Before going further in this tutorial...

6 minutes read.

CSS Selector: Class in selenium IDE

CSS Selector: Class Identify a web element with the help of Class as a CSS Selector is like using id attribute. The only difference between Class and id is in their...

2 minutes read.

Characteristics of Selenium IDE

Characteristics of Selenium IDE Selenium IDE is separated into different parts, each having their features and functionalities. The seven various components of Selenium IDE are as follows: MENU BAR: The menu bar is...

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

4 minutes read.

Selenium WebDriver - Browser Controls Commands

Selenium WebDriver - Browser Controls Commands WebDriver includes operations like opening a browser, fetch the title, and closing the browser, etc. Some of the most commonly used Browser controls commands for Selenium WebDriver...

6 minutes read.