【问题标题】:org.openqa.selenium.ElementNotInteractableException: Element <a class="bg-inverse "> could not be scrolled into view when trying to click a buttonorg.openqa.selenium.ElementNotInteractableException:尝试单击按钮时,元素 <a class="bg-inverse "> 无法滚动到视图中
【发布时间】:2018-12-04 03:04:02
【问题描述】:

我正在使用 FF 60.0 的壁虎驱动程序 selenium java。以前我的代码工作正常,但突然之间,现在每次运行它时,当我试图点击一个按钮时,它都会给我一个错误could not be scrolled into view。 下面是我的代码,我尝试使用 Thread.sleep(5000) 或隐式等待,但没有任何效果。我被困在这里。

public void goToWorkerSummary() throws InterruptedException {
    WebElement btnWorkerSummary = driver.findElement(By.xpath("//a[@href='/admin/worker-summary']"));
    //Thread.sleep(5000);//wait.until(ExpectedConditions.visibilityOf(btnWorkerSummary).click();
    btnWorkerSummary.click();
}

到目前为止,代码可以正常工作,但是一旦到达此处,就会显示上述错误。 下面是错误sn-p。

Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: Element <a class="bg-inverse text-white dropdown-item" href="/admin/worker-summary"> could not be scrolled into view
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T15:15:03.216Z'
System info: host: 'CPU-38', ip: '192.168.0.55', os.name: 'Windows 8', os.arch: 'x86', os.version: '6.2', java.version: '1.8.0_51'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 60.0.2, javascriptEnabled: true, moz:accessibilityChecks: false, moz:headless: false, moz:processID: 2480, moz:profile: C:\Users\xyz\AppData\Lo..., moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: XP, platformName: XP, platformVersion: 6.2, rotatable: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}
Session ID: 08c08933-06f6-480c-88c9-9d7ab718c2c8
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:276)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:83)

【问题讨论】:

    标签: selenium firefox selenium-webdriver webdriver geckodriver


    【解决方案1】:

    此错误消息...

    Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: Element <a class="bg-inverse text-white dropdown-item" href="/admin/worker-summary"> could not be scrolled into view
    

    ...暗示 GeckoDriver / FirefoxDriver 无法与所需元素交互。

    解决方案

    一旦你找到元素 btnWorkerSummary 向前移动,因为你调用 click() 而不是 ExpectedConditions 作为 visibilityOf 你需要使用elementToBeClickable()如下:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@href='/admin/worker-summary']"))).click();
    

    但是,另一个问题是您使用的二进制文件版本之间的不兼容,如下所示:

    • 您的 Selenium 客户端 版本是 3.12.0
    • 您的 JDK 版本是 1.8.0_51,它是古老的

    解决方案

    • JDK升级到最新级别JDK 8u201
    • 执行您的@Test

    【讨论】:

    • 虽然,我更新了 JDK,感谢您的建议,但我发现问题出在 xpath 上,更新了我的 xpath 并通过使用 elementToBeClickable() 修复了它
    【解决方案2】:

    尝试使用javascriptexecutor,如下所示的示例代码,

    JavascriptExecutor je = (JavascriptExecutor) driver;
    je.executeScript("arguments[0].scrollIntoView(true);",driver.findElement(By.xpath("//a[@href='/admin/worker-summary']")));
    

    【讨论】:

      【解决方案3】:

      您可以简单地尝试跟随js滚动页面。根据您的要求输入像素滚动页面。这里我使用 3000 Px 将页面滚动到中间

         JavascriptExecutor js = (JavascriptExecutor) driver;
          js.executeScript("window.scrollBy(0,3000)");

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-09
        • 2018-04-02
        • 2017-10-21
        • 1970-01-01
        • 2018-12-17
        • 2017-01-16
        • 1970-01-01
        相关资源
        最近更新 更多