JS Selenium findElements无法解决

时间:2017-09-18 10:30:07

标签: javascript selenium promise webdriver

我想知道为什么这段代码永远不会解决成功或失败。

driver.findElements(By.id(eid)).then(
      function (ret) {
        console.log("Success");
        return !!ret.length;
      }, function (err) {
        console.log("Failure");
      }
);

https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/webdriver_exports_WebDriver.html#findElements

https://github.com/SeleniumHQ/selenium/blob/master/javascript/node/selenium-webdriver/lib/webdriver.js#L1049

2 个答案:

答案 0 :(得分:0)

我在某处读到你不应该依赖回调而你应该使用承诺。

所以在我的情况下,它看起来像:

let ret = driver.findElements(By.id(eid));
console.log("Success");
return !!ret.length;

你觉得怎么样?

答案 1 :(得分:0)

我在我的chrome上运行下面的代码,它找到了。我可以找到h2计数:7'。 我安装了selenium-webdriver@3.5.0

DataResponse responseNiDatabase = new Gson().fromJson(JSON_STRING, DataResponse.class);

    List<Result> markersFromDatabase = new ArrayList<>();

   if (responseNiDatabase != null){

       markersFromDatabase = responseNiDatabase.getResult();
       SugarRecord.saveInTx(markersFromDatabase);
   }
   else{
       Toast.makeText(this, "An error occured, fetching from database", Toast.LENGTH_SHORT).show();

       markersFromDatabase = Select.from(Result.class).list();
   }

    if (!markersFromDatabase.isEmpty()){
        for (Result resultFromResponse : markersFromDatabase){
            mMap.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(resultFromResponse.getLatitude()),
                    Double.parseDouble(resultFromResponse.getLongitude())))
                    .title(resultFromResponse.getShopName())
                    .snippet(resultFromResponse.getShopContact())
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)));
        }
    }
    else{
        Toast.makeText(this, "No markers available", Toast.LENGTH_SHORT).show();
    }