I'm writing a script for an application. The requirement is to click on Login button in the first page, and when it goes to the login page, I have to click on Google login button. The button has this attribute data-testid, which is set to google-login. So I tried to locate the button as below:
WebElement googleLoginBtn = driver.findElement(By.xpath("//button[@data-testid='google-login']"));
But the test is failing with the below error:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@data-testid='google-login']"}
I was able to locate the login button in the same way. It is working fine. Even I tried to target the element in the browser DevTools using the above XPath expression, which locates the button correctly. But I don't know why Selenium is unable to locate it. Could anyone suggest a solution to this?