Wait for a particular URL in selenium

大城市里の小女人 提交于 2019-12-11 00:38:43

问题


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.


回答1:


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()); };
}



回答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"));


来源:https://stackoverflow.com/questions/37570322/wait-for-a-particular-url-in-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!