Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for a particular URL in selenium

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.

like image 213
IT researcher Avatar asked Jun 01 '16 13:06

IT researcher


People also ask

How do I make Selenium wait for some time?

We can make Selenium wait for 10 seconds. This can be done by using the Thread. sleep method. Here, the wait time (10 seconds) is passed as a parameter to the method.

What is the wait command in Selenium?

In automation testing, wait commands direct test execution to pause for a certain length of time before moving onto the next step. This enables WebDriver to check if one or more web elements are present/visible/enriched/clickable, etc.

How do you add an explicit wait?

Syntax of Explicit wait in selenium webdriver // Create object of WebDriverWait class WebDriverWait wait=new WebDriverWait(driver,20); // Wait till the element is not visible WebElement element=wait. until(ExpectedConditions. visibilityOfElementLocated(By.


1 Answers

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()); };
}
like image 55
Mobrockers Avatar answered Sep 30 '22 19:09

Mobrockers