In this video will see how to get any attributes e.g src(source),text,sizes,class values for any web element.
How to take web element xpath:
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Code:
public class GetAttributes{
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "directory path of chrome driver exe\\chromedriver.exe");
ChromeOptions op = new ChromeOptions();
op.setBinary("directory path of chrome exe\\chrome.exe");
op.addArguments("--remote-allow-origins=*");
WebDriver driver = new ChromeDriver(op);
driver.get("https://programmerworld.co/");
WebElement element = driver.findElement(By.xpath("//img[@class='custom-logo']"));
String attributeSource = element.getAttribute("src");
System.out.println("Source = "+attributeSource);
String attributeSize = element.getAttribute("sizes");
System.out.println("Size = "+attributeSize);
String attributeClass = element.getAttribute("class");
System.out.println("Class = "+attributeClass);
String attributeAlt = element.getAttribute("src");
System.out.println("Alt = "+attributeAlt);
}
Excerpt:
The provided content is a code snippet demonstrating how to retrieve attributes such as source, text, sizes, and class values for a web element using Selenium WebDriver in Java. The code sets up a Chrome WebDriver, navigates to a website, locates an image element by XPath, and retrieves the specified attributes using the getAttribute method. The attributes extracted in this example are source, sizes, class, and alt. The code showcases how to perform these operations for web automation purposes.