简体   繁体   English

如何在量角器中单击没有文本的按钮?

[英]How to click on button with no text in Protractor ?

I want to Click on the two Plus(+) button but the button is not having any text in Music Library Section 我想单击两个加号(+)按钮,但是该按钮在“音乐库”部分中没有任何文本

How to Click the same? 如何单击相同?

I tried the following code: 我尝试了以下代码:

describe ('angularjs homepage', function() {
    browser.get('http://angular.github.io/peepcode-tunes/public/');
    element(by.css('[ng-click="player.playlist.add(album)"]')).click();
});

Unfortunately It is not working. 不幸的是,它不起作用。 Please Help 请帮忙

You are not able to click them because both of them have same class & attributes. 您无法单击它们,因为它们都具有相同的类和属性。 In these cases you have 2 options: 在这些情况下,您有2个选择:

1) You can directly use nth child concept of CSS. 1)您可以直接使用CSS的nth child概念。

describe ('angularjs homepage', function() {
browser.get('http://angular.github.io/peepcode-tunes/public/');
element(by.css('#container > section > ul > li:nth-child(1) > button')).click();  // it will click the first "+" button
element(by.css('#container > section > ul > li:nth-child(2) > button')).click();  // it will click the second "+" button
});

2) You can access them with index as @noor suggested in the above answer: 2)您可以使用上述答案中建议的索引作为@noor来访问它们:

describe ('angularjs homepage', function() {
browser.get('http://angular.github.io/peepcode-tunes/public/');
element.all(by.css('.queue.add')).get(0).click();  // it will click the first "+" button
element.all(by.css('.queue.add')).get(1).click();  // it will click the second "+" button
});

I tested it and both of them work! 我对其进行了测试,并且它们都可以工作! let me know if you face issues! 让我知道您是否遇到问题!

Better use protractor.ExpectedConditions to wait until element become clickable status and then do click operation. 最好使用protractor.ExpectedConditions等到元素变为可点击状态,然后再执行点击操作。

Code Snippet: 代码段:

EC = protractor.ExpectedConditions;

describe ('angularjs homepage', function() {

it('test case name',function(){

browser.ignoreSynchronization=true;

var twoButtons=element.all(by.css('.queue.add'));
browser.get('http://angular.github.io/peepcode-tunes/public/');

/*It will click first button, chance get(1) for second button*/    
browser.wait(EC.elementToBeClickable(twoButtons.get(0)),  
                                          8000).thenCatch(function () {
                      assert.fail(' element is not click able');
              });
twoButtons.get(0).click();
});
});

i have checked this in java, though u don't tag java here. 我已经在Java中对此进行了检查,尽管您未在此处标记Java。 hope u can convert ur code to js. 希望您可以将您的代码转换为js。

in java,to click on first +, use the below: 在Java中,要先单击+,请使用以下命令:

driver.findElements(By.cssSelector(".queue.add")).get(0).click();

for second +: 第二秒:

driver.findElements(By.cssSelector(".queue.add")).get(1).click();

so, i think if u use the below code for js, this will work: 因此,我认为如果您对js使用以下代码,则可以使用:

 element(by.css('.queue.add')).click();

if there is any way in js to take elements, first take elements and than click using element index. 如果js中有任何获取元素的方法,则首先获取元素,然后单击使用元素索引。

Check the Protractor API API. 检查量角器API API。 Make sure you have the latest version. 确保您具有最新版本。 You can grab the plus signs multiple ways. 您可以通过多种方式获取加号。 One way would be to use webdriver.By.css 一种方法是使用webdriver.By.css

Try : 尝试:

browser.findElement(by.css('.queue')).click(); 

You can also add an ID to the plus sign and grab it that way, using webdriver.By.id 您还可以使用webdriver.By.id将ID添加到加号并以这种方式获取它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM