2016-10-04 56 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下許多其他的選擇,你應該在鏈接的文檔退房。

相關問題