WebDriver等待FindElement无法正常工作

时间:2017-10-17 18:51:49

标签: java selenium selenium-webdriver async-await

WebDriver不会等待在我的Web应用程序中启用或显示元素。 Web应用程序广泛使用AJAX / Javascript来加载非常大的动态应用程序,特别是Bubble的Web应用程序编辑器(https://bubble.is)。我正在使用bubble开发应用程序,并希望在Bubble编辑器中使用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完全加载之前出现在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)

1 个答案:

答案 0 :(得分:-1)

我用过

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

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

    希望它能起作用