Selenium Web 自动化中的元素不可交互异常 [英] element not interactable exception in selenium web automation

查看:31
本文介绍了Selenium Web 自动化中的元素不可交互异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我无法在密码字段中发送密码密钥,我尝试单击该字段,清除该字段并发送密钥.但现在在任何一种方法中工作.但如果我调试和测试它的工作

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");
 }

推荐答案

尝试将隐式等待设置为 10 秒.

Try setting an implicit wait of maybe 10 seconds.

gmail.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

或者设置显式等待.显式等待是您定义的代码,用于在进一步处理代码之前等待特定条件发生.在您的情况下,它是密码输入字段的可见性.(感谢 ainlolcat 的评论)

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");

说明:selenium 找不到元素的原因是因为密码输入字段的id 最初是Passwd-hidden.单击下一步"按钮后,Google 首先验证输入的电子邮件地址,然后显示密码输入字段(通过将 id 从 Passwd-hidden 更改为 Passwd).因此,当密码字段仍处于隐藏状态时(即 Google 仍在验证电子邮件 ID),您的网络驱动程序将开始搜索仍处于隐藏状态的 id Passwd 的密码输入字段.因此,抛出异常.

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.

这篇关于Selenium Web 自动化中的元素不可交互异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆