vb.net 等待 selenium 中的特定 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37570322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:54:28  来源:igfitidea点击:

Wait for a particular URL in selenium

c#vb.netseleniumwebautomationchrome-web-driver

提问by IT researcher

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.

我需要在 Chrome 浏览器中使用 Selenium 等待网站自动化中的特定 URL。用户将在我们的网站上进行在线支付。从我们的网站用户被重定向到支付网关。当用户完成支付后,网关将重定向到我们的网站。我想收到从网关重定向到我们网站的通知。

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

我得到了一个在网页中等待“特定 ID”的示例,这是 vb.net 代码

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.

这将导航到“gmail.com”并等待该页面上的 ID“next”。相反,我只想在特定 URL 加载时继续代码。

How can I do this?

我怎样才能做到这一点?

Please help me.

请帮我。

回答by Mobrockers

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

我不确定您使用的是哪种语言,但在 Java 中,您可以执行以下操作:

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:

如果由于某种原因无法使用最新的 selenium 版本,您可以自己实现该方法:

public static Func<IWebDriver, bool> UrlToBe(string url)
{
    return (driver) => { return driver.Url.ToLowerInvariant().Equals(url.ToLowerInvariant()); };
}

回答by Kieran

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

他们现在增加了对预期条件的更多支持。您必须创建一个 webdriver 等待并期望 url 包含一个值

WebDriverWait wait = new WebDriverWait(yourDriver, TimeSpan.FromSeconds(5));
wait.Until(ExpectedConditions.UrlContains("/url-fragment"));