Selenium WebDriver在线程“main”中抛出exceptionorg.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
是W3Cexception,抛出该exception表示虽然DOM TREE
上存在一个元素,但它不处于可以与之交互的状态。
原因和解决方案:
发生ElementNotInteractableException
的原因可能很多。
-
在我们感兴趣的
WebElement
WebElement
上临时叠加其他WebElement
:在这种情况下,直接解决方案是将
ExplicitWait
即WebDriverWait
与ExpectedCondition
结合使用,因为invisibilityOfElementLocated
为folllows: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();
-
通过我们感兴趣的
WebElement
永久覆盖其他WebElement
:如果在这种情况下覆盖是永久覆盖,我们必须将
WebDriver
实例WebDriver
为JavascriptExecutor
并执行单击操作,如下所示:WebElement ele = driver.findElement(By.xpath("element_xpath")); JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("arguments[0].click();", ele);
现在解决这个特定上下文中的错误ElementNotInteractableException
,我们需要添加ExplicitWait
即WebDriverWait
,如下所示:
您需要引导一些wait
Password field
在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();