Selenium WebDriver: Interacting with WebElements – Textbox

Selenium WebDriver Inspect Element

Interacting with WebElements (Textbox)

In previous post, we learnt various techniques to identify locators(WebElements). Now we will see how to interact with these WebElements and automate our AUT.

First of all, create a new Java Project and configure Selenium jars and ChromeDriver.exe as mentioned in first post. Once the project is created, create a package and add a Class under it. Select main method while creating this class.

First of all we will declare our WebDriver object:
public class DropdownValues {
    public WebDriver driver;

    public static void main(String[] args) {
            
    }
}

Now we will create a method to navigate to our AUT. Recently ChromeDriver started throwing exceptions while working in Linux. Following code is used to initialize ChromeDriver.

public void getUrl() {
        System.setProperty("webdriver.chrome.driver", "/home/dj/eclipse-workspace/SeleniumAutomation/Driver/chromedriver");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("start-maximized"); // open Browser in maximized mode
        options.addArguments("disable-infobars"); // disabling infobars
        options.addArguments("--disable-extensions"); // disabling extensions
        options.addArguments("--disable-gpu"); // applicable to windows os only
        options.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
        options.addArguments("--no-sandbox"); // Bypass OS security model
        driver = new ChromeDriver(options);
        driver.get("https://www.wikipedia.org/");
        
    }

System.setProperty method is used to tell the WebDriver that we are going to use Chrome browser and the location where the ChromeDriver.exe is placed. If you are working in windows, just change the location of your WebDriver, put .exe prefix after ChromeDriver and replace the / with .

Other options are used to open the browser in native mode, disable all extensions and other properties.

get() method is used to navigate to desired url. To run this code add following code to your main method:
public static void main(String[] args) {
        DropdownValues obj = new DropdownValues();
        obj.getUrl();

    }

In above, we are creating an object of our main class and calling the getUrl() method. This will open a browser and then navigate to the entered url.

Input some text in Wikipedia searchbox

Once navigated to Wikipedia website, right click on Wikipedia search box and click on “Inspect Element”.

Selenium WebDriver Inspect Element

In bottom Elements section, we can see that this WebElement is having an id. We will first use this id to locate our Search box WebElement and then enter some text in it.

Create a new method enterText() in same class and add following code to locate Search box and then enter some text in it.

public void enterText() {
        WebElement searchBox = driver.findElement(By.id("searchInput"));
        searchBox.sendKeys("Test");
    }

searchBox is the WebElement type variable which hold reference to search box. Using the ‘.’ (dot) operator we can see a list of all actions(and beyond) which can be performed on this textbox. One thing to note here is that putting a ‘.’ after searchBox will list down all the possible methods/actions which a WebElement can do, but we need to use our intelligence which method to use. For instance, a WebElement type variable of a link will show clear() method but this action is not applicable on links.

Now sendKeys() is one of the WebDriver methods which is used to enter any text in a textbox. This method require a string parameter.

Update the main method by adding below code:
public static void main(String[] args) {
        DropdownValues obj = new DropdownValues();
        obj.getUrl();
        obj.enterText();
    }

Upon execution, it will open chrome browser, navigate to wikipedia and enter “Test” in the textbox.

Author: Dhawal Joshi

A post-graduate in MCA, ISTQB & ITIL certified QA with more than 8 years of experience in QA working with a CMMI Level 5 organization as System Analyst. I started my automation journey with HP UFT(formerly known as QTP) and for the past few years, I am using Selenium for automation. I also have experience in Android Application Development, Java, HTML, and VBScript. When I am not working, I like to spend time with my family, cooking and learning new developments in IT.

Leave a Reply

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