Header Ads

  • Breaking News

    How to get values from web elements in Selenium webDriver

    In last post we discussed how to find xpaths in selenium webdriver, different functions and axes of xpath in selenium. (If you  don't know how to find xpath, here is the complete guidline to find xpaths). Now In this post we will discuss, how to get values from web elements you already found using xpath.

    Use case 

    Sometimes you want to check the checkbox. You already have xpath for that checkbox. If you directly click on that checkbox it will get checked, but what if it was already checked ? it will get unchecked. So here, before clicking on check box, you need to know the value of check box whether it is checked or not.

    Other use cases are, getting value entered in text box for validation. Getting selected element from dropdown.

    Let's see how to get those values from web-elements in selenium webdriver. I am writing my scripts in Java.


    How to get values from TextBox in Selenium Webdriver  

    How to get values from web elements in Selenium webDriver
    How to get values from web elements in Selenium webDriver

    You know how to find Xpath of selenium. Now you wan to find text entered in the textbox. So how to find these text entered in Textbox. Here is the code in java - 


    WebElement textbox= driver.FindElement(By.Id("TextBoxID"));
    if (textbox != null) {
    elementController.implicitWait(1);
    existingValue=textbox.getAttribute("value");
    if(existingValue.equals("xyz"))
    isVerified=true;



    Here, getAttribute("value") Will return the value of that text box. Sometime people ended up using method webelement.text which most of time fails as it returns inner html of that webelement. 

    How to get Selected value from DropDown in Selenium Webdriver  

    If you want to verify if dropdown selected proper value or not, you can use this method. it will give you the top selected value of dropdown. Here is the code in java - 


    select = new Select(driver.FindElement(By.Id("DropDownID"));
    elementController.implicitWait(1);
    String existingValue= select.getFirstSelectedOption().getText();
    if (existingValue.equalsIgnoreCase(attributeValue)) {
    isVerified = true;
    }


    e;">Select select = null;

    getFirstSelectedOption() will return the xpath of first element (which is selected) and getText() will give you the text of that element which you can check. 


    How to check if checkbox was checked or not in selenium webdriver

    Identifying if checkbox is checked or not is little tricky. If it is plain HTML then you can directly use this one 

    checkBox = driver.findElement(By.Id("CheckBox")); if(!checkBox.isSelected())
    checkBox1.click(); 
    }

    Sometimes when checkbox is checked then one of the value like class or name get changed to checked. So you can do like this checkBox .getAttribute("class").equalsIgnoreCase("checkbox-checked")

    Here when check box is checked then class getting changed to 
    checkbox-checked.

    So checkbox, dropdown (multilist) and check box are essential elements. When you want to verify forms or something similar then getting there values are very important. Same way you can get the value of Radio Buttons (true or false) .

    Hope this  article  helped you. For any query you can comment, mail us. Please do subscribe with email to get updated articles in your mailbox. Follow on social media and share this article.  


    Reading List 

    Post Top Ad

    Post Bottom Ad