如何在获取元素之前等待页面加载?

时间:2016-05-13 06:50:31

标签: java selenium

这不是重复,因为我已经阅读了与此有关的其他问题,但它没有给我我正在寻找的答案。

我在Java中使用Selenium进行UI自动化。

我的代码是这样的:

function CartForm($scope) {
  $scope.searchInfo = [];
  $scope.total = 0;
  $scope.invoice = {
    items: [{
      product_name: 'x',
      qty: 10,
      description: 'item',
      cost: 9.95
    }, {
      product_name: 'y',
      qty: 170,
      description: 'item',
      cost: 8
    }, {
      product_name: 'z',
      qty: 150,
      description: 'item',
      cost: 7
    }],
    selectedItem: []
  };

  $scope.addItem = function() {
    $scope.invoice.selectedItem.push({
      qty: null,
      description: null,
      cost: null,
      product: null,
      total: null
    });
  };

  $scope.removeItem = function(index) {
    $scope.invoice.selectedItem.splice(index, 1);
    $scope.totalFinel();
  };

  $scope.totalFinel = function() {

    //console.log('test');

    var total = 0;
    //  alert(total);
    $scope.invoice.selectedItem.forEach(function(item, index) {
        total += item.total;
        //alert(item.total);
      })
      // return total;
    $scope.total = total;
  };

  $scope.calculateTotal = function(selected, index) {
    $scope.invoice.selectedItem[index].description = selected.description;
    $scope.invoice.selectedItem[index].qty = selected.qty;
    $scope.invoice.selectedItem[index].cost = selected.cost;
    $scope.invoice.selectedItem[index].product = selected.product;
    $scope.invoice.selectedItem[index].total = selected.qty * selected.cost;
    $scope.totalFinel();
  };
}

这仍然会给我一个错误,因为它正在搜索仍然不存在的元素。

3 个答案:

答案 0 :(得分:2)

您也可以等到所需元素存在,可见,可点击,...

waitFor(ExpectedConditions.presenceOfElementLocated(By.id("yourMailInputId")));

答案 1 :(得分:0)

这是一个很长的镜头。但是尝试改变" equals" to" equalsIgnoreCase"。

final boolean response = js.executeScript("return document.readyState").equalsIgnoreCase("complete");

答案 2 :(得分:0)

WebDriver driver_;

public void waitForPageLoad() {

    Wait<WebDriver> wait = new WebDriverWait(driver_, 30);
    wait.until(new Function<WebDriver, Boolean>() {
        public Boolean apply(WebDriver driver) {
            System.out.println("Current Window State       : "
                + String.valueOf(((JavascriptExecutor) driver).executeScript("return document.readyState")));
            return String
                .valueOf(((JavascriptExecutor) driver).executeScript("return document.readyState"))
                .equals("complete");
        }
    });
}