量角器问题:W/element - 为定位器找到多个元素 By(css selector, button[class*='btn-info']) - 将使用第一个结果
Protractor Issue : W/element - more than one element found for locator By(css selector, button[class*='btn-info']) - the first result will be used
步骤 :
- 导航到 https://qaclickacademy.github.io/protocommerce/shop
- 点击商店
- Select 一些手机品牌并加入购物车
- 点击结帐按钮
- 提取每个手机的总值并打印
问题:我使用下面的定位器点击添加按钮
[ element.all(by.css("按钮[class*='btn-info']")).click() ]
但似乎所有的添加按钮都有相同的代码。
默认情况下,第一个添加按钮会被多次点击,并且会重复添加同一个手机。
谁能帮我解决这个问题?
我的量角器代码 :
describe('Assignment',function(){
function Shopping(MobileBrand){
//Use tag name locator when only tag name is present
element.all(by.tagName("app-card")).each(function(value){
//when single h4 and single a tag use them directly no need to give attribute='value'
value.element(by.css("h4 a")).getText().then(function(name){
if(name==MobileBrand)
{
console.log("Mobile Name Retrieved" +name);
console.log("Mobile Name Provided" +MobileBrand);
//Click on Add button
element.all(by.css("button[class*='btn-info']")).click().then(function(){
browser.sleep(5000);
});
}
})
})
}
it('Task',function(){
browser.driver.manage().window().maximize();
browser.get("https://qaclickacademy.github.io/protocommerce/");
element(by.linkText("Shop")).click();
//Pass the mobile brands to be selected
Shopping("Samsung Note 8");
Shopping("Nokia Edge");
Shopping("Blackberry");
element(by.partialLinkText("Checkout")).click().then(function(){
browser.sleep(5000);
})
});
})
您正在声明 elementArrayFinder
element.all(by.css("button[class*='btn-info']")) // <--- .all means many elements
这意味着,您希望页面有多个元素。所以你需要指示量角器你想使用哪一个。为此,您可以使用 .get() 方法
如果你这样做
element.all(by.css("button[class*='btn-info']")).get(0).click();
脚本将单击此定位器的第一个元素。同样,您可以指示单击第二个 (.get(1)
)、第三个 (.get(2)
) 元素等
另一种选择是声明一个函数,该函数将 return 按文本
的元素
let buyPhoneButton = function (name) {
return element(by.xpath("//a[text()=" + name + "]/ancestor::app-card//button"))
}
然后使用它
buyPhoneButton("iphone X").click()
如有帮助,请采纳
步骤 :
- 导航到 https://qaclickacademy.github.io/protocommerce/shop
- 点击商店
- Select 一些手机品牌并加入购物车
- 点击结帐按钮
- 提取每个手机的总值并打印
问题:我使用下面的定位器点击添加按钮 [ element.all(by.css("按钮[class*='btn-info']")).click() ]
但似乎所有的添加按钮都有相同的代码。 默认情况下,第一个添加按钮会被多次点击,并且会重复添加同一个手机。
谁能帮我解决这个问题?
我的量角器代码 :
describe('Assignment',function(){
function Shopping(MobileBrand){
//Use tag name locator when only tag name is present
element.all(by.tagName("app-card")).each(function(value){
//when single h4 and single a tag use them directly no need to give attribute='value'
value.element(by.css("h4 a")).getText().then(function(name){
if(name==MobileBrand)
{
console.log("Mobile Name Retrieved" +name);
console.log("Mobile Name Provided" +MobileBrand);
//Click on Add button
element.all(by.css("button[class*='btn-info']")).click().then(function(){
browser.sleep(5000);
});
}
})
})
}
it('Task',function(){
browser.driver.manage().window().maximize();
browser.get("https://qaclickacademy.github.io/protocommerce/");
element(by.linkText("Shop")).click();
//Pass the mobile brands to be selected
Shopping("Samsung Note 8");
Shopping("Nokia Edge");
Shopping("Blackberry");
element(by.partialLinkText("Checkout")).click().then(function(){
browser.sleep(5000);
})
});
})
您正在声明 elementArrayFinder
element.all(by.css("button[class*='btn-info']")) // <--- .all means many elements
这意味着,您希望页面有多个元素。所以你需要指示量角器你想使用哪一个。为此,您可以使用 .get() 方法
如果你这样做
element.all(by.css("button[class*='btn-info']")).get(0).click();
脚本将单击此定位器的第一个元素。同样,您可以指示单击第二个 (.get(1)
)、第三个 (.get(2)
) 元素等
另一种选择是声明一个函数,该函数将 return 按文本
的元素let buyPhoneButton = function (name) {
return element(by.xpath("//a[text()=" + name + "]/ancestor::app-card//button"))
}
然后使用它
buyPhoneButton("iphone X").click()
如有帮助,请采纳