Selenium ElementNotInteractableException

时间:2017-07-11 23:54:04

标签: java selenium web click element

我试图在线自动化认证,但它阻止我点击链接进入表单页面。 我已经使用了其他人给出的其他解决方案,并且无法找到元素来获取org.openqa.selenium.ElementNotInteractableException:

这是我的代码片段

List<WebElement> linkList = driver.findElements(By.tagName("a"));
for(int i = 0; i < linkList.size(); i++){
    System.out.println(linkList.get(i).getAttribute("href"));
    if(linkList.get(i).getAttribute(("href")).contains("student")){
        linkList.get(i).click();
    }
}

基本上我使用循环来查找哪个tagName包含我需要的链接,但这里出现错误:

Jul 11, 2017 7:49:21 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C

org.openqa.selenium.ElementNotInteractableException: 
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'DESKTOP-35FGBMU', ip: '192.168.1.117', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=C:\Users\pizza\AppData\Local\Temp\rust_mozprofile.Heg8gMUCnERq, rotatable=false, timeouts={implicit=0.0, pageLoad=300000.0, script=30000.0}, pageLoadStrategy=normal, platform=ANY, specificationLevel=0.0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=54.0.1, platformVersion=10.0, moz:processID=7448.0, browserName=firefox, javascriptEnabled=true, platformName=windows_nt}]
Session ID: 322f496f-033f-4ccf-a0ab-324182e773b3

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:150)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:115)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:45)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:272)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:82)
    at com.Certify.startCertify(Certify.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

我也从未见过任何有过ElementNotInteractableException的人,但它的类似变体类似(我认为) &#34;元素目前无法互动,可能无法操纵&#34;

以下是相关的HTML:

<li class="list label"
    <a href="/desW/cp/search/searchLocation.do?method=view&amp;launchFrom=fromStudentAdminMenu&amp;clearSearchCriteria=true&amp;mainMenuOption=prompt.menu.student.manage&amp;mainMenuOptionUrl=url.menu.manage.my.student" title="Manage My Students">Manage My Students</a>
</li>

任何帮助将不胜感激

编辑:找到一种方法来使用

解决它
driver.navigate().to(String.valueOf(linkList.get(i).getAttribute("href")));

但我真的很想知道如何正确地做到这一点,因为我不认为这是方法。

1 个答案:

答案 0 :(得分:0)

假设1:有一些隐藏的链接。您必须通过选中isDisplayed()跳过它们。

List<WebElement> linkList = driver.findElements(By.tagName("a"));
for(int i = 0; i < linkList.size(); i++){
    System.out.println(linkList.get(i).getAttribute("href"));
    if(linkList.get(i).getAttribute(("href")).contains("student") && linkList.get(i).isDisplayed()){
        linkList.get(i).click();
    }
}

假设2:某些元素正在某些链接上呈现。你必须等到可以点击它们。

List<WebElement> linkList = driver.findElements(By.tagName("a"));
for(int i = 0; i < linkList.size(); i++){
    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    System.out.println(linkList.get(i).getAttribute("href"));
    if(linkList.get(i).getAttribute(("href")).contains("student")){
        try {
            wait.until(ExpectedConditions.elementToBeClickable(linkList.get(i));
            linkList.get(i).click();
        } catch (Exception e) {
            System.out.println("Unable to click on " + linkList.get(i).getAttribute("href") + ". Exception: " + e.printStackTrace());
        }
    }
}