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

标签 java selenium selenium-webdriver webdriver webdriverwait

测试场景: try catch 和测试 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");

最佳答案

ElementNotInteractableException

根据文档,ElementNotInteractableException 是 W3C 异常,它被抛出以指示尽管元素存在于 DOM Tree 上, 不处于可以交互的状态。

原因及解决方案:

ElementNotInteractableException 发生的原因有很多。

  1. 将其他 WebElement 临时覆盖在我们感兴趣的 WebElement 上:

    在这种情况下,直接的解决方案是引入 ExplicitWait,即 WebDriverWait 结合 ExpectedCondition 作为 invisibilityOfElementLocated如下:

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

    更好的解决方案是更精细一些,而不是使用 ExpectedCondition 作为 invisibilityOfElementLocated 我们可以使用 ExpectedConditionelementToBeClickable 如下:

    WebDriverWait wait1 = new WebDriverWait(driver, 10);
    WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_of_element_to_be_clicked")));
    element1.click();
    
  2. 将其他 WebElement 永久覆盖在我们感兴趣的 WebElement 上:

    如果在这种情况下覆盖是永久性的,我们必须将 WebDriver 实例转换为 JavascriptExecutor 并按如下方式执行点击操作:

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

现在解决这个特定上下文中的错误 ElementNotInteractableException 我们需要添加 ExplicitWaitWebDriverWait 如下:

您需要进行一些等待,以便密码 字段在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();

关于java - Selenium WebDriver 在线程 "main"org.openqa.selenium.ElementNotInteractableException 中抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44690971/

相关文章:

java - 上下文切换的 native 线程无法附加到 JVM

javascript - 在 Selenium 中使用 Java Script Executor 时无法读取未定义的属性 'click'

java - 如何在测试自动化中处理 Firefox 浏览器确认消息?

java - 使用 Selenium WebDriver 和 TestNG 处理单点登录

java - 在 webelement 上获取 NoSuchElementException (找到相同的 webelement,但有时我得到异常)

java - 如何使用 Selenium 和 Java 手动输入 OTP 后继续自动化

java - 在整数、字符串的采访中使用的好散列函数?

java - nio 的一些麻烦

java - 如何检查文件是否存在?

javascript - Webdriver.io 因 NoSessionIdError 崩溃