4

I have the requirement of waiting for a particular URL in website automation using Selenium in Chrome browser. The user will be doing online payment on our website. Fro our website user is redirected to the payment gateway. When the user completes the payment, the gateway will redirect to our website. I want to get notified redirection from gateway to our site.

I got an example which waits for “Particular Id” in the web page, here is vb.net code

driver.Url = "http://gmail.com"
   Dim wait As New WebDriverWait(driver, TimeSpan.FromSeconds(10))
                wait.Until(Of IWebElement)(Function(d) d.FindElement(By.Id("next")))

This navigates to “gmail.com” and waits for ID “next” on that page. Instead, I want to continue the code only when particular URL loads.

How can I do this?

Please help me.

IT researcher
  • 3,132
  • 15
  • 70
  • 133

2 Answers2

12

I'm not sure what language you're using, but in Java you can do something like this:

new WebDriverWait(driver, 20).Until(ExpectedConditions.UrlToBe("my-url"));

To wait until your url has loaded.

If you cannot use the latest selenium version for some reason, you can implement the method yourself:

public static Func<IWebDriver, bool> UrlToBe(string url)
{
    return (driver) => { return driver.Url.ToLowerInvariant().Equals(url.ToLowerInvariant()); };
}
Mobrockers
  • 2,068
  • 1
  • 14
  • 28
  • I am using vb.net, c# also OK for me. – IT researcher Jun 01 '16 at 14:05
  • I edited my answer to the C# variant. Might not be completely correct as I don't have VS installed at the moment. – Mobrockers Jun 01 '16 at 14:10
  • I didnt find difference in your answer. Also I am getting an error 'urlToBe' is not a member of 'OpenQA.Selenium.Support.UI.ExpectedConditions' – IT researcher Jun 01 '16 at 14:24
  • 1
    According to the selenium git history, that expected condition was added in selenium 2.53.0, so if you are not using the latest version you cannot use it yet. See: https://github.com/SeleniumHQ/selenium/blob/master/dotnet/src/support/UI/ExpectedConditions.cs – Mobrockers Jun 01 '16 at 14:26
  • Thank you, I had lower version of selenium and upgraded it now UrlToBe works. I have one more question, is it possible to wait until user clicks on some button in my webpage ? – IT researcher Jun 02 '16 at 06:09
  • If clicking on the button on a page changes something on the page (url change, element disappearing, new element created etc) you can wait for that change to happen. If you want to wait until exactly when a button is pressed, and not until the effect of the button press has happened, I suppose you could add a javascript onclick to your button through selenium that sets a javascript variable when the button is clicked, and then in selenium you can wait until that variable is found in the javascript. I can't help you with the javascript though, and it's better to just wait until html changed. – Mobrockers Jun 02 '16 at 06:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113586/discussion-between-it-researcher-and-mobrockers). – IT researcher Jun 02 '16 at 06:29
2

They have added more support for expected conditions now. You would have to create a webdriver wait and expect the url to contain a value

WebDriverWait wait = new WebDriverWait(yourDriver, TimeSpan.FromSeconds(5));
wait.Until(ExpectedConditions.UrlContains("/url-fragment"));
Kieran
  • 15,368
  • 4
  • 41
  • 51