Clicking links with WebdriverIO

别说谁变了你拦得住时间么 提交于 2021-02-08 10:40:23

问题


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.com')
  .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!


回答1:


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.com')
  .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!




回答2:


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.com')
  .click('a')
  .end()
);

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

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


来源:https://stackoverflow.com/questions/34569341/clicking-links-with-webdriverio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!