2017-04-20 62 views
0

我有一個表有幾行。其中一些行可能具有特定元素,而其他行可能不具有。肯定有些人會和有些人不會。不等待這麼長的元素

我找到該行並將其放入WebElement中。現在看某元素是否存在i執行以下操作(假定的xp =」 .//someelement)

List<WebElement> eles = row.findElements(By.xpath(xp)); 
if (eles.size() == 0) { 
    // element is not there 
} else { 
    // element is there 
} 

這是細當元件是存在的。如果不是,它需要像30秒或一分鐘弄清楚,它不存在。如果頻繁調用這個真的可以減緩的考驗。我可以用更詳細的異常做

try { 
    WebElement ele = row.findElement(by.xpath(xp)); 
} catch (Exception ex) { 
    // element is not there 
} 

。這工作也沒關係,但同樣的問題。它等待一分鐘半分鐘

有沒有辦法更快速地檢查元素是否存在或不?如果它是相對於驅動程序(driver.findElementBy())而不是一個元素(row.findElementBy()),我想我可能會知道如何去做。

這是Java。

+0

查看WebDriverWait http://stackoverflow.com/questions/11736027/webdriver-wait-for-element-using-java – pbuck

回答

0

在你有第一個元素列表的例子中,你並沒有試圖找到一個元素;但是有幾個(比方說行集合而不是一行)。第二個元素ele找到(或試圖找到)一個特定的項目(比方說1行)。因此,理想情況下,您應該在您的意見中說,某些元素不適用於eles。儘管如此,時間問題可能只是隱含的或明確的等待。請閱讀here瞭解更多信息。

我更喜歡第一種檢查元素集合的方法(因此您可以將它瞄準xpath並找到所有包含的標記(或者根本不包含任何標記)。理想情況下,儘管您應該明確地等待

這是等待的方法,它會基於在輪詢時間內(例如10秒)是否存在該元素返回真/假;值得注意的是,如果元素被發現存在的時間早於10秒的限制,循環會中斷並返回true,當然你可以用timeOut來獲得想要的結果;不要去儘快(比如2秒),否則由於表格沒有加載,你偶爾會冒着失敗的風險:

public boolean waitForElement(String elementXpath, int timeOut) { 

    try{      
    WebDriverWait wait = new WebDriverWait(driver, timeOut); 
    boolean elementPresent=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(elementXpath)).isDisplayed()); 

    System.out.printf("%nElement is present [T/F]..? ")+elementPresent; 
    }   
    catch(TimeoutException e1){e1.printStackTrace();elementPresent=false;}   

    return elementPresent; 
} 

我猜你已經對所有findElement嘗試顯式等待了30秒,因此出現了這種差異。

希望以上幫助,

祝你好運!

+0

如何將行元素傳遞給wait?你能說新的WebDriverEwait(someElement,30)嗎? – Tony

+0

是的,幾乎在那裏!只需在該行的xpath中使用該方法即可。所以你可以輸入:waitForElement(「// div [@class =''row_xpath'// td // tr」,10) –

+0

但是不要每次都輸入一個超時值,你最好將它存儲在一個var(超時)中,並且只有在你知道需要等待更多的特殊情況下才能更改它。 –

0

另一種選擇是使用WebDriverWait(顯式等待)而不是隱式的。

這基本上使得你的測試只會在你告訴他們時等待很長時間,否則如果他們沒有找到你要找的元素,他們會立即失敗。

slide52 of tourdedave's slideshare


// Use this class whenever you have to access the driver 
// And you should only have to setDriver in a BeforeMethod when setting up. 
// This method shows how to do it with a single browser 
// This could be converted quite easily to threadlocals for parallel test runs 
public class DriverManager { 
private final WebDriver driver; 
public static WebDriver getDriver() { 
    return driver; 
} 
public static setDriver(WebDriver driver) { 
    DriverManager.driver = driver; 
} 

public class WaitUntil { 
public static Boolean displayed(By locator, Integer... timeout) { 
    try { 
     waitFor(ExpectedConditions.visibilityOfElementLocated(locator), 
     (timeout.length = 0 : null ? timeout[0];  
    } catch (TimeoutException exception) { 
     return false; 
    } 
    return true; 
} 
// add additional methods you want to wait for 
// Look at the source of the ExpectedConditions class to see how to create 
// your own 
// e.g. presence of element 
// number of results from locator changes 
// element done moving 
// url changes, etc. 


private static void waitFor(ExpectedCondition<WebElement> condition, Integer timeout) { 
    timeout = timeout != null ? timeout[0] : 5; //default timeout if no value given 
    WebDriverWait wait = new WebDriverWait(driver, timeout); 
    wait.until(condition); 
} 

}

然後適應於任何類,你可以 通過submitButtonBy = By.cssSelector(」提交); 使用最好推遲.displayed(submitButtonBy); 它會等待5秒鐘。如果你想等10: 使用WaitUntil.displayed(submitButtonBy,10);

用類似這樣的方法創建一個類的好處是可以很容易地添加額外的異常,因此如果存在陳舊元素或其他東西時可以選擇返回false,而不必處理try catch在頁面類或測試類中。