One of the most common error while working with selenium 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. A login example can help us better understand the scenario.
A login screen basically contains three text fields for Client Id, Usercode and Password. The system retrieves the Client Id from database 1 and the password from the 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 the login button, the refreshment of Usercode and Password creates issues with login. To avoid such bugs, you need to focus the mouse pointer before entering the Usercode and Password data.
The “Find Element” command serves as an effective way to solve this issue of finding a web element to be focused within a page. To focus on multiple web elements Find Elements command can be used. One of the ways for uniquely focusing 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();