【问题标题】:WebDriver Waits not working on FindElementWebDriver 等待在 FindElement 上不起作用
【发布时间】:2017-10-20 01:19:03
【问题描述】:

WebDriver 不等待在我的 Web 应用程序中启用或显示元素。 Web 应用程序广泛使用 AJAX/Javascript 来加载一个非常大的动态应用程序,特别是 Bubble 的 Web 应用程序编辑器 (https://bubble.is)。我正在使用气泡开发应用程序,并希望在气泡编辑器中使用 Selenium/WebDriver。

在使用隐式等待、显式等待和流畅等待时,尝试单击元素总是会返回以下错误:

org.openqa.selenium.WebDriverException: unknown error: Element <div class="tab tabs-3">...</div> is not clickable at point (30, 185). Other element would receive the click: <div class="status-notification" style="display: block;">...</div>

此问题似乎与在 DOM 中出现的元素完全加载之前有关。以下 Java 代码是这里使用的,在 Java 9 上的 Eclipse IDE 中编译和运行:

  public static void main(String[] args) {
      try {
        WebDriver chrome = bubbleLogin(false); // login to application via static page. this step works
        WebDriver driver = chrome;
    //  load app 
        WebDriverWait wait = new WebDriverWait(driver, DEFAULT_TIMEOUT_IN_SECONDS);


        driver.get("https://bubble.is/page?type=page&name=index&id=test123456code&tab=tabs-1");
        switchToWindowWithTitle("test123456", driver);

//      EXPLICIT WAIT: wait for "elementToBeClickable" to be true - element must be displayed and enabled
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.tab.tabs-3")));
//      FLUENT WAIT: wait for element.IsEnabled to be true - element must be displayed and enabled
// instantiate an instance of this class to run the wait function I wrote
        testng test = new testng(); 
        test.waitUntilElementExistsAndIsEnabled(driver,By.cssSelector("div.tab.tabs-3"));

//      only works via sleep... not implicit, explicit or fluent waits

//      Thread.sleep(10000);
        driver.findElement(By.cssSelector("div.tab.tabs-3")).click();

    } catch (Exception e) {
        System.out.println("failed run");
        e.printStackTrace();
    }
  }
}

以及上面的 FluentWait 方法:

private void waitUntilElementExistsAndIsEnabled(WebDriver driver, final By by) {
       new FluentWait<WebDriver>(driver).withTimeout(DEFAULT_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS)
          .pollingEvery(DEFAULT_SLEEP_TIME_IN_SECONDS, TimeUnit.SECONDS).ignoring(NoSuchElementException.class)
          .until(new ExpectedCondition<Boolean>() {
             public Boolean apply(WebDriver wd) {
                return wd.findElement(by).isEnabled(); 
             }
          });
    }

以下是来自 Eclipse 的错误消息的完整转储: (会话信息:chrome=61.0.3163.100)

(Driver info: chromedriver=2.32.498537 (cb2f855cbc7b82e20387eaf9a43f6b99b6105061),platform=Mac OS X 10.12.1 x86_64) (WARNING: The server did not provide any stacktrace information)

Command duration or timeout: 0 milliseconds

Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T16:15:26.402Z'

System info: host: 'Krystals-MacBook-Air.local', ip: 'fe80:0:0:0:c33:4430:fcfd:933c%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.12.1', java.version: '9'

Driver info: org.openqa.selenium.chrome.ChromeDriver

Capabilities [{mobileEmulationEnabled=false, hasTouchScreen=false, platform=MAC, acceptSslCerts=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, platformName=MAC, setWindowRect=true, unexpectedAlertBehaviour=, applicationCacheEnabled=false, rotatable=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.32.498537 (cb2f855cbc7b82e20387eaf9a43f6b99b6105061), userDataDir=/var/folders/gr/lggvp4hn09x2zqg6k561y4fr0000gn/T/.org.chromium.Chromium.haqAAG}, takesHeapSnapshot=true, pageLoadStrategy=normal, unhandledPromptBehavior=, databaseEnabled=false, handlesAlerts=true, version=61.0.3163.100, browserConnectionEnabled=false, nativeEvents=true, locationContextEnabled=true, cssSelectorsEnabled=true}]

Session ID: 0c9110334bb1b20a306363006f4bb868

at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:82)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:45)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:586)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:279)
at             org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:83)
at testng.main(testng.java:497)

【问题讨论】:

标签: java selenium selenium-webdriver async-await


【解决方案1】:

我用过

  1. thread.sleep 在我点击元素之前,然后
  2. 我会在点击主元素之前找到父元素

(new WebDriverWait(driver, 10)).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Reset")));
boolean Sort = driver.findElement(By.linkText("Reset")).isDisplayed();
if (Sort == true){
}else{
}

希望它会起作用

【讨论】:

  • 谢谢!如果可能,我想避免使用 thread.sleep(X),因为等待某些条件为真的动态等待会更有效率。显式等待看起来与我一直在尝试的类似 - 如果使用 Actions 函数没有帮助,我将首先尝试找到父元素。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-09
  • 2023-03-05
  • 1970-01-01
  • 2013-11-15
  • 2018-09-07
  • 1970-01-01
相关资源
最近更新 更多