TestingDocs.com
    Software Testing website
    • Automation
      • Selenium
      • JBehave Framework
    • Tutorials
      • MySQL Tutorials
      • Testlink
      • Maven
      • Git
    • IDEs
      • IntelliJ IDEA
      • Eclipse
    • Flowcharts
      • Flowgorithm
      • Raptor
    • About

    Selenium

    Selenium Find Element using By ID Attribute

    Overview

    In this tutorial, we will learn to inspect and find element on a web page using the ID Attribute. We will test the TestLink Login page in this example. TestLink is an open-source Testcase Management application.

    Environment and Tools

    The environment and the tools used in this example:

    • JDK
    • Maven build tool
    • Selenium Webdriver
    • Eclipse IDE
    • Firefox Web browser Tools
    • Windows 10

    Inspect Elements

    To inspect elements using Firefox Web Developer Tools, follow this link:

    https://www.testingdocs.com/inspect-elements-using-firefox-web-developer-tools/

    Sample Test Example

    In this example test we will identify the elements using the ID attribute. Launch the Web developer tools to inspect the elements in the login form.

    For example the login name text box element using the id: tl_login

    By.id(“tl_login”)

    The HTML markup code for the element is:

    <input maxlength=”100″ name=”tl_login” id=”tl_login” type=”text” class=”form__input” placeholder=”Login Name” required=””>

     

    By ID Attribute Selenium locator

    Similarly repeat the process for the password textbox and Log in button.

    The HTML code for the password text box is:

    <input name=”tl_password” id=”tl_password” type=”password” class=”form__input” placeholder=”Password” required=””>

    The id of the element is: tl_password

    To identify the element we can use he following code:

    driver.findElement(By.id(“tl_password”)) .sendKeys(“admin”);

    Code Listing

    package com.testingdocs.tests;
    
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import org.testng.annotations.AfterClass;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.Test;
    import org.openqa.selenium.By;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.concurrent.TimeUnit;
    
    // Selenium Tutorials - www.TestingDocs.com
    
    public class FindByIDExampleTest {
     public WebDriver driver;
    
    
     @BeforeClass
     public void setUp() throws MalformedURLException {
     DesiredCapabilities dCaps = new DesiredCapabilities();
     dCaps.setBrowserName("chrome");
     driver = new RemoteWebDriver(new 
    URL("http://localhost:4444/wd/hub"), dCaps);
     }
    
     @Test
     public void testIDLocator() throws InterruptedException {
     driver.navigate()
    .to("http://localhost/testlink/login.php");
     driver.manage().window().maximize();
     try {
     driver.findElement(By.id("tl_login"))
    .sendKeys("admin"); 
     driver.findElement(By.id("tl_password"))
    .sendKeys("admin");
     driver.findElement(By.id("tl_login_button"))
    .click();
     driver.manage().timeouts()
    .implicitlyWait(60, TimeUnit.SECONDS);
     System.out.println("By.id demo Login 
    with default credentials...");
     
     } catch (Exception e) {
     System.out.println(e.getMessage());
     }
     }
    
     @AfterClass
     public void tearDown() {
     if (driver != null) {
     driver.quit();
     }
     }
    }

     

    Selenium Find Element Test ID Attribute

     

    Run the test with TestNG framework. Right click >> Run as >> TestNG Test

    Note that we are just identifying elements and performing some actions on them. We are not testing any expected output in the test.

    —

    Selenium Tutorials on this website:

    https://www.testingdocs.com/selenium-webdriver-tutorial/

    Official Website:

    https://www.selenium.dev/

    Related Posts

    Windows 10 Settings

    Selenium /

    Add Microsoft Webdriver on Windows OS

    Download Selenium Components

    Selenium /

    Getting Started with Selenium Webdriver

    LambdaTest Testing Cloud SaaS Platform

    Selenium /

    LambdaTest – Testing Cloud SaaS Platform

    Selenium /

    Selenium 3.0 and Mozilla GeckoDriver

    SauceLabs Website

    Selenium /

    Run an Example Test on SauceLabs

    ‹ EventFiringWebDriver› Inspect elements using Firefox Web Developer Tools

    Recent Posts

    • ChatGPT Plans Free and PlusChatGPT Subscription Plans
    • Stellar Converter for Database ToolStellar Converter for Database
    • Stellar MySQL Log AnalyzerStellar Log Analyzer for MySQL
    • Stellar Repair for MySQLStellar Repair for MySQL
    • ChatGPT IntroductionChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI FeaturesChatGPT4 Conversational AI Features
    • Trends in Software EngineeringShaping the Future of Development: Exploring Key Trends in Software Engineering
    • Java PerformanceImproving Java Performance with Multithreading
    • QDrant Vector DatabaseOpen-source Vector Databases
    • Difference between PHP and JavaScript?
    • Bing AI Browser Web ContentBing Conversation Styles
    • ChatGPT PreviewChatGPT Introduction
    • Open Source AI Frameworks TensorFlowOpen Source AI Frameworks
    • Artificial Intelligence Tools

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com