陈旧元素引用:使用if else条件时,元素未附加到页面文档

时间:2016-01-15 06:19:13

标签: java selenium-webdriver selenium-chromedriver

虽然在我的comapny网络中运行时,它工作正常,但是在我的家庭网络中它抛出异常陈旧元素引用:元素未附加到页面文档。 当使用isDisplayed时,如果显示错误消息,它工作正常。但是当元素没有显示时,它应该移动到else条件。我尝试使用try和catch但仍然无法弄清楚。

Console Output:
RRRRRRRR  org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
Exception found ::::org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document


Using below code in main, calling

Acti_05CreateNewCust.errorMsgCust(driver) method
//It is capturing the properties of element which displayed when error will display. 

try     
{
    if(Acti_05CreateNewCust.errorMsgCust(driver).isDisplayed())
        {
            takeSnapShot(driver, "WarnOmCustomerCreation Page");
            if(Acti_05CreateNewCust.click_BtnCancel(driver).isEnabled())
            {
            Acti_05CreateNewCust.click_BtnCancel(driver).click();
            }
            Acti_05CreateNewCust.close_popup(driver);
            Log.warn("Customer has not been created successfully");
            }
}
catch(Exception e)
{

    if(Acti_04ProjandCust.val_newcustomer(driver, ExcelUtils.getCellData(i,0))==true)
    {
        Log.info("Customer has been created successfully");
    }
System.out.println("Exception found ::::"+e);
}


//Acti_05CreateNewCust.errorMsgCust(driver) 

public static WebElement errorMsgCust(WebDriver driver)
     {
        try
        {
            wait = new WebDriverWait(driver, 30);
            element =driver.findElement(By.xpath(".//*[@id='customerLightBox_footer']/table/tbody/tr/td[1]/div/span"));
            wait.until(ExpectedConditions.visibilityOf(element));
            Log.warn("Error message element has been identifeid");

        }
         catch(Exception e)
         {
            System.out.println("RRRRRRRR  "+e);
         }
        return element;
     }

1 个答案:

答案 0 :(得分:0)

让我们尝试下面

public static WebElement errorMsgCust(WebDriver driver)
{
   try
   {
      WebDriverWait wait = new WebDriverWait(driver, 30);
      wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='customerLightBox_footer']/table/tbody/tr/td[1]/div/span")));

   }
    catch(Exception e)
    {
       System.out.println("RRRRRRRR  "+e);
    }

   return driver.findElement(By.xpath(".//*[@id='customerLightBox_footer']/table/tbody/tr/td[1]/div/span"));
}

请提供完整的例外以获取更多信息..

谢谢你, 穆拉利