Harish Naidu
Harish Naidu

Reputation: 35

How can i get xpath count in selenium webdriver using jasmine javascript?

var sample1 = browser.findElements(driver.By.xpath('//somenode')).getXpathCount();
console.log( sample1.intValue() );

while printing the count I am getting error:

error occuredTypeError: undefined is not a function

Upvotes: 1

Views: 1525

Answers (3)

Alexei Barantsev
Alexei Barantsev

Reputation: 534

findElements method returns an Array promise, so you have to do something like this:

browser.findElements(driver.By.xpath('//somenode')).then(function(elements) {
  var count = elements.length;
  ...
})

Upvotes: 0

Justin
Justin

Reputation: 11

Like @alecxe stated, the syntax for getXpathCount() is browser.getXpathCount("//somenode").

I saw you opened an issue on the selenium git and had more code there. What isn't showing here is you have just the following.

var browser = require('selenium-webdriver');
var sample1 = browser.findElements(driver.By.xpath('//somenode')).getXpathCount();
console.log( sample1.intValue() );

I haven't used WebDriverJs, so someone please correct me if I am wrong, but I think you need to create a browser object. Right now you only have created a driver object named browser.

Can you try the following snippet?

var webdriver = require('selenium-webdriver');
var browser = new webdriver.Builder().usingServer().withCapabilities({'browserName': 'chrome' }).build();

browser.get('http://en.wikipedia.org/wiki/Wiki');
browser.getXpathCount('//*[@id="www-wikipedia-org"]/div[1]/div');

Upvotes: 1

alecxe
alecxe

Reputation: 474141

I think you are not using getXpathCount() correctly. You should do it this way:

browser.getXpathCount("//somenode");

Upvotes: 0

Related Questions