Introduction
Selenium Webdriver is used to automate the testing. There are some visible and invisible, editable and non-editable elements on the webpage. We can access those elements using Selenium and check whether the element is accessible or not.
Locating an element:
An element can be located by ID, name, XPath, etc.
-
By ID:
To locate an element on a webpage using ID, we need to have a basic understanding of HTML.Steps to follow:
- Go to “https://www.studysection.com/users/login”.
- Go to the “Log in” field, right-click on it and then click on inspect element.
- We can see the id for the login field is “UserEmail”.
Syntex:
1. driver.findElement(By.id(“id”));
2. WebElement loginById = driver.findElement(By.id(“id”)); -
By name:
From the above image, we can see that the name for the login field is; ”data[User][email]”.
Syntax:
1. driver.findElement(By.name(“name”));
2. WebElement loginByName = driver.findElement(By.name(“name”)); -
By XPath:
- Right-click on the tag line for the login field from inspect elements.
- Hover mouse over the “copy”, it’ll expand other options and then click on “Copy XPath”.
Syntax:
1. driver.findElement(By.xpath(“xpath”));
2. WebElement loginByxpath = driver.findElement(By.xpath(“xpath”));
Finding out the visible and editable fields using a program and implementing these locators
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Test1 {
public static void main(String[] args) {
//setting up Chrome
System.setProperty("webdriver.chrome.driver","D:/Chrome/chromedriver.exe");
//open chrome
ChromeDriver driver = new ChromeDriver();
//open URL
driver.get("https://www.studysection.com/users/login");
//locate element using name and enter value
driver.findElement(By.id("UserEmail")).sendKeys("xyz@abc.com");
//clear the value from textbox
driver.findElement(By.name("data[User][email]")).clear();
//locate element with WebElment and enter value
WebElement eid = driver.findElement(By.id("UserEmail"));
eid.sendKeys("abc@xy.com");
//locate element using xpath and enter value
WebElement pass = driver.findElement(By.xpath("//*[@id=\"UserPassword\"]"));
pass.sendKeys("123456");
//check if the textbox is visible and editable
boolean visible = eid.isDisplayed();
boolean enabled = eid.isEnabled();
if(visible==true && enabled==true) {
System.out.println("Email field is visible and editable");
}else
System.out.println("Email field not visible and editable");
//confirm the value in the field
String fvalue = eid.getAttribute("value");
if(fvalue.contentEquals("abc@xyz.com")){
System.out.println("Email is correct");
}else
System.out.println("Email not matching, entered email is "+fvalue);
driver.close();
}
}
Output:
Email field is visible and editable
Email not matching, entered email is abc@xy.com
If you have skills in PHP programming and you want to enhance your career in this field, a PHP certification from StudySection can help you reach your desired goals. Both beginner level and expert level PHP Certification Exams are offered by StudySection along with other programming certification exams.