Selenium WebDriver抛出异常,在线程"main"中 org.openqa.selenium.ElementNotInteractableException

5

测试场景: 尝试捕获并测试Gmail登录。

当前输出: Mozilla实例打开。用户名已经输入,但是WebDriver代码没有输入密码。

System.setProperty("webdriver.gecko.driver", "C:\\Users\\Ruchi\\workspace2\\SeleniumTest\\jar\\geckodriver-v0.17.0-win64\\geckodriver.exe");
FirefoxDriver  varDriver=new FirefoxDriver();

varDriver.get("http://gmail.com");  
WebElement webElem=  varDriver.findElement(By.id("identifierId"));
webElem.sendKeys("error59878@gmail.com");
WebElement nextButton=varDriver.findElement(By.id("identifierNext"));
nextButton.click();

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

WebElement wePass=varDriver.findElement(By.cssSelector(".rFrNMe.P7gl3b.sdJrJc.Tyc9J"));

wePass.sendKeys("test1");
1个回答

9

无法交互的元素异常

根据文档,无法交互的元素异常是W3C异常,用于指示虽然元素存在于DOM树中,但它不处于可交互状态。

原因和解决方法:

无法交互的元素异常发生的原因可能有很多。

  1. Temporary Overlay of other WebElement over the WebElement of our interest:

    In this case, the direct solution would have been to induce ExplicitWait i.e. WebDriverWait in combination with ExpectedCondition as invisibilityOfElementLocated as follows:

    WebDriverWait wait2 = new WebDriverWait(driver, 10);
    wait2.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("xpath_of_element_to_be_invisible")));
    driver.findElement(By.xpath("xpath_element_to_be_clicked")).click();
    

    A better solution will be to get a bit more granular and instead of using ExpectedCondition as invisibilityOfElementLocated we can use ExpectedCondition as elementToBeClickable as follows:

    WebDriverWait wait1 = new WebDriverWait(driver, 10);
    WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_of_element_to_be_clicked")));
    element1.click();
    
  2. Permanent Overlay of other WebElement over the WebElement of our interest :

    If the overlay is a permanent one in this case we have to cast the WebDriver instance as JavascriptExecutor and perform the click operation as follows:

    WebElement ele = driver.findElement(By.xpath("element_xpath"));
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].click();", ele);
    

现在解决在这种特定情况下出现的错误 ElementNotInteractableException,我们需要添加 ExplicitWait,即 WebDriverWait,如下所示:

您需要等待一段时间,使 密码 字段在 HTML DOM 中正确渲染。您可以考虑为其配置一个 ExplicitWait。以下是使用 Mozilla Firefox 登录到 Gmail 的工作代码:

System.setProperty("webdriver.gecko.driver","C:\\Users\\Ruchi\\workspace2\\SeleniumTest\\jar\\geckodriver-v0.17.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
String url = "https://accounts.google.com/signin";
driver.get(url);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
WebElement email_phone = driver.findElement(By.xpath("//input[@id='identifierId']"));
email_phone.sendKeys("error59878@gmail.com");
driver.findElement(By.id("identifierNext")).click();
WebElement password = driver.findElement(By.xpath("//input[@name='password']"));
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(password));
password.sendKeys("test1");
driver.findElement(By.id("passwordNext")).click();

1
非常感谢!!现在它正常工作了。如果我们使用类名,我注意到类名正在改变。 - ruchilalla

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接