如何在网站中调用javascript函数



这是一个网站元素

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

假设我不想点击它,而是想运行"logout()"来自selenium的脚本,这可能吗?如果有,那又如何?

这是我尝试过的

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

但是我得到这个错误:

javascript error: logout is not defined

Eg page:

<!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函数,所以你直接调用它。

调用并打开浏览器控制台,您可以看到"打印

有很多方法可以在angular模块之外暴露angular函数,但不是直接的

Angular2 -如何从应用外部调用组件函数

最新更新