Wait for a particular URL in selenium
Asked Answered
S

2

8

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. From 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.

Surrebuttal answered 1/6, 2016 at 13:35 Comment(0)
M
19

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()); };
}
Melinamelinda answered 1/6, 2016 at 13:46 Comment(7)
I am using vb.net, c# also OK for me.Surrebuttal
I edited my answer to the C# variant. Might not be completely correct as I don't have VS installed at the moment.Melinamelinda
I didnt find difference in your answer. Also I am getting an error 'urlToBe' is not a member of 'OpenQA.Selenium.Support.UI.ExpectedConditions'Surrebuttal
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: github.com/SeleniumHQ/selenium/blob/master/dotnet/src/support/…Melinamelinda
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 ?Surrebuttal
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.Melinamelinda
Let us continue this discussion in chat.Surrebuttal
I
6

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"));
Immerge answered 7/8, 2017 at 4:49 Comment(1)
Be sure to add the Nuget package: DotNetSeleniumExtras.WaitHelpers. https://mcmap.net/q/95459/-c-selenium-39-expectedconditions-is-obsolete-39Mertz

© 2022 - 2024 — McMap. All rights reserved.