需要从arryelement中找到元素的计数,如果count>1需要单击
/*search name is clinic name
clinicsearch is the arrayelement which has the list of clinic name list from the table*/
public SearchAndSelectClinicA(SearchName:string){
this.ClinicSearch.sendKeys(SearchName);
this.SearchIcon.click(); //click on the icon to get the clinic names
this.SearchedClinic.count().then(function(count){
//get the count of elements which are there in the Searchedclinic element array
if(count>1){
this.SearchedClinic.each(function(rows){
/*Failed: Cannot read property 'SearchedClinic' of undefined
(Error gets displayed when it comes to above line.*/
rows.getText().then(function(value){
if(value.indexOf(SearchName)){
//Click the element if the statement is satisfied
rows.click();
}
});
});
}
});
}
请帮我解决这个问题
失败:无法读取未定义的属性'SearchedClinic‘。
正在显示。
它能够执行到if语句。稍后,它会抛出上面指定的error,并立即停止执行
发布于 2018-06-01 07:29:50
在JavaScript中,每个函数都有一个隐式的this
。在嵌套函数情况下使用this
时需要小心。
下图说明了这些this
来自哪个函数。
第二个this.SearchedClinic
将尝试访问属于嵌套函数this
:function(count){}
,该函数没有名为SearchedClinic
的属性。
您期望this
指向上面的那个。因此,您需要将顶部的this
赋给另一个变量,如下所示:
现在me
指向最上面的this
,你可以根据需要在任何嵌套函数中使用它。
发布于 2018-06-01 07:32:04
您的function(rows)
将创建一个新的闭包。这意味着该作用域内的this
不一定与外部的this
相同。
以下是几个解决方案
var self = this;
self.SearchedClinic`
this.SearchedClinic.count().then((count) => {});
.bind(this)
This.SearchedClinic.count()计数((function(.then) => { }).bind(this));
发布于 2018-06-02 08:02:18
假设有以下代码:
const result = obj1.printName.call(obj2);
在函数call
下使用this
将引用obj2
,因为call()的第一个参数是显式设置this
引用的内容。
这就是你所遇到的问题。
this.SearchedClinic.count().then(function(count){
//calling this will reference the parameter count
}
荣誉之间的任何this
都将引用该函数的第一个参数。您要做的是声明一个全局变量:var searchedClinic = this.SearchedClinic
。下面是更新后的代码:
public SearchAndSelectClinicA(SearchName:string){
var searchedClinic = this.SearchedClinic;
this.ClinicSearch.sendKeys(SearchName);
this.SearchIcon.click(); //click on the icon to get the clinic names
this.SearchedClinic.count().then(function(count){
//get the count of elements which are there in the Searchedclinic element array
if(count>1){
searchedClinic.each(function(rows){
rows.getText().then(function(value){
if(value.indexOf(SearchName)){
//Click the element if the statement is satisfied
rows.click();
}
});
});
}
});
}
https://stackoverflow.com/questions/50627570
复制相似问题