Friday, November 11, 2011

How to locate an element with multiple CSS classes?

Consider if multiple classes are applied to an element, then how to identify it using CSS locator?

See the below HTML element is applied with three CSS classes (i.e. fac, important & cls).

<font class="fac important cls">MULTI CSS</font>

This element can be identified using CSS locator with all the three classes as given below:
String strText1;
strText1=selenium.getText("css=font.fac&&font.important&&font.cls");//Multi CSS
System.out.println(strText1);

Tuesday, October 25, 2011

Sending KeyStrokes

Sending key combination using Selenium-WebDriver

public void fn_sendKeys() {
String strURL;
WebElement txtGoogle;
WebDriver firefox=new FirefoxDriver();

strURL="www.google.com"
firefox.get(strURL);
txtGoogle=firefox.findElement(By.name("q"));

txtGoogle.sendKeys(Keys.CONTROL,"G");//Sending Ctrl+G key combination
}

Monday, October 24, 2011

Checking and Unchecking Web Checkbox

We can use the below methods to check or uncheck web checkbox.


//Checking
public void CheckingChkbox(WebElement chkbx1){
boolean checkstatus;
checkstatus=chkbx1.isSelected();
if (checkstatus==true){
System.out.println("Checkbox is already checked");
}
else
{
chkbx1.click();
System.out.println("Checked the checkbox");
}
}

//Unchecking
public void UnCheckingChkbox(WebElement chkbx1){
boolean checkstatus;
checkstatus=chkbx1.isSelected();
if (checkstatus==true) {
chkbx1.click();
System.out.println("Checkbox is unchecked");
}
else
{
System.out.println("Checkbox is already unchecked");
}
}

Thursday, October 20, 2011

Selecting Multiple Items in Listbox

Selecting multiple HTML Listbox's options

String strURL;
strURL="URL";

WebDriver firefoxDriver=new FirefoxDriver();
firefoxDriver.get(strURL);

WebElement list1;
list1=firefoxDriver.findElement(By.name("lst"));

List<WebElement> lstOptions=list1.findElements(By.tagName("option"));
list1.sendKeys(Keys.CONTROL);
lstOptions.get(0).click();//Selects the first option.
lstOptions.get(1).click();//Selects the second option.