Java Selenium Web 自动化中的元素不可交互异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45183797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:52:23  来源:igfitidea点击:

element not interactable exception in selenium web automation

javaseleniumautomationtestngwebautomation

提问by ragesh-ragav 1993

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

采纳答案by anshul Gupta

Try setting an implicit wait of maybe 10 seconds.

尝试设置可能 10 秒的隐式等待。

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)

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

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.

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

回答by Rostech

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

回答by Testilla

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

我遇到了同样的问题,然后找出了原因。我试图输入跨度标签而不是输入标签。我的 XPath 是用 span 标记编写的,这是错误的做法。我查看了该元素的 Html 并发现了问题。然后我所做的就是找到恰好是一个子元素的输入标签。如果您的 XPath 是使用输入标记名创建的,则您只能在输入字段中输入

回答by Sachin Singh

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.

您也可以尝试完整的 xpath,我遇到了类似的问题,我必须单击具有属性 javascript onclick 函数的元素。完整的 xpath 方法有效并且没有抛出可交互的异常。