【问题标题】:I getException in thread "main" org.openqa.selenium.ElementNotInteractableException我在线程“主”org.openqa.selenium.ElementNotInteractableException 中出现异常
【发布时间】:2017-12-26 23:49:04
【问题描述】:

代码:

WebElement betting = driver.findElement(By.id("flex-menu"));
        List<WebElement> hallo = betting.findElements(By.xpath("//*[@id='flex-menu']//ul//li//a"));

            System.out.println(hallo.get(0).getText());

            hallo.get(0).click();

错误按摩:

线程“main”中的异常 org.openqa.selenium.ElementNotInteractableException:

会话 ID:4bfaaf77-6275-4ffc-a8d7-b24b70f3acca 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 在 java.lang.reflect.Constructor.newInstance(Constructor.java:422) 在 org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187) 在 org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122) 在 org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49) 在 org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164) 在 org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83) 在 org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601) 在 org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:279) 在 org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:83) 在 Aufgabe1.Main.main(Main.java:41)

该守则有什么问题?我不能点击,但它找到了元素。

【问题讨论】:

标签: java selenium webdriver


【解决方案1】:

这可能是因为两个原因。

1) 您的按钮元素可见但不可点击。如果是这种情况,请使用等待条件。

WebDriverWait myWaitVar = new WebDriverWait(driver,20);
WebElement el = myWaitVar.until(ExpectedConditions.elementToBeClickable(betting.findElements(By.xpath("//*[@id='flex-menu']//ul//li//a")).get(0)));
el.click();

2) 如果元素被其他元素隐藏/重叠,请使用 JavascriptExecutor

 WebDriverWait myWaitVar = new WebDriverWait(driver,20);
    WebElement el = myWaitVar.until(ExpectedConditions.elementToBeClickable(betting.findElements(By.xpath("//*[@id='flex-menu']//ul//li//a")).get(0)));
((JavascriptExecutor)driver).executeScript("arguments[0].click()", el); 

更多信息见 In Selenium Webdriver, ExpectedCondition.elementToBeClickable is not waiting until the progress bar disappears

【讨论】:

    【解决方案2】:

    这可能是因为您正在尝试的元素被隐藏/需要隐式等待条件。尝试使用 .enable() 函数实现隐式-显式等待条件,然后尝试使用 .click() 函数。

    【讨论】:

      猜你喜欢
      • 2017-11-25
      • 2021-03-10
      • 1970-01-01
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      • 2013-12-09
      • 2016-09-12
      相关资源
      最近更新 更多