Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make selenium No wait for element

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!)
like image 354
Leon Barkan Avatar asked Oct 21 '25 05:10

Leon Barkan


1 Answers

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.

like image 199
Shamik Avatar answered Oct 23 '25 20:10

Shamik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!