首页 / JAVA / java – Selenium WebDriver在线程“main”中抛出异常org.openqa.selenium.ElementNotInteractableException
java – Selenium WebDriver在线程“main”中抛出异常org.openqa.selenium.ElementNotInteractableException
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – Selenium WebDriver在线程“main”中抛出异常org.openqa.selenium.ElementNotInteractableException,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3053字,纯文字阅读大概需要5分钟。
内容图文
![java – Selenium WebDriver在线程“main”中抛出异常org.openqa.selenium.ElementNotInteractableException](/upload/InfoBanner/zyjiaocheng/820/83a3616213de426c8764102a15297414.jpg)
测试场景:尝试捕获并测试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的原因可能很多.
>在我们感兴趣的Web元素上临时覆盖其他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,我们可以使用ExpectedCondition作为elementToBeClickable,如下所示:
WebDriverWait wait1 = new WebDriverWait(driver, 10);
WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_of_element_to_be_clicked")));
element1.click();
>永久覆盖我们感兴趣的Web元素上的其他WebElement:
如果在这种情况下覆盖是永久覆盖,我们必须将WebDriver实例强制转换为JavascriptExecutor并执行单击操作,如下所示:
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();
内容总结
以上是互联网集市为您收集整理的java – Selenium WebDriver在线程“main”中抛出异常org.openqa.selenium.ElementNotInteractableException全部内容,希望文章能够帮你解决java – Selenium WebDriver在线程“main”中抛出异常org.openqa.selenium.ElementNotInteractableException所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。