【问题标题】:How to click on button with no text in Protractor ?如何单击量角器中没有文字的按钮?
【发布时间】:2016-07-21 06:22:48
【问题描述】:

I want to Click on the two Plus(+) button but the button is not having any text in Music Library Section

如何点击相同?

我尝试了以下代码:

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

不幸的是,它不起作用。 请帮忙

【问题讨论】:

  • 通过说“它不工作”,您的意思是没有触发任何操作或控制台中有一些错误?您确定在单击之前已找到您的按钮吗?
  • 我已提供网站链接。你可以试试告诉我。我无法找到按钮。 @tratto

标签: javascript angularjs selenium-webdriver jasmine protractor


【解决方案1】:

您无法单击它们,因为它们具有相同的类和属性。 在这些情况下,您有 2 个选择:

1) 可以直接使用nth child CSS 的概念。

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)您可以使用上述答案中建议的@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
});

我测试了它,它们都可以工作!如果您遇到问题,请告诉我!

【讨论】:

  • Failures: 1) angularjs主页遇到声明异常消息:TypeError: element(...).get is not a function Stack: TypeError: element(...).get is not a function在 Suite. (D:\Protractor\git.js:3:32) 在 Object. (D:\Protractor\git.js:1:1) 在 Module._compile (module.js:413 :34) 在 Object.Module._extensions..js (module.js:422:10) 在 Module.load (module.js:357:32) 在 Function.Module._load (module.js:314:12) 在Module.require (module.js:367:17)
  • 1 个规范,1 个失败 0.03 秒内完成 C:\Users\kipatel\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\pro mise.js:1329 return callbackFn (this.value_); ^ 错误:在页面angular.github.io/peepcode-tunes/public 上找不到 Angular:Angular 从未在 处提供 resumeBootstrap
  • 我错过了.all 选项,我更新了我的答案。你试过nth child吗?
  • C:\Users\kipatel\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\pr mise.js:1329 return callbackFn(this.value_); ^ 错误:在页面angular.github.io/peepcode-tunes/public 上找不到 Angular:ang lar 从未在 C:\Users\kipatel\AppData\Roaming\npm\node_modules\protractor\built\protractor.js:679:17 提供 resumeBootstrap这..我做错了什么?你能测试一下并告诉我吗?
  • 你用的是哪个量角器版本和jasmine框架?
【解决方案2】:

最好使用 protractor.ExpectedConditions 等到元素变为可点击状态后再进行点击操作。

代码片段:

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();
});
});

【讨论】:

  • 不工作@Suresh
  • 定位器可能不正确,分享您与该按钮相关的 HTML 代码。如果不起作用,请附上您的错误消息,这将有助于我们预测可能导致问题的原因
  • 我已附上网站。你能用检查元素看看吗?
  • 试过了。不工作。你能运行它并告诉我吗?
  • @KishanPatel,似乎不是 angularjs 应用程序。所以我把 browser.ignoreSynchronization=true;,现在它可以工作了。对我来说,它有效
【解决方案3】:

我已经在 java 中检查了这个,虽然你没有在这里标记 java。希望你能把你的代码转换成js。

在java中,点击第一个+,使用下面的:

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

第二个+:

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

所以,我想如果你使用下面的 js 代码,这将起作用:

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

如果在js中有什么方法可以取元素,先取元素再点击使用元素 索引。

【讨论】:

  • noor ,过去 1 年我一直在研究 selenium。我在硒中做了同样的事情。但我想要它在量角器中。 element(by.css('.queue.add')).click();不工作,我之前试过了。
【解决方案4】:

检查Protractor API API。确保您拥有最新版本。您可以通过多种方式获取加号。一种方法是使用 webdriver.By.css

试试:

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

您还可以使用 webdriver.By.id 将 ID 添加到加号并以这种方式获取它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-03
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    相关资源
    最近更新 更多