How to click a link using link text in nightwatch.js

旧时模样 提交于 2019-12-04 03:48:01

The locator By.linkText uses an XPath internally.

So to click the second link from your example with an XPath :

.useXpath()     // every selector now must be XPath
.click("//a[text()='Two']")
.useCss()      // we're back to CSS now

Note that depending on the inner HTML, you may need to concatenate the children and trim the spaces:

.click("//a[normalize-space()='Some link']")

The first param to elements() method is the locator strategy, use link text - it is supported:

client
  .url('http://website.org')
  .waitForElementVisible('body', 1000)
  .elements('link text', 'Two', function (result) {

    for (var i = 0; i < result.value.length; i++) {
      var element = result.value[i];

      // do something
    }
  })
  .end();

The only way that worked for me was using:

.useXpath()
.click("//*[contains(text(), 'Two')]")
.useCss()      

There is a better way now, you can use any of the documented locator strategies. Specifically id, css selector, link text, partial link text, tag name, xpath.)

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