TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

WebDriver Locating Strategies By CSS Tag, Class and Attribute

WebDriver Locating Strategies By CSS Tag, Class and Attribute with Introduction, features, selenium basic terminology, what is selenium, selenium limitations, selenium vs qtp, tool suite, selenium ide, ide-installation, ide-features, ide-first test case, ide-commands, ide-creating test cases manually, ide-login test etc.

<< Back to WEBDRIVER

Locating Strategies- (By CSS-Tag, Class and Attribute)

In this section, you will learn how to locate a particular web element using CSS - Tag, Class and Attribute Selector.

Under this scenario, we will inspect the search text box of one of the most popular search engine i.e. Google.

As we know that locating a particular web element involves inspection of its HTML codes. Therefore, follow the steps given below to locate the textbox provided on the initial web page of Google Search Engine.

Locating Strategies- By CSS-Tag, Class and Attribute
  • It will launch a window containing all the specific codes involved in the development of the textbox.
Locating Strategies- By CSS-Tag, Class and Attribute
  • Take a note of its Tag, Class and Attribute.
Locating Strategies- By CSS-Tag, Class and Attribute

The Java Syntax for locating a web element through CSS - Tag, Class and Attribute Selector is written as:

driver.findElement(By.cssSelector("tag.class[attribute=value]"))

Therefore, for locating the Textbox on the Google Search Engine page we will use the input tag, Class and Name attribute:

driver.findElement(By.cssSelector("input.gsfi[name=q]"))

Similarly, for locating the Google Search button on the Google Search Engine page we will use its respective input tag, Class and Name attribute:

driver.findElement(By.cssSelector("input.jsp[name=btnK]"))

We have created a sample script for you to get a better understanding of how to use CSS - Tag, Class and Attribute Selector. We have embedded comments in each section of code which will guide you through whole automation process.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class SampleFour {

	public static void main(String[] args) {
		
	 // System Property for Gecko Driver 
System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );
		
       // Initialize Gecko Driver using Desired Capabilities Class
		DesiredCapabilities capabilities = DesiredCapabilities.firefox();
		capabilities.setCapability("marionette",true);
		WebDriver driver= new FirefoxDriver(capabilities);
		
	
      // Launch Website
driver.navigate().to("www.google.co.in");
	
      // Click on the textbox and send value
driver.findElement(By.cssSelector("input.gsfi[name=q]")).sendKeys("javaTpoint Tutorials");
	 
	// Click on the Google Search button using click() command
driver.findElement(By.cssSelector("input.jsp[name=btnK]")).click();
 
		
	 // Close the Browser
	  driver.close();
	
	}

}





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf