微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Selenium WebDriver在线程“main”中抛出异常org.openqa.selenium.ElementNotInteractableException

测试场景:尝试捕获并测试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();

原文地址:https://codeday.me/bug/20190527/1163818.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐