2016-04-16 6 views
1
public static IWebElement FindElement(ExpectedConditions expectedConditions, By by, int timeoutInSeconds) 
{ 
    DefaultWait<IWebDriver> wait = new DefaultWait<IWebDriver>(driver); 
    wait.Timeout = TimeSpan.FromSeconds(timeoutInSeconds); 
    wait.PollingInterval = TimeSpan.FromMilliseconds(10000); 
    wait.IgnoreExceptionTypes(typeof(NoSuchElementException)); 

    IWebElement element = 
     wait.Until<IWebElement>(ExpectedConditions.ElementIsVisible(by)); 
} 

Meine Fragen:Ist dies der beste Weg, um auf Selen WebElement zu warten?

Wie dies expectedConditions statt, was zur Zeit in meiner Methode ist setzen?

Ich versuche zu ändern:

IWebElement element = 
     wait.Until<IWebElement>(ExpectedConditions.ElementIsVisible(by)); 

mit diesem:

IWebElement element = 
     wait.Until<IWebElement>(expectedConditions(by)); 

und erhielt diesen Fehler:

Method name expected.

Antwort

4

Die Bis-Methode ein Prädikat als erstes Argument erfordert. Ein Prädikat ist eine Funktion, die in einem regelmäßigen Intervall aufgerufen wird, bis sie etwas anderes als null oder false zurückgibt.

Also in Ihrem Fall müssen Sie es ein Prädikat machen zurückkehren und kein IWebElement:

public static Func<IWebDriver, IWebElement> MyCondition(By locator) { 
    return (driver) => { 
     try { 
      var ele = driver.FindElement(locator); 
      return ele.Displayed ? ele : null; 
     } catch (StaleElementReferenceException){ 
      return null; 
     } 
    }; 
} 

// usage 
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
IWebElement element1 = wait.Until(MyCondition(By.Id("..."))); 

, die gleich:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.Id("..."))); 
element.Click(); 

Sie könnten auch einen Lambda-Ausdruck

verwenden
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); 
IWebElement element = wait.Until((driver) => { 
    try { 
     var ele = driver.FindElement(By.Id("...")); 
     return ele.Displayed ? ele : null; 
    } catch (StaleElementReferenceException){ 
     return null; 
    } 
}); 
element.Click(); 

Oder eine Erweiterungsmethode:

Wie Sie sehen, gibt es viele Möglichkeiten zu warten, bis ein Element ein bestimmter Zustand ist.

Verwandte Themen