I get an exception, “Element is no longer attached to DOM” when I run my code, but In Debug mode, it works fine. I’m using Java and TestNG

In the webpage I’m testing, there are 3 options in a dropdown and on selecting each option, rows in a table gets populated.. I’m just trying to locate the table value through xpath and I get the exception. Sharing the code I tried and the page source snippets. Please, help me out..!!

I get an exception, “Element is no longer attached to DOM” when I run my code, but In Debug mode, it works fine. I'm using Java and TestNG

I get an exception, “Element is no longer attached to DOM” when I run my code, but In Debug mode, it works fine. I'm using Java and TestNG


WebElement sp=driver.findElement(By.xpath("/html/body/div[1]/div/div[2]/div/div[3]/div[1]/div/div/a/button"));

if(sp.isDisplayed())

{

sp.sendKeys(Keys.ENTER);

Thread.sleep(3000);

String spurl=driver.getCurrentUrl();

if(spurl.contains("unbranded-sample-packs"))

{

//Business card sample kit:

WebElement w1=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/button"));

w1.click();

driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/div/ul/li[1]/a/span")).click();

WebElement p=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a"));

if(p.isDisplayed())

{

if(p.getText().equals("£8.00"))

{

System.out.println("\n Business card sample kit displays the correct page");

}

else

{

System.err.println("\n Business card sample kit option displays the wrong page !");

}

}

else

{

System.err.println("\n Prices are not displayed");

}

// Reseller Catalogues:

WebElement selectorElement = driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/button"));

try

{

WebElement sE=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/button/span[2]"));

sE.click();

}

catch (NoSuchElementException e)

{

selectorElement.click();

WebElement sE=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/button/span[2]"));

sE.click();

}

try

{

WebElement elementOfInterest = selectorElement.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/div/ul/li[2]/a/span"));

elementOfInterest.click();

System.out.println("\n Reseller catalogues option is clicked");

}

catch (NoSuchElementException e)

{

selectorElement.click();

WebElement elementOfInterest = selectorElement.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/div/ul/li[2]/a/span"));

elementOfInterest.click();

System.out.println("\n Reseller catalogues option is clicked");

}

// Select dropdown = new Select(driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/div/ul/li[2]/a/span"))); dropdown.selectByVisibleText("Reseller Catalogues");

new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a")));

if(driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a")).isDisplayed())

{

new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a")));

if(driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a")).getText().equals("£10.00"))

{

System.out.println("\n Reseller Catalogues displays the correct page");

}

else

{

System.err.println("\n Reseller Catalogues displays the wrong page !");

}

}

else

{

System.err.println("\n Prices are not displayed");

}

//Unbranded sample pack option is selected

WebElement selectorElement1 = driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/button"));

try

{

WebElement sE=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/button/span[2]"));

sE.click();

}

catch (NoSuchElementException e)

{

selectorElement1.click();

WebElement sE=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/button/span[2]"));

sE.click();

}

try

{

WebElement elementOfInterest = selectorElement1.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/div/ul/li[3]/a/span"));

elementOfInterest.click();

System.out.println("\n Unbranded Sample pack option is clicked");

}

catch (NoSuchElementException e)

{

selectorElement1.click();

WebElement elementOfInterest = selectorElement1.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div[2]/div/div[1]/div/div/ul/li[3]/a/span"));

elementOfInterest.click();

System.out.println("\n Unbranded Sample pack option is clicked");

}

new WebDriverWait(driver, 6).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a")));

WebElement p3=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a"));

if(p3.isDisplayed())

{

new WebDriverWait(driver, 6).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a")));

WebElement p4=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[1]/div/div[2]/div[2]/div/div[1]/div/div/table/tbody/tr[1]/td[2]/div/a"));

if(p4.getText().equals("£12.00"))

{

System.out.println("\n Unbranded Sample Pack displays the correct page");

}

else

{

System.err.println("\n Unbranded sample pack displays the wrong page !");

}

}

else

{

System.err.println("\n Prices are not displayed");

}

System.out.println("\n 'Finished size' dropdowns validated \n");

Reporter.log(" 'Finished size' dropdowns Validation done |");

}

else

{

System.err.println("\n Sample Pack button links to a wrong path");

}

}

else

{

System.err.println("\n Sample packs button is not available");

}

System.out.println("\n Unbranded Sample packs validated \n");

Reporter.log(" Sample packs Validation done |");

}
Add Comment
  • 2 Answer(s)

    stop using absolute xpath’s in ur code, start using relative xpath’s, that ill help u

    Default Answered on November 19, 2015.
    Add Comment

    Hi.. Thanks a lot for your reply.. Even if I use relative xPath, its not consistent.. It is working at times rarely and fails frequently.. Please help me in solving this..

    Default Answered on November 19, 2015.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.