Friday, November 1, 2013

All about WebElement

"WebElement" is a special type of a class which comes with the selenium libraries which is capable of handling elements in the web site. All method calls will do a freshness check to ensure that the element reference is still valid. This essentially determines whether or not the element is still attached to the DOM. If this test fails, then an is thrown an exception.

Below are the most popular methods in the "WebElement" class with examples.  

Find Elements 

WebDriver A = new FirefoxDriver();
A.get("http://echoecho.com/htmlforms10.htm");

//Finds the Element 
A.findElement(By.xpath("//input[@value='Milk']")).click();
List<WebElement> B = A.findElements(By.xpath(".//td[@class='table5']//input[(@type='radio')]"));
//Gets the Size 
int count = B.size();
System.out.println(count)


Print Element Values  

//Pritns the values 
for(WebElement z : B) {
String x = z.getAttribute("value");
System.out.println(x);

      }

Clicks on the Element 

//clicks on the element 
for(WebElement z : B) {
String x = z.getAttribute("value");
z.click();

      }

Check if the element is clicked or not  

//Checks if an element is clicked or not 
for(WebElement z : B) {
String x = z.getAttribute("value");
System.out.println(z.getAttribute("value")+ "----> "+ z.isSelected());
                 }



Check if the element is enabled or not  

//Checks if an element is enabled or not 
for(WebElement z : B) {
String x = z.getAttribute("value");
System.out.println(z.getAttribute("value")+ "----> "+ z.isEnabled());
             }


Check if the element is displayed or not  

//Checks if an element is displayed or not 
for(WebElement z : B) {
String x = z.getAttribute("value");
System.out.println(z.getAttribute("value")+ "----> "+ z.isDisplayed());            }

Check location of the element 

//Gets the location of the element (If exists)
for(WebElement z : B) {
String x = z.getAttribute("value");
System.out.println(z.getAttribute("value")+ "----> "+ z.getLocation());
   }


Check location of the element 

//Gets the CSS value of the element (If exists)
for(WebElement z : B) {
String x = z.getAttribute("value");
System.out.println(z.getAttribute("value")+ "----> "+ z.getCssValue(x));
       }

No comments:

Post a Comment