Selenium Tricks-How to handle driver click issue while page navigation .

Test Automation

It is quite common that while doing web automation(using selenium) test script fails because of some exception where driver did not clicked to a web object even when same(webobject) is available, which can be handled by keeping implicit, explicit wait however it is possible sometimes that even if a element exist selenium driver does not click. Usually stale element exception or webdriver exception is the error which comes for such cases.

This issue mostly occur when on clicking a button or a menu another page should open to handle such transaction or workflows we can use do-while loop (java) to handle it.

Below written code can be used, In below code script is trying to click a button until label of next page is not read .
a counter Counter is being initialize as zero in the beginning and inside do-while loop
driver is first clicking that button and trying to match label value inside if condition, On matching if condition script is increasing counter value to 1 and breaking the loop.

int Counter=0

do
{
try
{
String str ;
driver.findelement(By.xpath("Page1button")).click();
str = driver.findelement(By.Xpath("Page2Label")).gettext();
if(str.equals("Page2Label"))
{
Counter=Counter+1;
break;
}
}
catch(Exception ex)
{

}
)
while(Counter == 0)

Above code will go in a do-while loop and perform click action until it find next page label. This is one fine way to handling such selenium exception where click is not happening normally.

I have used this piece of code more often using web automation. Hope this post will help you in finding simple and unique way of handling driver click failure issue.

Its Prashant Chaudhary, I have 10+ years of experience in software test automation, performance & process automation with various commercial and open source tools. This blog shares my experience on the topic of Automation. The content published on my blog is solely mine, it does not express the views of any of my employers (current or previous).
No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Selenium Basic
1
Step by step selenium – step 3, what is findelement method

Abstract- So far, we have learned how to launch a web browser, entering values in a text box and clicking button. In this post, we will learn about findelement method. Test automation is all about get and set, which means get from web and set into web. Talking more specifically- …

Test Automation
1
What is ITestListner interface and its Use in Selenium

There are several interfaces available in TestNG that allow you to modify TestNG’s behavior or to give test scripts additional strength. These interfaces are broadly called “TestNG Listeners”. Here are a few listeners which are commonly used: 1- IAnnotationTransformer 2- IAnnotationTransformer2 3- IHookable 4- IInvokedMethodListener 5- IMethodInterceptor 6- IReporter 7- …

Test Automation
How to Capture Screenshot in Selenium Webdriver-3 different ways

Abstract– Capturing screenshot is an essential feature of a test automation framework, They help in defect analyses once after test execution is over. In this blog post we will see how to capture screenshot, there are different ways to capture screenshots. Which includes- 1- Capture full Page 2- Capturng full …

Shares