Friday, 14 February 2014

How to get the value of attribute in selenium webdiver in Java?

Selenium webdriver provides one method called - getAttribute() which can be used to read the value  of any web element's attribute in Selenium Webdriver in Java.

String actualValue = c.getAttribute("value");
above code will return the value of the attribute - value of the webelement c in selenium webdriver.

Full example in Java with selenium webdriver

package temp;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class first {

public static void main(String[] args) {
              // TODO Auto-generated method stub

System.setProperty("webdriver.chrome.driver""C:\\Selenuim\\chromedriver2.3.exe");
WebDriver driver =  new ChromeDriver();

try{
driver.get("http://register.rediff.com/register/register.php");

Thread.sleep(2000);
WebElement e = driver.findElement(By.tagName("td"));

String actualValue = e.getAttribute("value");


       System.out.println("value of the e -> " + actualValue);
      
Thread.sleep(2000);

}

catch(Exception ex){
       System.out.println("Exception " + ex.getMessage());
              }
              finally{
                    
                     driver.close();
                     driver.quit();
              }
       }

}


Please note that we can get the value of  attribute of all other web elements  like button, checkbox, radiobutton, checkbox, combobox in similar way using getAttribute method in Java using selenium webdriver.

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

1 comment:

  1. Hi Sagar
    Your examples helped me very much
    If you have videos for selenium WD training [in java ] and willing to sell
    please let me know, I will buy them.
    Thanks
    Eric
    New York, US

    here is my email
    *****************************************************************
    eric.smith589@yahoo.com
    *****************************************************************

    ReplyDelete

Buy Best Selenium Books