"Karmanyevadhikaraste ma phaleshu kadachana, Ma karma phala hetur bhurmatey sangostva akarmani."

webdriver3

WebDriver driver= null;
if(browser.equals("Firefox")){
driver= new FirefoxDriver();}
else if (browser.equals("Chrome")){
System.setProperty("webdriver.chrome.driver", "c:\\meeraja\\chromedriver.exe");
driver= new ChromeDriver();

Get the attribute value:

String x= driver.findElement(By.xpath("//*[@id='search-input']")).getAttribute("name");
System.out.println(x);
driver.findElement(By.xpath("//*[@id='search-input']")).sendKeys("meeraja");
System.out.println(x);
driver.findElement(By.xpath("//*[@id='search-input']")).clear();
}}

find Elements:  returns list of all link elements

List of all links in a web page:

WebDriver driver= new FirefoxDriver();
driver.get("http:\\www.yahoo.com");
//List<WebElement> list= new List()
List<WebElement> linklist=driver.findElements(By.tagName("a"));
System.out.println("total links  " +linklist.size());

WebElement link41= linklist.get(22);
System.out.println(link41.getText());

for(int i=0; i<linklist.size();i++){
System.out.println(linklist.get(i).getText());


List all the web elements of fields..
List<WebElement> fields=driver.findElements(By.xpath("//input[@type='text']"));
fields.size();



//suppose in a outer box, there are 5 links, and user wants to click one by one.
//if we check the xpath the last part only changing, so make it 2 strings and add th enumber
//eg://*[@id='news_moreTopStories']/ul/li[1]/a
////*[@id='news_moreTopStories']/ul/li[2]/a

WebDriver driver= new FirefoxDriver();
driver.get("www.bbc.com");
String part1= "//*[@id='news_moreTopStories']/ul/li[";
String part2= "1]/a";

for(int i=1;i<5;i++){
String text= driver.findElement(By.xpath(part1+i+part2)).getText();
System.out.println(text);
driver.findElement(By.xpath(part1+i+part2)).click();
System.out.println(driver.getTitle());


Element present on page:

WebDriver driver= new FirefoxDriver();
driver.get("www.gmail.com");

int count=  driver.findElements(By.xpath("//*[@id='Email']")).size();
System.out.println(count);

function:  
public static boolean isElementPresent(String x_path) {
int k = driver.findElements(By.xpath(x_path)).size();
if(k == 0)
return false;
else

return true;

we can substitute in above code,,
int i=1;
while(isElementPresent(part1+i+part2)){
String text= driver.findElement(By.xpath(part1+i+part2)).getText();
System.out.println(text);
driver.findElement(By.xpath(part1+i+part2)).click();
System.out.println(driver.getTitle());
i++



Drop down list:


WebDriver driver= new FirefoxDriver();
driver.get("http://www.timesofindia.com");
WebElement droplist= driver.findElement(By.xpath("//select[@id='cityselection']"));
//xpath of dropdown
droplist.sendKeys("Goa");  //selected
List<WebElement> options =  droplist.findElements(By.tagName("option"));
//list all the elements with tag name as option
System.out.println("total options is " +options.size());
//no.of options in the dropdown

for(int i=0;i<options.size();i++)
{
//get the text of the option
System.out.println("options are  " + options.get(i).getText() + "...." +options.get(i).getAttribute("selected"));
//get the value true or null based on if it is selected or not..
}


Radio button:

WebDriver driver= new FirefoxDriver();
driver.get("http://www.echoecho.com/htmlforms10.htm");
List<WebElement> radio= driver.findElements(By.xpath("//input[@name='group1']"));//xpath of radio
System.out.println(radio.size());//no.of radio buttons
radio.get(0).click();//will click the first radio button


radio.get(0).getAttribute("checked"); //return true as it is selected
radio.get(1).getAttribute("checked");//
radio.get(2).getAttribute("checked");
radio.get(1).click();//now changing to 2nd radio
radio.get(0).getAttribute("checked");
radio.get(1).getAttribute("checked");//return true as it is selected
radio.get(2).getAttribute("checked");
}

Element visible or not:
WebDriver driver= new FirefoxDriver();
driver.get("http:\\www.yahoo.com");
//List<WebElement> list= new List()
List<WebElement> linklist=driver.findElements(By.tagName("a"));
System.out.println("total links  " +linklist.size());//get no.of links-tells element present or not

WebElement link41= linklist.get(42);//get the 42 rd link
System.out.println(link41.getText());//link text



for(int i=0; i<linklist.size();i++){
System.out.println(linklist.get(i).getText());//get the text of all links
System.out.println(linklist.get(i).isDisplayed());//element is displayed or not


Screenshot:

File srcFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE) ;
FileUtils.copyFile(srcFile, new File("c:\\xxx.jpg"));