element not interactable exception in selenium web

2020-07-03 04:36发布

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("ragesh@gmail.in");
   }
   else{
  WebElement newloginfield = driver.findElemnt(By.cssSelector("#identifierId"));                                      
       newloginfield.sendKeys("ragesh@gmail.in");
      // 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");
 }

4条回答
仙女界的扛把子
2楼-- · 2020-07-03 04:58

Please try selecting the password field like this.

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement passwordElement = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#Passwd")));
    passwordElement.click();
  passwordElement.clear();
     passwordElement.sendKeys("123");
查看更多
时光不老,我们不散
3楼-- · 2020-07-03 05:05

I had the same problem and then figured out the cause. I was trying to type in a span tag instead of an input tag. My XPath was written with a span tag, which was a wrong thing to do. I reviewed the Html for the element and found the problem. All I then did was to find the input tag which happens to be a child element. You can only type in an input field if your XPath is created with an input tagname

查看更多
我想做一个坏孩纸
4楼-- · 2020-07-03 05:20

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.

查看更多
萌系小妹纸
5楼-- · 2020-07-03 05:20

you may also try full xpath, I had a similar issue where I had to click on an element which has a property javascript onclick function. the full xpath method worked and no interactable exception was thrown.

查看更多
登录 后发表回答