selenium web 自动化中的元素不可交互异常

新手上路,请多包涵

在下面的代码中,我无法在密码字段中发送密码密钥,我尝试单击该字段,清除该字段并发送密钥。但是现在正在使用任何方法。但是如果我调试和测试它就可以工作

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

原文由 ragesh-ragav 1993 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 611
2 个回答

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

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

或设置显式等待。显式等待是您定义的代码,用于等待特定条件发生,然后再继续执行代码。在您的情况下,它是密码输入字段的可见性。 (感谢 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");

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

原文由 anshul Gupta 发布,翻译遵循 CC BY-SA 3.0 许可协议

“元素不可交互” 错误可能意味着两件事:

一种。 元素未正确呈现:

解决方案就是使用隐式/显式等待

  • 隐式等待:

driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

  • 显式等待:

WebDriverWait wait=new WebDriverWait(driver, 20); element1 = wait.until(ExpectedConditions.elementToBeClickable(By.className(“fa-stack-1x”)));

b. 元素已呈现但不在屏幕的可见部分:

解决方案只是滚动到元素。根据 Selenium 的版本,它可以用不同的方式处理,但我将提供一个适用于所有版本的解决方案:

     JavascriptExecutor executor = (JavascriptExecutor) driver;
    executor.executeScript("arguments[0].scrollIntoView(true);", element1);

  1. 假设所有这些都失败了,那么另一种方法是再次使用 Javascript 执行器,如下所示:

    executor.executeScript(“arguments[0].click();”, element1);

  2. 如果您仍然无法点击 ,那么这可能又意味着两件事:

1. 内嵌框架

检查 DOM 以查看您正在检查的元素是否存在于任何框架中。如果是这样,那么您需要在尝试任何操作之前切换到此框架。

     driver.switchTo().frame("a077aa5e"); //switching the frame by ID
    System.out.println("********We are switching to the iframe*******");
    driver.findElement(By.xpath("html/body/a/img")).click();

2.新标签

如果打开了一个新选项卡并且该元素存在于该选项卡上,那么您再次需要像下面这样编写代码以在尝试操作之前切换到它。

 String parent = driver.getWindowHandle();
driver.findElement(By.partialLinkText("Continue")).click();
Set<String> s = driver.getWindowHandles();
// Now iterate using Iterator
Iterator<String> I1 = s.iterator();
while (I1.hasNext()) {
String child_window = I1.next();
if (!parent.equals(child_window)) {
    driver.switchTo().window(child_window);
    element1.click()
}

原文由 user3251882 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题