In the below code i cannot send password keys in the password field, i tried clicking the field, clearing the field and sending the keys. But now working in any of the method. But its working if i debug and test
public class TestMail { protected static WebDriver driver; protected static String result; @BeforeClass public static void setup() { System.setProperty("webdriver.gecko.driver","D:\\geckodriver.exe"); driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); } @Test void Testcase1() { driver.get("http://mail.google.com"); WebElement loginfield = driver.findElement(By.name("Email")); if(loginfield.isDisplayed()){ loginfield.sendKeys("[email protected]"); } else{ WebElement newloginfield = driver.findElemnt(By.cssSelector("#identifierId")); newloginfield.sendKeys("[email protected]"); // System.out.println("This is new login"); } driver.findElement(By.name("signIn")).click(); // driver.findElement(By.cssSelector(".RveJvd")).click(); driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); // WebElement pwd = driver.findElement(By.name("Passwd")); WebElement pwd = driver.findElement(By.cssSelector("#Passwd")); pwd.click(); pwd.clear(); // pwd.sendKeys("123"); if(pwd.isEnabled()){ pwd.sendKeys("123"); } else{ System.out.println("Not Enabled"); }
To fix this, we can either apply explicit wait so that the webdriver waits for the expected condition - invisibilityOfElementLocated of the overlaying webelement. Or, we can apply the expected condition - elementToBeClickable on the webelement that we want to interact with.
Class ElementNotInteractableException Thrown to indicate that although a WebElement is present on the DOM, it is not in a state that can be interacted with. This includes an element that is not displayed or whose center point can not be scrolled into the viewport. See Also: Serialized Form.
NoSuchElementException Ideally, the exception occurs due to the use of incorrect element locators in the findElement(By, by) method. To handle this exception, use the wait command. Use Try/Catch to ensure that the program flow is interrupted if the wait command doesn't help.
Try setting an implicit wait of maybe 10 seconds.
gmail.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Or set an explicit wait. An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. In your case, it is the visibility of the password input field. (Thanks to ainlolcat's comment)
WebDriver gmail= new ChromeDriver(); gmail.get("https://www.gmail.co.in"); gmail.findElement(By.id("Email")).sendKeys("abcd"); gmail.findElement(By.id("next")).click(); WebDriverWait wait = new WebDriverWait(gmail, 10); WebElement element = wait.until( ExpectedConditions.visibilityOfElementLocated(By.id("Passwd"))); gmail.findElement(By.id("Passwd")).sendKeys("xyz");
Explanation: The reason selenium can't find the element is because the id of the password input field is initially Passwd-hidden. After you click on the "Next" button, Google first verifies the email address entered and then shows the password input field (by changing the id from Passwd-hidden to Passwd). So, when the password field is still hidden (i.e. Google is still verifying the email id), your webdriver starts searching for the password input field with id Passwd which is still hidden. And hence, an exception is thrown.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With