1

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

enter image description here

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 Answer 1

6

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);
        });
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.