I would like to traverse through all th elements on my page:

The path to the th elements is div[id='specs-list']/table/tbody/tr/th:
My script is:
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
build();
driver.get('http://www.gsmarena.com');
driver.findElement(webdriver.By.name('sName')).sendKeys('iphone 4s');
driver.findElement(webdriver.By.id('quick-search-button')).click();
driver.findElement(webdriver.By.xpath("//div[@id='specs-list']/table/tbody/tr/th")).then(function(elem){
console.log(elem.getText());
});
But I get:
drobazko@drobazko:~/www$ node first_test.js
{ then: [Function: then],
cancel: [Function: cancel],
isPending: [Function: isPending] }
Instead text General
Questions are:
1. How can I get correct text string?
2. How Can I traverse through many th elements?
1 - How can I get correct text string?
driver.findElement(webdriver.By.xpath("//div[@id='specs-list']/table/tbody/tr/th")).getText().then(function(textValue){
console.log(textValue);
});
Or
driver.findElement(webdriver.By.xpath("//div[@id='specs-list']/table/tbody/tr/th")).then(function(elem){
elem.getText().then(function(textValue) {
console.log(textValue);
});
});
Why?
Both findElement and getText() return you a promise. You would get similar result if you try to console.log(driver.findElement(....))
2 - How Can I traverse through many th elements?
driver.findElements(webdriver.By.xpath("//div[@id='specs-list']/table/tbody/tr/th")).then(function(elems){
elems.forEach(function (elem) {
elem.getText().then(function(textValue){
console.log(textValue);
});
});
});
Sorry for asking my question here. I really need the answer. How can we use that textValue out of the function. I need to use the textValue to compare it with another data.