There may be cases when you get exception “Element is no
longer attached to the DOM”. This is due to State Element Execption.
I will show you the problem here and then it’s solution that
how we can overcome with this issue.
Just take an example of Cleartrip.com where in round trip
flight search, you will have to select
DepartDate and ReturnDate from date picker. Below is
the code to automate same using webdriver in TestNg Framework.
Here I am giving you the complete set of code so that you
run it and see what happening.
import java.util.List;
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.firefox.FirefoxDriver;
import
org.testng.annotations.AfterTest;
import
org.testng.annotations.Test;
public class roundTripSearch
{
WebDriver driver= new FirefoxDriver();
@Test
public void
tc01_RoundTripSearchResult() throws InterruptedException
{
driver.get("http://www.cleartrip.com/");
if(driver.findElement(By.id("RoundTrip")).isSelected() == true){}
else driver.findElement(By.id("RoundTrip")).click();
driver.findElement(By.id("FromTag")).sendKeys("New Delhi, IN
- Indira Gandhi Airport (DEL)");
driver.findElement(By.id("ToTag")).sendKeys("Mumbai, IN -
Chatrapati Shivaji Airport (BOM)");
driver.findElement(By.id("DepartDate")).click();
List
col1= driver.findElements(By.tagName("td"));
for (WebElement box: col1)
{
if(box.getText().equals("24"))
{
box.findElement(By.linkText("24")).click();
}
}
driver.findElement(By.id("ReturnDate")).click();
List
col2= driver.findElements(By.tagName("td"));
for (WebElement box:
col2)
{
if(box.getText().equals("27"))
{
box.findElement(By.linkText("27")).click();
}
}
driver.findElement(By.id("Adults")).sendKeys("2");
driver.findElement(By.id("Childrens")).sendKeys("1");
driver.findElement(By.id("Infants")).sendKeys("0");
driver.findElement(By.id("SearchBtn")).click();
}
@AfterTest
public void closebrowser()
{
driver.close();
}
}
If you run this, you will get the error with exception:
FAILED:
tc01_RoundTripSearchResult
org.openqa.selenium.StaleElementReferenceException:
Element is no longer attached to the DOM
Command duration or
timeout: 9 milliseconds
For documentation on
this error, please visit:
http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.53.0', revision:
'35ae25b', time: '2016-03-15 16:57:40'
This is due to use of java script. When we select first date, it
automatically open second calendar and the element is lost in way. Here I fixed
this using break statement. See the
code..it works fine.
import java.util.List;
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.firefox.FirefoxDriver;
import
org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
public class roundTripSearch
{
WebDriver driver= new FirefoxDriver();
@Test
public void
tc01_RoundTripSearchResult() throws InterruptedException
{
driver.get("http://www.cleartrip.com/");
if(driver.findElement(By.id("RoundTrip")).isSelected() == true){}
else driver.findElement(By.id("RoundTrip")).click();
driver.findElement(By.id("FromTag")).sendKeys("New Delhi, IN
- Indira Gandhi Airport (DEL)");
driver.findElement(By.id("ToTag")).sendKeys("Mumbai, IN -
Chatrapati Shivaji Airport (BOM)");
driver.findElement(By.id("DepartDate")).click();
List
col1= driver.findElements(By.tagName("td"));
for (WebElement box: col1)
{
if(box.getText().equals("24"))
{
box.findElement(By.linkText("24")).click();
break;
}
}
driver.findElement(By.id("ReturnDate")).click();
List
col2= driver.findElements(By.tagName("td"));
for (WebElement box: col2)
{
if(box.getText().equals("27"))
{
box.findElement(By.linkText("27")).click();
break;
}
}
driver.findElement(By.id("Adults")).sendKeys("2");
driver.findElement(By.id("Childrens")).sendKeys("1");
driver.findElement(By.id("Infants")).sendKeys("0");
driver.findElement(By.id("SearchBtn")).click();
}
@AfterTest
public void closebrowser()
{
driver.close();
}
}
Enjoy.
No comments :
Post a Comment