One often doubt while working with selenium

blog_1
APR 09

One of the common issue faced by all those working with Selenium is focusing issue. While interacting with web the users need to find the web content. The concept 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 problems mouse pointer needs to be focussed before entering to Usercode and Password data.

Solution to the problem

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 way for uniquely focussing a web element within a page is by using XPATH.

Syntax for Find Element

driver.get("http://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();