【问题标题】:How can I call a javascript function in a website with selenium如何在带有 selenium 的网站中调用 javascript 函数
【发布时间】:2021-05-01 20:53:55
【问题描述】:

所以这是网站中的一个元素

<span class="log-out-ico" ng-click="logout()">/

假设我不想点击它,而是只想从 selenium 运行“logout()”脚本, 甚至可能吗?如果有怎么办?!

这是我尝试过的

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("logout()");

但我收到此错误:

javascript error: logout is not defined

【问题讨论】:

    标签: javascript c# angularjs selenium


    【解决方案1】:

    例如页面:

    <!DOCTYPE html>
    <html>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
    <body ng-app="myApp">
    
    <div ng-controller="myCtrl">
      <p>Click the button to run a function:</p>
      <button ng-click="myFunc()">OK</button>
      <p>The button has been clicked {{count}} times.</p>
    </div>
    
    <script>
    a=function(){
    console.log("hiii")
    }
    angular.module('myApp', [])
      .controller('myCtrl', ['$scope', function($scope) {
        $scope.count = 0;
        $scope.myFunc = function() {
          $scope.count++;
        };
      }]);
    </script>
    </body>
    </html>
    

    您可以看到 muFunc 不在脚本块内,而是在 Angular 控制器内。你不能直接调用它。

    但是函数'a'是普通的javascript函数,所以你直接调用它。

    调用a并打开浏览器控制台,可以看到“hiiii”打印出来了

    有一些方法可以在角度模块之外公开角度函数,但它不是直截了当

    Angular2 - how to call component function from outside the app

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      • 2017-09-01
      • 2022-10-25
      相关资源
      最近更新 更多