2016-10-04 31 views
-1

我正在获取元素没有附加到上面代码的页面文档错误。此外,我想知道如何处理作为目标页面上的操作结果的“出现”的元素。线程“main”中的异常org.openqa.selenium.StaleElementReferenceException:陈旧的元素引用:元素未附加到页面文档

WebDriver driver = new ChromeDriver(); 
    driver.get("https://www.irctc.co.in/eticketing/loginHome.jsf"); 
    driver.manage().window().maximize(); 
    driver.findElement(By.linkText("Sign up")).click(); 

    //Verify whether the navigation is to the correct page 
    String title=driver.getTitle(); 
    if (title.equals("IRCTC Next Generation eTicketing System")){ 
    driver.findElement(By.id("userRegistrationForm:userName")).sendKeys("abcdef"); 
    driver.findElement(By.linkText("Check Availability")).click(); 

    WebElement text=driver.findElement(By.xpath(".//*[@id='userRegistrationForm:useravail']")); 
    boolean availability=text.isDisplayed(); 
    try {if(availability==true){ 
    System.out.println("The user id is available. Please enter other details to complete registration"); 
    }} 

    catch (NoSuchElementException e){ 
    System.out.println("The user id is not available. Please enter a different user ID."); 
    } 
    } 
    else 
    System.out.println("You are on a wrong page"); 

    } 
    } 

回答

0

您需要创建自定义等待来处理因操作而出现的元素。所以从技术上讲,你正在等待预期条件成立。

private void waitForElement(String element) { 
WebDriverWait wait = new WebDriverWait(Driver, 10); // Wait for 10 seconds. 
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(element))); 
WebElement element = driver.findElement(By.xpath(element)); 
element.click(); // The element is now present and can now be clicked 
} 

在上面的代码中,您知道元素的字符串/ xpath。你可以将它传递给这个方法,并等待10秒钟,使得可信的条件成为现实。如果属实,则可以单击该元素。

0

与其接收异常,最好的方法是使用.findElements()(复数),然后检查是否为空。如果该集合为空,则页面上不存在该元素。如果存在,请抓住.get(0)并使用它。

我为您的代码添加了一些其他优化。我会做更多这样的事情。

driver.get("http://toolsqa.com/automation-practice-table/"); 
driver.findElement(By.linkText("Sign up")).click(); 

// Verify whether the navigation is to the correct page 
if (driver.getTitle().equals("IRCTC Next Generation eTicketing System")) 
{ 
    driver.findElement(By.id("userRegistrationForm:userName")).sendKeys("abcdef"); 
    driver.findElement(By.linkText("Check Availability")).click(); 

    List<WebElement> text = driver.findElements(By.id("userRegistrationForm:useravail")); 
    if (!text.isEmpty()) 
    { 
     // element found 
     if (text.get(0).isDisplayed()) 
     { 
      System.out.println("The user id is available. Please enter other details to complete registration"); 
     } 
    } 
    else 
    { 
     // element NOT found 
     System.out.println("The user id is not available. Please enter a different user ID."); 
    } 
} 
else 
{ 
    System.out.println("You are on a wrong page"); 
} 

如果你执行一些动作,然后需要等待一个元素出现,你可以使用WebDriverWait。下面的示例等待BUTTON可见并点击它。

By buttonLocator = By.id("theButtonId"); 
WebDriverWait wait = new WebDriverWait(driver, 10); 
WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(buttonLocator)); 
button.click(); 

ExpectedConditions下许多其他的选择,你应该在链接的文档退房。

相关问题