How can I use the “getAttribute” method in selenium for Checking if the “submit” button is enabled or disabled?

26    Asked by DavidPiper in Salesforce , Asked on May 9, 2024

 There is a scenario where I am testing a web-based application that has a signup form. As a part of the testing, I need to verify that the “submit” button is disabled until all required fields are filled out by the user. How can I use the “getAttribute” method in Selenium to check if the  “submit” button is disabled or enabled based on its attribute values? 

Answered by David

In the context of selenium, you can verify if the “submit” button is disabled or even enabled based on the value of its attribute by using the “getAttribute” method in selenium with Python. Here is how you can do so:-


From selenium import webdriver

# Assuming you have initialized your WebDriver (e.g., ChromeDriver)
Driver = webdriver.Chrome()
# Navigate to the webpage with the signup form
Driver.get(https://example.com/signup)
# Find the Submit button element
Submit_button = driver.find_element_by_id(“submit_button_id”)
# Get the value of the “disabled” attribute of the Submit button
Is_disabled = submit_button.get_attribute(“disabled”)
# Check if the button is disabled or enabled based on the attribute value
If is_disabled == “true”:
    Print(“Submit button is disabled.”)
Else:
    Print(“Submit button is enabled.”)
# Close the WebDriver
Driver.quit()

Here is the approach given by using the java programming language:-

Import org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebElement;
Import org.openqa.selenium.chrome.ChromeDriver;
Public class SubmitButtonVerification {
    Public static void main(String[] args) {
        // Set the path to the ChromeDriver executable
        System.setProperty(“webdriver.chrome.driver”, “path_to_chromedriver”);
        // Initialize the WebDriver
        WebDriver driver = new ChromeDriver();
        // Navigate to the webpage with the signup form
        Driver.get(https://example.com/signup);
        // Find the Submit button element by its ID (replace “submit_button_id” with the actual ID)
        WebElement submitButton = driver.findElement(By.id(“submit_button_id”));
        // Get the value of the “disabled” attribute of the Submit button
        String isDisabled = submitButton.getAttribute(“disabled”);
        // Check if the button is disabled or enabled based on the attribute value
        If (isDisabled != null && isDisabled.equals(“true”)) {
            System.out.println(“Submit button is disabled.”);
        } else {
            System.out.println(“Submit button is enabled.”);
        }
        // Close the WebDriver
        Driver.quit();
    }
}

Here is the approach given by using HTML along with javascript which would demonstrate the usage of “getAttribute” for checking I the “submit” button is enabled or disabled on its attributes value:-




    Submit Button Verification



   


        Username:

       

       

        Password:

       

       

       

   


    [removed]

        Document.getElementById(“signupForm”).addEventListener(“input”, function() {
            Var username = document.getElementById(“username”).value;
            Var password = document.getElementById(“password”).value;
            Var submitButton = document.getElementById(“submitButton”);
            If (username.trim() !== “” && password.trim() !== “”) {
                submitButton.removeAttribute(“disabled”);
            } else {
                submitButton.setAttribute(“disabled”, “true”);
            }
        });
        Document.getElementById(“submitButton”).addEventListener(“click”, function() {
            Var submitButton = document.getElementById(“submitButton”);
            Var isDisabled = submitButton.getAttribute(“disabled”);
            If (isDisabled === “true”) {
                Alert(“Please fill out all required fields.”);
            } else {
                Alert(“Form submitted successfully!”);
            }
        });

    [removed]





Your Answer

Interviews

Parent Categories