How can I use the “sendkeys” method in Selenium for inputting a username and password?

182    Asked by CelinaLagunas in QA Testing , Asked on May 22, 2024

 I am currently engaged in a particular task that is related to testing a web-based application login functionality by using selenium. How can I use the “sendkeys” method for inputting a username and password into their respective text Field? 

Answered by Damini das

 In the context of selenium, here is how you can use the “sendkeys” method in selenium for inputting a username and password along with the handling scenarios like incorrect Credentials or empty fields:-


Import org.openqa.selenium.By;
Import org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebElement;
Import org.openqa.selenium.chrome.ChromeDriver;

Public class LoginTest {

    Public static void main(String[] args) {
        // Set the path to ChromeDriver executable
        System.setProperty(“webdriver.chrome.driver”, “/path/to/chromedriver”);
        // Initialize ChromeDriver
        WebDriver driver = new ChromeDriver();
        // Navigate to the login page
        Driver.get(https://example.com/login);
        // Find the username and password fields by their IDs
        WebElement usernameField = driver.findElement(By.id(“username”));
        WebElement passwordField = driver.findElement(By.id(“password”));
        // Input username and password using sendKeys
        usernameField.sendKeys(“your_username”);
        passwordField.sendKeys(“your_password”);
        // Find and click the login button
        WebElement loginButton = driver.findElement(By.id(“loginBtn”));
        loginButton.click();
        // Check if login was successful
        If(driver.getCurrentUrl().equals(https://example.com/dashboard)) {
            System.out.println(“Login successful!”);
        } else {
            // Handle incorrect credentials or empty fields
            WebElement errorMessage = driver.findElement(By.id(“error-message”));
            System.out.println(“Login failed: “ + errorMessage.getText());
        }
        // Close the browser
        Driver.quit();
    }
}

Here is the method given by using python programming language:-

From selenium import webdriver
From selenium.webdriver.common.by import By
From selenium.webdriver.support.ui import WebDriverWait
From selenium.webdriver.support import expected_conditions as EC
# Set the path to ChromeDriver executable
Chrome_driver_path = “/path/to/chromedriver”
# Initialize ChromeDriver
Driver = webdriver.Chrome(executable_path=chrome_driver_path)
# Navigate to the login page
Driver.get(https://example.com/login)
# Find the username and password fields by their IDs
Username_field = driver.find_element(By.ID, “username”)
Password_field = driver.find_element(By.ID, “password”)
# Input username and password using sendKeys
Username_field.send_keys(“your_username”)
Password_field.send_keys(“your_password”)
# Find and click the login button
Login_button = driver.find_element(By.ID, “loginBtn”)
Login_button.click()
# Wait for the dashboard page to load
Dashboard_url = https://example.com/dashboard
Try:
    WebDriverWait(driver, 10).until(EC.url_to_be(dashboard_url))
    Print(“Login successful!”)
Except:
    # Handle incorrect credentials or empty fields
    Error_message = driver.find_element(By.ID, “error-message”)
    Print(“Login failed: “ + error_message.text)
# Close the browser
Driver.quit()

Here is the method or appropriate approach given in HTML:-




    <meta</span> charset=”UTF-8”>

    <meta</span> name=”viewport” content=”width=device-width, initial-scale=1.0”>

    Login Page



   


        Username:

       


        Password:

       


       

   


   

        Incorrect username or password.

   





Your Answer

Interviews

Parent Categories