Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts
How to get attributes values of element in Selenium using getAttribute Method
Using getAttribute, we can extract the value of attribute of an element in Selenium WebDriver. Extracting values of attribute of element can be helpful in a number of ways. Some of the important use of getattribute method are as follows:
- Name of all the links or button or an object type in the Page.
- Validating if object with particular property exists in the Page.
- Extracting the href value for a button/link.
- Extracting values of element of a particular class or Id. For e.g: If error message are displayed on the page with the same class. We can get all the error messages displayed getting text attribute for all element with the common class name.
Below code explains use of getattribute in Selenium Webdriver for the above purpose.
package testproject;
import java.io.File;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class testingC {
static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
// Connect to the Internet driver server and create an instance of Internet explorer driver.
File file = new File("D:\\selenium\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
try{
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
}
catch(Exception e)
{
driver=new InternetExplorerDriver();
}
driver.navigate().to("https://google.com");
//function to get names of all the links in the Page
getALLNameforObject("link");
// function to validate a particular object with particular text appears in the Page
ValidateObjectExists("link", "About");
//
}
public static void getALLNameforObject(String strObject)
{
if(strObject.toLowerCase().trim().contentEquals("link"))
{
strObject = "a";
}
if(strObject.toLowerCase().trim().contentEquals("button"))
{
strObject = "button";
}
List<WebElement> elemLink = driver.findElements(By.tagName(strObject));
int intLinksinPage = elemLink.size();
System.out.println(intLinksinPage);
for (int i = 0;i<intLinksinPage;i++)
{
System.out.println("The name of the link " + (i+1) +" in the page is :- " + elemLink.get(i).getAttribute("text"));
}
}
public static void ValidateObjectExists(String strObject, String ObjName)
{
if(strObject.toLowerCase().trim().contentEquals("link"))
{
strObject = "a";
}
if(strObject.toLowerCase().trim().contentEquals("button"))
{
strObject = "button";
}
List<WebElement> elemLink = driver.findElements(By.tagName(strObject));
int intLinksinPage = elemLink.size();
System.out.println(intLinksinPage);
for (int i = 0;i<intLinksinPage;i++)
{
if(elemLink.get(i).getAttribute("text").contentEquals(ObjName))
{
System.out.println("Link exists in Page with text " + elemLink.get(i).getAttribute("text"));
}
}
}
}
Highlighting an element using JavaScriptexecutor: Selenium Code Solution
Problem Statement: How to highlight an element in Selenium Webdriver?
Solution : We can highlight an element in Selenium WebDriver by creating a custom method to highlight an element using JavaScriptexecutor.
What is JavaScriptexecutor?
JavaScriptexecutor
class provides mechanism to execute Javascript through selenium driver. JavaScript executor provides two
methods to execute javascript in the code:
a.) executeAsyncScript - Execute an
asynchronous code of JavaScript
b.) executeScript - Executes a code of JavaScript.
Parameters for the two methods are the Script to be executed and arguments for the script.
Below package needs to be imported to use
JavascriptExecutor:
import
org.openqa.selenium.JavascriptExecutor;
Below code explains how to use JavaScriptExecutor to highlight an element in Selenium WebDriver using Java.
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class testingC {
static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
// Connect to the Internet driver server and create an instance of Internet explorer driver.
File file = new File("D:\\selenium\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
try{
driver = new InternetExplorerDriver();
//added new line to maximise browser on launch
driver.manage().window().maximize();
}
catch(Exception e)
{
driver=new InternetExplorerDriver();
}
//Navigate to the webpage and identify the element to be highigted
driver.navigate().to("https://qaautomationqtp.blogspot.com");
WebElement element = driver.findElement(By.linkText("Play"));
//highlight the element
methodhighlightElement(element);
element.click();
}
public static void methodhighlightElement(WebElement element) throws InterruptedException
{
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style','border: solid 8px blue')", element);
Thread.sleep(2000);
js.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "");
}
}
Subscribe to:
Posts (Atom)