One common error while working with Selenium

blog_1
08 Jan 2020

One of the most common issues faced by software testing service companies and all others testing mobile applications with Selenium is the focusing issue. While interacting with web, the users need to find the web content. The scenario can be better understood with the help of a login example.

blog_1

A login screen basically contains three text fields for Client Id, Usercode and Password. Client Id is retrieved from database 1 and password from database.

After a user enters Client Id, Usercode and Password the pointer again return back to Client Id. In this case when the user presses login button Usercode and Password get refreshed which creates issues with login. To avoid such bugs mouse pointer needs to be focussed before entering to Usercode and Password data.

Solution to the issue

The "Find Element" command serves as an effective way to solve this issue of finding a web element to be focussed within a page. To focus on multiple web elements Find Elements command can be used. One of the ways for uniquely focussing a web element within a page is by using XPATH.

Syntax for Find Element

driver.get("https://198.38.85.16/Humantiz.ver2/NewLogin.aspx");

driver.findElement(By.xpath("//input[@id='txtClientId']")).sendKeys("MS00001");

JavascriptExecutor js = (JavascriptExecutor) driver;

js.executeScript("document.getElementById('txtUserName1').focus();");

js.executeScript("document.getElementById('txtUserName1').value='admin';");

Thread.sleep(3000);

js.executeScript("document.getElementById('txtPassword').value='admin@12';");

WebElement login=driver.findElement(By.xpath("//input[@type='submit'and

@name='btnLogIn']"));

login.click();