Using implicit wait in selenium - selenium

I am a beginner. I understand what waits basically does but I am confused over how different tutorials over the internet place it and explain it. For example, in the below code it is placed before loading the URL. So, is it only to wait for the URL to be loaded or for finding the element or both? Is is true that if I use an implicit wait once in my try block, it will be applicable for every element search I am performing in my code?
from selenium import webdriver
driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
driver.get("http://somedomain/url_that_delays_loading")
myDynamicElement = driver.find_element_by_id("myDynamicElement")

ImplicitWait
ImplicitWait as per the Java Docs is to specify the amount of time the WebDriver instance i.e. the driver should wait when searching for an element if it is not immediately present in the HTML DOM in-terms of NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS or DAYS when trying to find an element or elements if they are not immediately available. The default setting is 0 which means the driver when finds an instruction to find an element or elements, the search starts and results are available on immediate basis.
In this case, after a fresh loading of a Webpage an element or elements may be / may not be found on an immediate search. So your Automation Framework may be facing any of these exceptions:
NoSuchElementException
TimeoutException
ElementNotVisibleException
ElementNotSelectableException
Hence we introduce ImplicitWait. By inducing ImplicitWait the driver will poll the DOM Tree until the element has been found for the configured amount of time looking out for the element or elements before throwing a NoSuchElementException. By that time the element or elements for which you had been looking for may be available in the HTML DOM. As in your code you have already set ImplicitWait to a value of 10 seconds, the driver will poll the HTML DOM for 10 seconds.
Python:
driver.implicitly_wait(10)
Java:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
DotNet:
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
Finally, once you set the ImplicitWait, the WebDriver instance i.e. the driver is able to carry this configuration till its lifetime. But if you need to change the coarse of time for the WebDriver instance i.e. the driver to wait then you can reconfigure it as follows:
Python:
driver.implicitly_wait(5)
Java:
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
DotNet:
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
If at any point of time you want to nullify the ImplicitWait you can reconfigure it as follows:
Python:
driver.implicitly_wait(0)
Java:
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
DotNet:
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(0);
Answering your questions
...Wait for the URL... : No, ImplicitWait have no effect on page loading.
...For finding the element... : Yes, ImplicitWait will define the coarse of time the WebDriver instance will wait looking out for the element or elements.
...Implicit wait once... : Yes, you need to configure ImplicitWait only once and it is applicable throughout the lifetime of the WebDriver instance.
...Every element search... : Yes, applicable when ever findElement() or findElements() is invoked.

yes, implicit_wait is globally applicable. so once you set it's applied to all the element.
I would not suggest to use implicit_wait unless your application is too slow. You could use explicit wait or any other wait based on your requirement from the following page.
it's a JAVADOC but implementation should be same for python as well.
https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebDriver.Timeouts.html#implicitlyWait-long-java.util.concurrent.TimeUnit-

Implicit wait is applicable for all the web elements where as Explicit wait is applicable only for the element it is specified.
Explicit wait is more intelligent and are really use full in handling Ajax on the other hand implicit wait is generally used to handle application sync issues.

Related

Alternative of Thread.sleep in selenium

In selenium testing framework I am using thread.sleep(40000). I have a requirement not to use thread.sleep()or its alternative fluent wait or any kind of wait, and keep your script engaged anyhow so that script will pick particular element after that some interval until that element actually appears on that page. Hence error wont be thrown while accessing the element. Do you have any suggestion how can I keep my script engaged for few miliseconds without using any wait ?
I use this code. You can add try catch with timeOutException.
import org.openqa.selenium.By.*;
By element = new ById("id");
long timeout;
WebElement webElement =(WebElement)(new WebDriverWait(getDriver(), timeout)).until(ExpectedConditions.visibilityOfElementLocated(element));

Implicit Wait Timeout not working

Was trying to block execution for 10 seconds properly with VB.Net Selenium, so found out about Implicit Wait on SO, and found this example.
driver.Manage.Timeouts.ImplicitWait = TimeSpan.FromSeconds(10)
Debug.WriteLine(driver.PageSource)
The problem is, I set a break point on both lines and Debug.WriteLine is called nearly instantly. I've read on here I shouldn't use Thread.Sleep here, so why is the timeout not having the desired effect?
Thanks!
An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
May be you missed the parentheses:
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
You can also try using below:
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
Alternatively you can use ExplicitWait:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(--ELEMENT TO BE VISIBLE--));
It is more extendible in the means that you can set it up to wait for any condition you might like. Usually, you can use some of the prebuilt ExpectedConditions to wait for elements to become clickable, visible, invisible etc.
Solution is in C#.NET. You might need to convert some syntax in VB.NET.

Selenium, what is the best practice to deal with element is not clickable

I am using selenium 2.46 (firefox driver) to develop an application. There are a lot of element.click() in my code. Sometimes that elements are not visible or not clickable make the application throws selenium exception.
To resolve that issue, i use WebdriverWait(driver, 10).until(...) for each single element which needs to be clicked.
My question is there is any other better way Or design pattern that can help me to solve the problem best.
Or at least i dont have to use WebdriverWait for each single element needs to be click().
You cannot avoid WebDriverWait. If you send a webdriver click command, webdriver will blindly assume that "element is clickable". You need to instruct webdriver to wait because your element is special and needs some synchronization before it can click on it. I don't think you need to do this for every other element. You can incorporate ExpectedConditions so that you can keep your code snippets manageable and small. So something like,
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.elementToBeClickable(By.id("foo"))).click();
The other option you can try other than clicking is hit enter on respective element, for that you can refer ID of that element.
driver.findElement(By.id("elementid")).sendKeys(Keys.ENTER);
use implicit wait instead of explicit wait and give the expected condition till the element doesn't visible on screen.
for more info you can check
http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#invisibilityOfElementLocated-org.openqa.selenium.By-
Hope this will help you

Is Selenium 2.0 waiting for element / page to load?

I heard that Selenium 2.0. is waiting for element or page to load by default, so there is no longer need to write specific methods like 'waitForElementToLoad' after calling click method.
Is it true? If yes, why can't I find it anywhere in documentation? I constantly find some posts like this, where it's only mentioned:
Selenium - don't wait until all elements are presented
Please advice where can I find any proof of that, what methods are waiting for element to load, and from which version it is implemented?
I am using Selenium 2.0 with Chrome Driver.
Thank you.
As in docs (http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp) :
"An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance."
So, you need to set it manually, ie need to wait elements to load. By default Selenium doesn't wait, as written above.
Also here: https://sqa.stackexchange.com/questions/2606/what-is-seleniums-default-timeout-for-page-loading is mentioned that "The default WebDriver setting for timeouts is never"

How to make xpath work with Selenium when filepicker.io is loaded

After implementing filepicker.io, some of our Selenium regression tests have started failing. The failures (intermittent, but more often than not in some circumstances) are that clicks are ignored on WebElements found via XPath queries. e.g.
driver.findElement(By.xpath("//a[text()='Demo data']")).click();
Adding a Sleep(2000) between findElement() and click() generally resolves the problem. (I say generally because Sleep(1000) was mostly enough, until it wasn't, so I made it Sleep(2000)...)
Checking element.isDisplayed() has not helped. The problem disappears if we stop including the filepicker.io JavaScript file.
Is it something to do with filepicker.io introducing an IFRAME? We have also noticed that JQuery's document.ready() seems to be now invoked twice.
As usual with this kind of problems, you are trying to find an element that is not yet available on the page due to AJAX request still downloading/processing it. You need to wait for the element to appear on the page.
There are three ways to do this:
Using sleep(). This is the discouraged way. You should not use hardcoded sleeps, because you'll either wait too long (making the tests unnecessarily slow) or too short (failing the test).
Use Implicit wait. That will always wait for an element if it's not found.
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
Use explicit wait. That enables you to wait explicitly for one element to (dis)appear / become available / whatever.
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Demo data")));
We now run this code first after opening any page that includes filepicker.js:
while (FindElementsMaybeNone(By.cssSelector("#filepicker_comm_iframe")).size() == 0)
Sleep(50);
while (driver.switchTo().frame("filepicker_comm_iframe") == null)
Sleep(50);
driver.switchTo().defaultContent();
We guess that filepicker's dynamic IFRAME insertion is discombobulating Firefox or Selenium. I'm not marking this as the answer because I don't really know why it works.