Script is getting TimeOutException sometimes. Working fine sometimes - selenium

I've written a script using Selenium with Java. It is working fine sometimes without any kind of exceptions. But sometimes I'm getting TimeOutException as I've used explicit wait. Does this kind of behavior relate to the application? What could be the problem?
ChromeOptions options = new ChromeOptions();
options.addArguments("incognito");
WebDriver driver = new ChromeDriver(options);
driver.get("url");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.id("usernameid")));
driver.findElement(By.id("usernameid")).sendKeys("632145");
wait.until(ExpectedConditions.elementToBeClickable(By.id("passwordid")));
driver.findElement(By.id("passwordid")).sendKeys("1234");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//button[#type='button']")));
driver.findElement(By.xpath(".//button[#type='button']")).click();
The script is failing sometimes at the button. I'm getting TimeOutException.

Mean while just use the "Waiter" api with lots of wait combinations..
<dependency>
<groupId>com.imalittletester</groupId>
<artifactId>thewaiter</artifactId>
<version>1.0</version>
</dependency>

Here, you are using WebDriverWait for 20 seconds on each element without setting ImplicitWait. If you need to wait for 20 seconds definitely on each element, first set ImplicitWait with more than 20 seconds and then use WebDriverWait.
As a side note, ImplicitWait will be applicable only on findElement and findElements methods.
The default timeout what selenium uses to find element is 0 seconds if we are not setting up ImplicitWait.
You can refer more details on this from this url: The default value of timeouts on selenium webdriver

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));

Browser runs faster than webdriver selenium command

I am performing an operation using selenium webdriver to wait for an element until an element is visible. After a few milliseconds, it gets disappeared(Expected).Generally we use explicit wait to synchronize with browser because browser is slower. But in this case, browser is faster and before command waits for the visibility , the element disappears hence failing the operation.
It would be great if anyone can help regarding the issue.
PS I am using jmeter webdriver plugin.
Thanks.
You could handle exception which breaks your validation (ignore NoSuchelementException but fail validation on TimeoutException) or create waiting method which waits for element to appear and after that wait to disappear.

Using implicit wait in 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.

Switching from FireFoxDriver to ChromeDriver cause some element loading errors (Selenium)

I have a selenium project and until now I used FireFoxDriver, but now I tried to use ChromeDriver (which is X100 faster) and I get allot of fails in my tests with the error "element not visible" or "Element is not currently intractable and may not be manipulated"
Is there a need for more wait.until? Why is this?
Selenium needs elements to be visible/exist in the DOM in order to interact with them. As you said, Chrome is faster than FireFox, so the WebDriver is trying to interact with the DOM before when elements are not yet visible/exist.
Explicit wait
wait.until(ExpectedConditions.visibilityOfElementLocated());
And implicit wait
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Should solve your problem.

Automation testing using selenium WebDriver?

Selenium RC command selenium.waitForPageToLoad("30000") is not working in WebDriver.
Is there any alternate command for this in WebDriver?
There are two types of waits you can use in Selenium; implicit and explicit.
Below examples are written in Java:
1) Explicit Wait:
new WebDriverWait(super.getDriver(), 10).until(ExpectedConditions.elementToBeClickable(site_logo));
Above code will wait 10 seconds for site logo element to be clickable, if not it will throw an exception. ExpectedConditions class has bunch of other methods you can use. You can check whether an element is present or not etc...
2) Implicit Wait:
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
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.
There is also Thread.sleep(Time in milliseconds); method, but I don't recommend you to use this one.
For more information: http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp
You can use WebDriveWait to solve it:
http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp
For waiting to an element, use wait.until(ExpectedConditions.visibilityOfElementLocated) :
#Test
public void test1() throws Exception {
WebDriverWait wait = new WebDriverWait(driver, 1);
driver.get("example.html");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(elementLocation)));
driver.close();
}