How to set focus to a web element in Selenium webdriver

18.8k views Asked by At

I am new to Selenium. I am learning by automating some test scenarios on MakeMyTrip website.

Scenario: Editing the user account created.

Code:(yet to be completed)

public class AccountEdit {

    @Test
    public void AccEdit() 
    {

        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.makemytrip.com/");
        driver.manage().window().maximize();
        driver.findElement(By.id("ssologinlink")).click();
        driver.findElement(By.id("username")).sendKeys("[email protected]");
        driver.findElement(By.id("password_text")).sendKeys("*****");
        driver.findElement(By.id("login_btn")).click();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.findElement(By.id("ssologinlink")).click(); **======> Here I notice the click is not happening to select the My Account or My Profile from the drop down.** 

    }

}   

Kindly let me know how I can take the focus back to the webelement once I login. driver.findElement(By.id("ssologinlink")).click(); works fine the first time but not post the user login.

3

There are 3 answers

1
Maddy On

Some time element ID gets changed post login(something like dynamic ID).. pls. check the element ID again and update..

0
Rek Sel On

Thank you for your comments. The element ID had not changed post the login. I had to wait for the user name to appear before I click on the drop down.

Below is the code which worked for me:

public class AccountEdit {

@Test
public void AccEdit() 
{

    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.makemytrip.com/");
    driver.manage().window().maximize();
    driver.findElement(By.id("ssologinlink")).click();


    driver.findElement(By.id("username")).sendKeys(""[email protected]"");
    driver.findElement(By.id("password_text")).sendKeys("*******!");
    driver.findElement(By.id("login_btn")).click();

     WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[@id='ssologinlink']/strong[contains(text(),'user')]")));
     myDynamicElement.click();

}

}

0
Saifur On

Try waiting for the element to be clickable with Expected Conditions of Explicit waits. See the doc here

public class AccountEdit {

    @Test
    public void AccEdit() 
    {

        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.makemytrip.com/");
        driver.manage().window().maximize();
        driver.findElement(By.id("ssologinlink")).click();
        driver.findElement(By.id("username")).sendKeys("[email protected]");
        driver.findElement(By.id("password_text")).sendKeys("*****");
        driver.findElement(By.id("login_btn")).click();
        //Waiting for the element to be clickable with Explicit wait       
        WebElement myDynamicElement = (new WebDriverWait(driver, 10))
        .until(ExpectedConditions.elementToBeClickable(By.id("ssologinlink")));
        myDynamicElement.click();
    }
}