How do you handle staleelement reference exception?

driver.get(” “);

driver.manage().window().maximize();

WebElement link=driver.findElement(By.id(” “));

link.click();

driver.navigate().back();

try{

link.click();

}

catch(StaleElementReferenceexception e){

link=driver.findElement(By.id(” “));

link.click();}

 

}}

 

 

 

 

 

 

click on hidden webelement ?

package com.test.java;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Testk {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty(“webdriver.chrome.driver”, “C:\\Users\\Dell\\Desktop\\Maven\\Hybridmodelnaveen\\src\\test\\resources\\executables\\chromedriver.exe”);
WebDriver driver=new ChromeDriver();
driver.get(“http://seleniumpractise.blogspot.com/2016/08/how-to-automate-radio-button-in.html”);

List<WebElement>radio=driver.findElements(By.id(“male”));

System.out.println(radio.size());

for(int i=0;i<radio.size();i++) {

int x=radio.get(i).getLocation().getX();
if(x!=0) {
radio.get(i).click();
break;
}
}
}

}

switch to child window and perform action using selenium

// TODO Auto-generated method stub
WebDriver driver=new ChromeDriver();
driver.get(“http://www.rediff.com/w3c/policy.html&#8221;);
driver.findElement(By.xpath(“//a[contains(text(),’form’)]”)).click();
Set<String>it=driver.getWindowHandles();
Iterator<String>id=it.iterator();
ArrayList<String>a=new ArrayList<String>();
while(id.hasNext()) {
a.add(id.next());
}
driver.switchTo().window(a.get(1));
driver.findElement(By.name(“name”)).sendKeys(“abc”);
driver.switchTo().window(a.get(0));
driver.findElement(By.xpath(“//b[contains(text(),’Rediff USA’)]”)).click();

Sorted set in java

public class Demosor {
public static void main(String[] args) {
// TODO Auto-generated method stub
SortedSet set = new TreeSet();
// Add the elements in the given set.
set.add(“Audi”);
set.add(“BMW”);
set.add(“Mercedes”);
set.add(“Baleno”);
System.out.println(“The list of elements is given as:”);
for (Object object : set) {
System.out.println(object);
}
}
}

comparator in java

public class Demo {

public static void main(String[] args) {
// TODO Auto-generated method stub
List<Integer>l=new ArrayList<>();
l.add(123);l.add(134);l.add(121);l.add(189);
Comparator<Integer>c=new Comparator<Integer>() {
public int compare(Integer i,Integer j) {
if(i%10>j%10)
return 1;
else
return -1;

}
};
Collections.sort(l,c);
System.out.println(l);
}

}

comparable interface in java

public class Stud implements Comparable<Stud> {
int rollno,marks;
String name;
public Stud(int rollno, int marks, String name) {
super();
this.rollno = rollno;
this.marks = marks;
this.name = name;
}
public String toString() {
return “Stud[rollno=”+rollno+”marks=”+marks+”name”+name+”]”;

}
@Override
public int compareTo(Stud o) {
// TODO Auto-generated method stub
return rollno>o.rollno?1:-1;
}

}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Collectiondemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Stud>studs=new ArrayList<>();
studs.add(new Stud(123,43,”helo”));
studs.add(new Stud(122223,41,”helo11″));
studs.add(new Stud(1213,4,”helo11″));
Collections.sort(studs);
for(Stud s:studs) {
System.out.println(s);
}
}
}

add first digit and last digit in a number

package com;
public class Test5 {
public static int sumdigit(int num) {
int firstdigit=0;
int lastdigit=0;

if(num<0) {
return -1;

}

lastdigit=num%10;int rev=0;
while(num!=0)
{

rev=rev*10+num%10;
num=num/10;

}
firstdigit=rev%10;
return firstdigit+lastdigit;

}
public static void main(String[]args) {

System.out.println(sumdigit(1238));
}

}

Selenium and java code