MirceaM
MirceaM

Reputation: 9

Is there any way to avoid using wait and sleep in selenium?

Is there any way to avoid using driver.wait or driver.sleep commands?

Something like driver.manage().timeouts().implicitlyWait(3000) to be used as a general timeout until the element is located?

I am new in automatic testing and coding :)

Upvotes: 0

Views: 1528

Answers (3)

MirceaM
MirceaM

Reputation: 9

Thank you very much for the answers. I managed to make a "detour" with the following:

function findClickElem(locator, path,  timeout) {

            driver.wait(generalspecs.getSpecs().until.elementLocated(generalspecs.getSpecs().By[locator](path)), timeout).then(function(elem){
                if(elem){
                    elem.click();
                }else{
                    console.log('no element!');
                }
            });
        }

Just added to the generalspecs and gets called each time i use a wait and clicks the element.

findClickElem("xpath" ,"//li[contains(@class, 'classCustom1')]", 15000);

Upvotes: 0

Sudeepthi
Sudeepthi

Reputation: 494

You can use explicit wait

new WebDriverWait(driver, new TimeSpan(0, 1, 0)).Until(ExpectedConditions.ElementIsVisible(By locator));

Waits for a minute for the element.

Upvotes: 0

Ben Smith
Ben Smith

Reputation: 20230

You can set-up explicit and implicit waits in Selenium.

An example of an explicit wait i.e. wait explicitly for a particular element to appear:

IWebDriver driver = new FirefoxDriver();
driver.Url = "http://somedomain/url_that_delays_loading";
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
{
    return d.FindElement(By.Id("someDynamicElement"));
});

An example of an implicit wait (i.e. wait an arbitrary amount of time) is:

WebDriver driver = new FirefoxDriver();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
driver.Url = "http://somedomain/url_that_delays_loading";
IWebElement myDynamicElement = driver.FindElement(By.Id("someDynamicElement"));

See here for more information.

Upvotes: 3

Related Questions