4

I am using selenium for a while and have some question about the IWebElement Wait procedure.

We have the Explicit Wait and Implicit I use them and understand the meaning but..

How can I make selenium no wait for element at all?

I tried not to use wait functions but still when I call FindElement or GoToUrl its not always return immediately sometimes still wait for 0 to 60 sec

I noticed that in most of time the wait in FindElement didn't return the element and wait for no reason.

for example: call for element id can take 3 sec and not immediately (lot off calls lot of time...) maybe I am doing something wrong.

the main purpose is to take full control of the program and handle the wait time myself (for better efficiency)

maybe there are better articles to understand selenium architecture of finding elements ? (not the selenium API)

(I am using latest version of selenium 2.48.0)

Code example:

driver = new FirefoxDriver();
js = driver as IJavaScriptExecutor;
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(0));
driver.Manage().Window.Maximize();

driver.Navigate().GoToUrl("someUrl");

IList<IWebElement> loginFrame = driver.FindElements(By.TagName("iframe"));
driver.SwitchTo().DefaultContent().SwitchTo().Frame(loginFrame[0]);

driver.FindElement(By.Id("userID")).SendKeys("username");
driver.FindElement(By.Id("userPassword")).SendKeys("userPassword");
driver.FindElement(By.Id("login")).Click();

driver.SwitchTo().DefaultContent();
driver.FindElement(By.XPath("//div[@class='something']/ul/li[2]/a")).Click();
driver.FindElement(By.PartialLinkText("someText")).Click(); // *
  • At Last call its throw exception after something like 3 ~ 5 seconds and not immediately (When I set Implicit to 60 sec it's find the element!)

2 Answers 2

4

I am not sure what you mean by "how can I make selenium no wait for element at all? "

If you mean that you dont want selenium to wait at all for an element at all. I think this might work -

driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); 
WebElement textbox = driver.findElement(By.id("textbox"));

Use this and write your own method which takes a locator and no of seconds to wait and then set the implicit wait inside the method depending on what has been passed to the method.

public WebElement locateElementById(int timeInSec, String id){
    driver.manage().timeouts().implicitlyWait(timeInSec, TimeUnit.SECONDS); 
    WebElement element = driver.findElement(By.id(id));
    return Element

}

Something like this. And then you can call it with the seconds you want.

when you want 0 seconds pass 0 when you want 10 seconds pass 10.

Sign up to request clarification or add additional context in comments.

11 Comments

i used : driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(0));
Hmm i think it should work .. Please try again and if it doesnt post your code.
I tried it but still when i call driver.FindElement(By.PartialLinkText("enter")).Click(); that function waits for couple of sec and throw exception what i want that it will throw the exception immediately and not waiting for 5 sec.. no element throw exception and let me handle it.
Post your relevant code. It should not wait at all if nothing is set as timeout. Default value is 0.
driver.FindElement(By.PartialLinkText("someText")).Click(); This line is trying to find a element with some text so i believe there might be some processing and searching time required for this, as it is a very inefficient way to find an element. Can you try with a more definite locator like ID and see how much time it takes.
|
0

Aforementioned code to set zero wait time

driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); 

is deprecated, for Selenium 4 use:

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(0));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.