Author - StudySection Post Views - 4,383 views
selenium

Locating elements(visible/editable) with Selenium

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.

  1. By ID:
    To locate an element on a webpage using ID, we need to have a basic understanding of HTML.

    Steps to follow:

    Syntex:
    1. driver.findElement(By.id(“id”));
    2. WebElement loginById = driver.findElement(By.id(“id”));

  2. 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”));

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

      signin1

    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.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri