C# WebDriverWait.Until > Need a simple example for waiting for a url to contain

883 views
Skip to first unread message

Phil

unread,
Mar 8, 2020, 9:53:53 PM3/8/20
to Selenium Users
How do I use this class to wait until a url contains a piece of text? WebDriverWait Class. There is an example on the bottom which shows how to wait for an element but not for a url to contain something. I searched on the net but it's all old answers relating to ExpectedConditions which is going to be deprecated.

For example:
IWebDriver driver;
...
IWebElement e;
e = wait.Until(driver = driver.Url.Contains("/dashboard"));

But this is currently wrong code because it tells me that it can't convert boolean to a IWebDriver type. It makes sense but that's why I need your guys' help to find the answer. Thanks in advance.

Josh Abrahamsen

unread,
Mar 8, 2020, 9:59:25 PM3/8/20
to Selenium Users
Hi Phil,

I have this method I use just for that. Get the nuget package DotNetSeleniumExtras.WaitHelpers for it.

public static string WaitForURL(string url, int timeout=30)
        {
            WebDriverWait wait = new WebDriverWait(Current, TimeSpan.FromSeconds(timeout));
            wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.UrlContains(url));
            return url;

Phil

unread,
Mar 9, 2020, 9:11:22 AM3/9/20
to Selenium Users
Thanks Josh, I will give it a go!

2020年3月9日月曜日 1時59分25秒 UTC Josh Abrahamsen:

Mike Hetzer

unread,
Mar 13, 2020, 2:52:57 PM3/13/20
to Selenium Users
With C# - you can use extension methods, which I find pretty convenient when using Selenium

With the example class below you can do an operation like
driver.WaitUntil_UrlContains("UrlPieceToExpect");

This is significantly cleaner than the 2-3 lines of code needed to perform this operation plus creating a new WebDriverWait instance.

I've done this in my framework for all available ExpectedConditions


using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using System;
using ExpectedConditions = SeleniumExtras.WaitHelpers.ExpectedConditions;

namespace SeleniumBase.PageObjects
{
public static class WebDriverExtensions
{
public static void ClickWithJavaScript(this IWebDriver driver, IWebElement element)
{
IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
executor.ExecuteScript("arguments[0].click();", element);
}
public static void ClickWithJavaScript(this IWebDriver driver, By locator)
{
IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
executor.ExecuteScript("arguments[0].click();", driver.FindElement(locator));
}
public static SelectElement FindSelectElement(this IWebDriver driver, By by)
{
return new SelectElement(driver.FindElement(by));
}

public static IWebDriver GetDefaultContent(this IWebDriver driver)
{
driver.SwitchTo().DefaultContent();
return driver;
}
public static IWebDriver WaitUntil_FrameToBeAvailableAndSwitchToIt(this IWebDriver driver, string frameNameOrId, int timeoutInSeconds = 30)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(frameNameOrId));
}
public static IWebDriver WaitUntil_FrameToBeAvailableAndSwitchToIt(this IWebDriver driver, By locator, int timeoutInSeconds = 30)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(locator));
}
public static bool WaitUntil_AlertState(this IWebDriver driver, bool expectedState, int timeoutInSeconds = 30)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(ExpectedConditions.AlertState(expectedState));
}
public static IWebDriver WaitUntil_TitleContains(this IWebDriver driver, string text, int timeoutInSeconds = 30)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
wait.Until(ExpectedConditions.TitleContains(text));
return driver;
}
public static IWebDriver WaitUntil_TitleIs(this IWebDriver driver, string text, int timeoutInSeconds = 30)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
wait.Until(ExpectedConditions.TitleIs(text));
return driver;
}
public static IWebDriver WaitUntil_UrlContains(this IWebDriver driver, string text, int timeoutInSeconds = 30)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
wait.Until(ExpectedConditions.UrlContains(text));
return driver;
}
public static IWebDriver WaitUntil_UrlMatches(this IWebDriver driver, string text, int timeoutInSeconds = 30)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
wait.Until(ExpectedConditions.UrlMatches(text));
return driver;
}

public static IWebDriver WaitUntil_UrlChangesFrom(this IWebDriver driver, string OldURL, int timeoutInSeconds = 30)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
wait.Until(UrlChanges(OldURL));
return driver;
}
public static IWebDriver WaitUntil_UrlToBe(this IWebDriver driver, string text, int timeoutInSeconds = 30)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
wait.Until(ExpectedConditions.UrlToBe(text));
return driver;
}

}
}



John Pulwers

unread,
Mar 13, 2020, 4:57:15 PM3/13/20
to seleniu...@googlegroups.com
Mike I like your example which is chock full of goodies. How do you Insert the frame of scrollable copiable code? I'd like to learn this for when I need to include one of scripts for help requests. I LOVE that it is C# because most examples seeM to be JAVA

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/1cfb1208-5737-4cde-84c7-25c48b8a16f2%40googlegroups.com.

Mike Hetzer

unread,
Mar 13, 2020, 5:24:58 PM3/13/20
to Selenium Users
John, see screenshot for how to add code snippet.
Yeah usually the java/c# sharp translation with Selenium is just going from Camel to Pascal case.
But with this, we get to spoil ourselves with C# goodies like extension methods and properties with getters/setters, which the java ilk do not get to enjoy.

I've also attached another helper class - it can be used as is or possibly turned into extension methods as well.
At the bottom of the class there is a region where we had to create some custom wait functions.

One such method was RetryClick seen below, created for a problematic "type ahead" field that kept generating a new list of selections after each letter was typed and we had to select one of them to properly set the field.

/// <summary>
/// To be used in the event of resolving immediate flakiness.
/// <para>Alternatives to using this method should eventually be found.</para>
/// <para>Especically in cases where blocking elements are known to occur and are not accounted for.</para>
/// </summary>
public static void RetryClick(this IWebDriver driver, By locator, int timeoutInSeconds = 30)
{
    var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
    if (!wait.Until(Utility.CustomWaitHelpers.ElementToBeClickableAndClickIt(locator)))
    {
        throw new Exception("Element was not able to be successfully clicked!!!");
    }
}


I've been calling these classes up from page/component objects by making a quick property on my BasePage like so . .

public CustomWaitHelpers WaitHelpers
{
    get
    {
return new CustomWaitHelpers(this._driver);
    }
}



CodeSnippet.jpg
Helpers.cs

Josh Abrahamsen

unread,
Mar 13, 2020, 7:32:16 PM3/13/20
to Selenium Users
Great examples in here!

John Pulwers

unread,
Mar 26, 2020, 5:12:19 PM3/26/20
to seleniu...@googlegroups.com
Mike - Where is the "{}" icon? What API is that? Is it VS 2019? I can't seem to find it there even with expanding the tool bar options. I would really love to see step by step how to submit HTML and C# code from VS 2019, so I can ask some questions.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.

Mike Hetzer

unread,
Mar 26, 2020, 6:05:23 PM3/26/20
to seleniu...@googlegroups.com
Hm, idk, it's just there for me on google groups in Chrome.
If anything you could pu some code on Pastebin and refer to it with a link - or Github "gist" is another easy way to share code snippets

You received this message because you are subscribed to a topic in the Google Groups "Selenium Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/selenium-users/AsIwa2-_0OA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CAGNuxc6iMEetTsS-DWam8u3xdv7LW-hMjuPHU0fFw38_eKqzQw%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages