Retrieving Text from Disabled Input Fields Using Selenium WebDriver

Retrieving Text from Disabled Input Fields Using Selenium WebDriver

In Selenium WebDriver, a disabled input field typically cannot be directly interacted with. However, there are methods to retrieve the text or value of a disabled input field using JavaScript execution. This guide will demonstrate how to achieve this in different programming languages including Python, Java, and JavaScript with WebdriverIO.

Introduction to Selenium WebDriver

Selenium WebDriver is a popular open-source tool used for automating web browsers. It allows developers to write scripts to interact with web pages, including filling out forms, clicking buttons, and scraping data. Unlike other Selenium operations, directly sending keys to a disabled input field is not possible. However, JavaScript execution in Selenium can bypass this limitation and retrieve the value of a disabled input field.

Retrieving Text from Disabled Input Fields

The core technique involves executing a JavaScript snippet through Selenium's JavaScript execution capabilities. This snippet will select the disabled input field using querySelector and retrieve its value using .value.

Python Example

from selenium import webdriver
# Initialize the WebDriver, replace with your path to the WebDriver
driver  
# Navigate to the page with the disabled input field
URL_OF_YOUR_PAGE  ""
(URL_OF_YOUR_PAGE)
# Use JavaScript to get the value of the disabled input field
disabled_input_value  driver.execute_script(
    "return document.querySelector('input[disabled]').value;"
)
print(disabled_input_value)
# Close the driver
driver.quit()

Java Example

import ;
import ;
import ;
public class GetDisabledInputValue {
    public static void main(String[] args) {
        // Initialize the WebDriver, replace with your path to the WebDriver
        WebDriver driver  new ChromeDriver();
        // Navigate to the page with the disabled input field
        String URL_OF_YOUR_PAGE  "";
        (URL_OF_YOUR_PAGE);
        // Use JavaScript to get the value of the disabled input field
        JavascriptExecutor js  (JavascriptExecutor) driver;
        String disabledInputValue  (String) js.executeScript(
            "return document.querySelector('input[disabled]').value;"
        );
        (disabledInputValue);
        // Close the driver
        driver.quit();
    }
}

JavaScript with WebdriverIO

const { remote }  require(webdriverio);
async function getDisabledInputValue() {
    const browser  await remote({ / your WebDriver config / });
    await browser.url("");
    // Use JavaScript to get the value of the disabled input field
    const disabledInputValue  await browser.execute(
        function() {
            return document.querySelector('input[disabled]').value;
        }
    );
    console.log(disabledInputValue);
    await browser.end();
}
getDisabledInputValue();

Summary

Here are the steps to retrieve the text from a disabled input field in Selenium WebDriver:

Select the disabled input field using querySelector. Retrieve its value using .value. Execute this script through Selenium's JavaScript execution capabilities.

Remember to replace URL_OF_YOUR_PAGE with the actual URL of the page you are testing and adjust the selector in the querySelector method if needed.

By following these steps, you can successfully retrieve the text from any disabled input field using Selenium WebDriver and JavaScript.