最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Clicking links with WebdriverIO - Stack Overflow

matteradmin8PV0评论

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")
Post a comment

comment list (0)

  1. No comments so far