Selenium WebElement Methods(sendKeys(),getAttribute(),getTagName(),getSize(),getPosition(),getCssValue(String PropertName))

In Selenium WebDriver when ever we try to idenfity the elements we get the return type as WebElement.

In the below example we are going to explore the different methods available in the WebElement:

HTML Source Code for textbox in Facebook:
<input type="text" class="inputtext _58mg _5dba _2ph-" data-type="text" name="firstname" aria-required="1" placeholder="" id="u_0_1" aria-label="First name"/>

SELENIUM WEBDRIVER CODE:
WebElement element = driver.findElement(By.id("u_0_1"));

Actions performed on the Textbox
element.sendKeys("firstname");
element.clear();
element.click();
element.getAttribute("value");
element.getTagName();
element.getSize();
element.getPosition();
element.isDisplayed()
element.isEnabled()
element.getCssValue(String propertyName) 
 Ex: The propertyName can be background-color.


Actions performed on the button
element.click();
element.submit();

Difference between click() and submit() methods.
If the button is available in the html form then we have to use
submit() method else we have to use click() method.
Refer to the screenshot as displayed below for more information for submit() method:











Misc Information about the WebElement
Apart from the methods mentioned we can findElement() and findElements() methods available.

element.findElement(By)
element.findElements(By)

Installation of Firebug in Chrome and Usage of Firebug in Chrome Browser!!!


Capturing full Screenshot in Selenium WebDriver using java.awt.Robot Class!!!

AWT stands for (Abstract Window Toolkit) is mainly useful to develop windows based applications using Core Java.

Basic Example using AWT as shown below:

import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*
 * Basic Example in AWT, Opens a frame with a button having text 'click me;.
 * Clicking on the button closes the frame.
 * 
 */
class BasicAwtEx 
{  
public static void main(String[] args) {
BasicAwtEx b  = new BasicAwtEx();
b.openDialog();
}
public void openDialog()
{
final Frame f = new Frame();
Button b=new Button("click me");  
b.setBounds(30,100,80,30);// setting button position  
b.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        f.dispose();
        }
     });
 
f.add(b);//adding button into frame  
f.setSize(300,300);//frame size 300 width and 300 height  
f.setLayout(null);//no layout manager  
f.setVisible(true);//now frame will be visible, by default not visible  


}


}

Output:






















Basic Example for taking screenshot using java.awt.Robot Class as shown below:

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import javax.imageio.ImageIO;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
/*
 * 
 * Taking a screenshot using Robot class in java rather than 
 * TakesScreenshot Interface in Selenium Webdriver.
 *
 */

public class CaptureFullScreen {

public static void main(String[] args) throws AWTException {
try 
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");

//Implementing the Robot Class
Robot robot = new Robot();
String format = "jpg";  
String imgFileName= new SimpleDateFormat("mm-dd-yyyy_HH-ss").
format( new GregorianCalendar().getTime());
System.out.println(new GregorianCalendar().getTime());

Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
ImageIO.write(screenFullImage, format, new File("d:\\"+imgFileName+".jpg"));

System.out.println("Screenshot Saved in today's date format in d:\\");
catch (  IOException ex)
{
System.err.println(ex);
}
}
}

Output: