logo image

WebElement getAttribute() example in selenium webdriver



getAttribute(java.lang.String name)

It gets the value of a given attribute.

Example:

package com.selenium.practise;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class TestAttribute {

	public static void main(String[] args) {
		System.setProperty("webdriver.chrome.driver", "E:\\Soft Wares\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		
	     driver.get("https://www.google.com/");
	     String attribute = driver.findElement(By.id("gb_70")).getAttribute("href");//'href' attribute value for 'signin' element
	     System.out.println(attribute);
	     driver.close();

	}

}

output :

https://accounts.google.com/ServiceLogin?hl=en&passive=true&continue=https://www.google.co.in/%3Fgfe_rd%3Dcr%26dcr%3D0%26ei%3DkxgAWsmKHYuFogOK5av4CQ