Hello Everyone! Welcome back to another Fresh article of MODTECHSTUFF where you Learn Automation testing.In this article, we will talk about how to use locators to inspect the element on the web page.
What do you understand by Locators?
- Locating By ID
- Locating By Name
- Locating By Class Name
- Locating By Tag Name
- Locating By Link Text
- Locating By CSS
The most common way of locating elements is by ID because every element has a different id.
Syntax: id=id of the element
For example, I will use Facebook as our test site. Use this page https://www.facebook.com/ for testing. Inspect the "Email or Phone" text box by clicking the right click on the text box and select inspect element after that a pop-up window will open. You have to select the id='email' as shown below.
We need to write the code which will click on the Text box, type the desired values on the text box and click on the submit button.
Here is the sample code to click on the Text box and type values as "Modtechstuff".
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class first {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
WebDriver driver=new ChromeDriver();
// Launch website
driver.navigate().to("https://www.facebook.com/");
// Click on the search text box and send value
driver.findElement(By.id("email")).sendKeys("Modetechstuff");
}
}
Locating By Name
Locating elements by name is very easy to use, we use the "name=" prefix
Syntax: name=name of the element
For example, I will use Instagram as our test site. Use this page https://www.instagram.com/?hl=en for testing. Inspect the "Email or Phone" text box by clicking the right click on the text box and select inspect element after that a pop-up window will open. You have to select the id='username' as shown below.
We need to write the code which will click on the Text box, type the desired values on the text box and click on the submit button.
Here is the sample code to click on the Text box and type values as "Modtechstuff".
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class first {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
WebDriver driver=new ChromeDriver();
// Launch website
driver.navigate().to("https://www.instagram.com/?hl=en");
// Click on the search text box and send value
driver.findElement(By.name("username")).sendKeys("Modetechstuff");
}
}
Locating By Class Name
The most common way of locating elements is by ID because every element has a different id.
Syntax: className=className of the element
For example, I will use Facebook as our test site. Use this page https://www.facebook.com/ for testing. Inspect the "Email or Phone" text box by clicking the right click on the text box and select inspect element after that a pop-up window will open. You have to select the className='inputtext login_form_input_box' as shown below.
We need to write the code which will click on the Text box, type the desired values on the text box and click on the submit button.
Here is the sample code to click on the Text box and type values as "Modtechstuff".
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class first {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
WebDriver driver=new ChromeDriver();
// Launch website
driver.navigate().to("https://www.facebook.com/");
// Click on the search text box and send value
driver.findElement(By.className("inputtext login_form_input_box")).sendKeys("Modetechstuff");
}
}
Locating By Tag Name
Locate a particular web element using its Tag Name.
Syntax: tagName=tagName of the element
For example, I will use Instagram as our test site. Use this page https://www.instagram.com/?hl=en for testing. Inspect the "Email or Phone" text box by clicking the right click on the text box and select inspect element after that a pop-up window will open. You have to select the tagName='input' as shown below.
We need to write the code which will click on the Text box, type the desired values on the text box and click on the submit button.
Here is the sample code to click on the Text box and type values as "Modtechstuff".
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class first {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
WebDriver driver=new ChromeDriver();
// Launch website
driver.navigate().to("https://www.instagram.com/?hl=en");
// Click on the search text box and send value
driver.findElement(By.tagName("input")).sendKeys("Modetechstuff");
}
}
We need to write the code which will click on the Text box, type the desired values on the text box and click on the submit button.
Here is the sample code to click on the Text box and type values as "Modtechstuff".
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class first {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
WebDriver driver=new ChromeDriver();
// Launch website
driver.navigate().to("https://www.instagram.com/?hl=en");
// Click on the search text box and send value
driver.findElement(By.linktext("Forgot password?")).sendKeys("Modetechstuff");
}
}
Locating By CSS
CSS means Cascading Style Sheets. It is a programming language that is used to define the appearance and formatting of a record written in a markup language.
Locating web elements under CSS includes the application of CSS Selector which distinguishes an element based on the sequence of HTML tag, id, class, and attributes. We can locate the element with the help of CSS in the following manner:
- Tag and ID
- Tag and Class
- Tag and Attribute
Tag and ID
We need to write the code which will click on the Text box, type the desired values on the text box and click on the submit button.
Here is the sample code to click on the Text box and type values as "Modtechstuff".
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class first {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
WebDriver driver=new ChromeDriver();
// Launch website
driver.navigate().to("https://www.facebook.com/");
// Click on the search text box and send value
driver.findElement(By.cssSelector("input#email")).sendKeys("Modetechstuff");
}
}
Tag and Class
We need to write the code which will click on the Text box, type the desired values on the text box and click on the submit button.
Here is the sample code to click on the Text box and type values as "Modtechstuff".
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class first {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
WebDriver driver=new ChromeDriver();
// Launch website
driver.navigate().to("https://www.facebook.com/");
// Click on the search text box and send value
driver.findElement(By.cssSelector("input#inputtext login_form_input_box")).sendKeys("Modetechstuff");
}
}
Tag and Attribute
We need to write the code which will click on the Text box, type the desired values on the text box and click on the submit button.
Here is the sample code to click on the Text box and type values as "Modtechstuff".
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class first {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
WebDriver driver=new ChromeDriver();
// Launch website
driver.navigate().to("https://www.facebook.com/");
// Click on the search text box and send value
driver.findElement(By.cssSelector("input[name=firstname]")).sendKeys("Modetechstuff");
}
}
Nice
ReplyDelete