stack over flowにそれっぽいのがあったので一先ず解決にします。
(これでもダメなときはダメですがマシにはなりました。)
需要があるかは分かりませんが一応、共有しておきます。
以下の拡張クラスをプロジェクトに追加します。
C#
1/// <summary>2/// Method that finds an element based on the search parameters within a specified timeout.3/// </summary>4/// <param name="context">The context where this is searched. Required for extension methods</param>5/// <param name="by">The search parameters that are used to identify the element</param>6/// <param name="timeOutInSeconds">The time that the tool should wait before throwing an exception</param>7/// <returns> The first element found that matches the condition specified</returns>8 public static IWebElement FindElement(this ISearchContext context, By by, uint timeOutInSeconds)9{10if(timeOutInSeconds >0)11{12 var wait = new DefaultWait<ISearchContext>(context);13 wait.Timeout = TimeSpan.FromSeconds(timeOutInSeconds);14return wait.Until<IWebElement>(ctx => ctx.FindElement(by));15}16return context.FindElement(by);17}18/// <summary>19/// Method that finds a list of elements based on the search parameters within a specified timeout.20/// </summary>21/// <param name="context">The context where this is searched. Required for extension methods</param>22/// <param name="by">The search parameters that are used to identify the element</param>23/// <param name="timeoutInSeconds">The time that the tool should wait before throwing an exception</param>24/// <returns>A list of all the web elements that match the condition specified</returns>25 public static IReadOnlyCollection<IWebElement>FindElements(this ISearchContext context, By by, uint timeoutInSeconds)26{2728if(timeoutInSeconds >0)29{30 var wait = new DefaultWait<ISearchContext>(context);31 wait.Timeout = TimeSpan.FromSeconds(timeoutInSeconds);32return wait.Until<IReadOnlyCollection<IWebElement>>(ctx => ctx.FindElements(by));33}34return context.FindElements(by);35}36/// <summary>37/// Method that finds a list of elements with the minimum amount specified based on the search parameters within a specified timeout.<br/>38/// </summary>39/// <param name="context">The context where this is searched. Required for extension methods</param>40/// <param name="by">The search parameters that are used to identify the element</param>41/// <param name="timeoutInSeconds">The time that the tool should wait before throwing an exception</param>42/// <param name="minNumberOfElements">43/// The minimum number of elements that should meet the criteria before returning the list <para/>44/// If this number is not met, an exception will be thrown and no elements will be returned45/// even if some did meet the criteria46/// </param>47/// <returns>A list of all the web elements that match the condition specified</returns>48 public static IReadOnlyCollection<IWebElement>FindElements(this ISearchContext context, By by, uint timeoutInSeconds,int minNumberOfElements)49{50 var wait = new DefaultWait<ISearchContext>(context);51if(timeoutInSeconds >0)52{53 wait.Timeout = TimeSpan.FromSeconds(timeoutInSeconds);54}5556// Wait until the current context found the minimum number of elements. If not found after timeout, an exception is thrown57 wait.Until<bool>(ctx => ctx.FindElements(by).Count >= minNumberOfElements);5859//If the elements were successfuly found, just return the list60return context.FindElements(by);61}
使い方は以下のような感じです。
C#
1ar driver = new FirefoxDriver();2driver.Navigate().GoToUrl("http://localhost");3var main = driver.FindElement(By.Id("main"));4// It can be now used to wait when using elements to search5var btn = main.FindElement(By.Id("button"),10);6btn.Click();7//This will wait up to 10 seconds until a button is found8var button = driver.FindElement(By.TagName("button"),10)9//This will wait up to 10 seconds until a button is found, and return all the buttons found10var buttonList = driver.FindElements(By.TagName("button"),10)11//This will wait for 10 seconds until we find at least 5 buttons12var buttonsMin= driver.FindElements(By.TagName("button"),10,5);13driver.Close();
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。