javascript - Clicking links with WebdriverIO - Stack Overflow

I have a web page that I am trying to test via Webdriver IO. My question is, how do I click a couple o

I have a web page that I am trying to test via Webdriver I/O. My question is, how do I click a couple of links via a test? Currently, I have the following:

var webdriverio = require('webdriverio');
var client = webdriverio.remote(settings).init()
  .url('')
  .elements('a')
  .then(function(links) {
    for (var i=0; i<links.value.length; i++) {
      console.log('Clicking link...');
      var link = links.value[i].ELEMENT;
      link.click().then(function(result) {
        console.log('Link clicked!');
      });
    }
  })
;

When the above gets executed, I get an error that says "click is not a function" on link. When I print link to the console, it looks like JSON, which would make sense since the documentation says that the elements function returns WebElement JSON objects. Still, I'm just trying to figure out how to click this link.

How does one do such?

Thanks!

I have a web page that I am trying to test via Webdriver I/O. My question is, how do I click a couple of links via a test? Currently, I have the following:

var webdriverio = require('webdriverio');
var client = webdriverio.remote(settings).init()
  .url('http://www.example.')
  .elements('a')
  .then(function(links) {
    for (var i=0; i<links.value.length; i++) {
      console.log('Clicking link...');
      var link = links.value[i].ELEMENT;
      link.click().then(function(result) {
        console.log('Link clicked!');
      });
    }
  })
;

When the above gets executed, I get an error that says "click is not a function" on link. When I print link to the console, it looks like JSON, which would make sense since the documentation says that the elements function returns WebElement JSON objects. Still, I'm just trying to figure out how to click this link.

How does one do such?

Thanks!

Share Improve this question edited Feb 13, 2016 at 20:36 Marty Aghajanyan 13.4k8 gold badges36 silver badges37 bronze badges asked Jan 2, 2016 at 18:36 JQuery MobileJQuery Mobile 6,30124 gold badges88 silver badges138 bronze badges 3
  • What about find your element by selector and click method? client.click('a', function(err,res) {...}) – Valijon Commented Jan 4, 2016 at 19:58
  • @Valijon That approach will only click the first link. It will not click each link. – JQuery Mobile Commented Jan 5, 2016 at 1:02
  • If you need to click couple links with "purpose", set class or id attributes for that links. Then, via selectors "#some_id", "a.some_class" click them. In our project, we use Selenium for Java, and we do click objectively, like login, logout, main page, second page, etc... – Valijon Commented Jan 5, 2016 at 9:35
Add a ment  | 

2 Answers 2

Reset to default 5 +100

You need elementIdClick http://webdriver.io/api/protocol/elementIdClick.html

Here is an example

var settings = {
  desiredCapabilities: {
    browserName: 'firefox',
  },
};

var webdriverio = require('webdriverio');
var client = webdriverio.remote(settings).init()
  .url('http://www.example.')
  .elements('a')
  .then(function(links) {
    for (var i=0; i<links.value.length; i++) {
      console.log('Clicking link...');
      var link = links.value[i].ELEMENT;
      client.elementIdClick(link).then(function(result) {
        console.log('Link clicked!');
      });
    }
  });

Result of the above code will be

Clicking link... Link clicked!

Hello there you could do directly this though: it clicks all elements a on the page

var client = webdriverio.remote(settings).init()
  .url('http://www.example.')
  .click('a')
  .end()
);

you could you a selector to target specific a elements example:

.click("article .search-result .abstract .more")

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745624872a4636747.html

相关推荐

  • javascript - Clicking links with WebdriverIO - Stack Overflow

    I have a web page that I am trying to test via Webdriver IO. My question is, how do I click a couple o

    8小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信