2013-09-05 26 views
2

我正在創建一個自動化框架,我需要從元素中獲取值並將其與預先收集的值進行匹配。現在,問題是,在加載頁面後,div值會更新,所以即使在添加隱式等待/流暢等待/ Thread.sleep後,代碼也不起作用。 但是,當我調試代碼時,代碼正常工作,沒有做任何改變。我不知道必須做什麼。在Selenium測試中找不到元素當div元素在頁面加載後重新加載

由於我是一個新用戶,我不能在這個問題中添加圖片,但最初的 「total:value」是「總數:0」,並且它需要一段時間才能填充,因此它會發生變化,但是由於預先加載了頁面超時不起作用。

請提出建議。

 String sID = oIPops.getLocator("<value from properties file>"); 

    /* 
    WebDriverWait wait = new WebDriverWait(driver,40); 
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(sID))); 
    */ 

    try { 
     Thread.sleep(10); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }  

    String tValue = driver.findElement(By.id(sID)).getText(); 
    System.out.println(""+tValue); // it will print Total : <populated value> 

    // Extracting the number from the string to match with data I have beforehand 
    char num = tValue.charAt(7); 
    String character = "+" + num; 
    Integer newNum = new Integer(character); 
    if (value == newNum) 
    { 
     logger.info("Sum of Situations on Cluster "+title+ " matches with the total value from situation table"); 
    } 
    else 
    { 
     logger.error("Sum of Situations on Cluster "+title+ " does not match with the total value from situation table"); 
    } 
+0

id = sID的元素是否從html的開頭存在,或者是以後注入的整個元素?如果是這樣,它只會改變一次嗎?最後,它在靜態HTML中以某種方式初始化? – luksch

+0

每次頁面加載時,值是否都會更改?我的意思是,你能否明確地等待價值改變,然後才能得到它? –

+0

該元素從默認值0開始存在,實際值稍後注入。因此它只能更改一次,但這取決於刷新頁面或填充id值的表被刷新。我確實嘗試了明確的等待,但它不起作用,正如我上面提到的。 – Avataar17

回答

0

這很容易。你所要做的就是使用FluentWait類的.ignoring方法處理元素異常,然後重試(在有限循環內)以重新找到元素並重試。不使用FluentWait可能會更容易,然後只處理常規Java方式的異常。

相關問題