1

When I do.

I receive as the screenshot attached.

But I need one array having each array as a whole number.

'use strict'
const { helper, browserstackBase } = require('@core/IQAir')
const webdriver = require('selenium-webdriver')
const { initializeBrowser } = require('@core/IQAir/Automation-Library/library')

class USaqMajorCityRankingTable extends browserstackBase {
 
  async execute(driver) {
    await driver.get('www.iqair.com/us/world-air-quality')
    await driver.sleep(2000)
    await initializeBrowser(driver)
    await driver.sleep(2000)
   
const aqiData = await driver.findElements(webdriver.By.xpath("//table[@class = 'ranking__table ng-star-inserted']//td/p"));
   await helper.sleep(2000);
        await helper.sleep(2000);
        for (const aqi of aqiData){
            await helper.sleep(2000);
            const a = await aqi.getText()
           const promise1= Promise.resolve(a)
promise1.then((value) => {

    });
      }
  }
 
}
module.exports = new USaqMajorCityRankingTable().runTest()
for (const aqi of aqiData){
           
            const a = await aqi.getText()
const promise1= Promise.resolve(a)
promise1.then((value) => {
const flattened = [].concat(...value);
console.log (flattened)

Can Anyone please help!! enter image description here

I am confused if it is a 2D array, because, if I want to return [0][0] or [0][1]

I receive an error:

"cannot read properties of undefined (reading '0')"
5
  • I used, but didn't work
    – Beginner
    Commented Jul 8 at 19:40
  • Where are you getting that error? Commented Jul 8 at 20:26
  • @CodingEnthusiast I need those numbers in each array as a whole number. but it returns Nan/undefined
    – Beginner
    Commented Jul 8 at 20:59
  • and what does aqi.getText() returns? does it returns a comma seperating string containing multiple numbers or what? Commented Jul 8 at 21:05
  • It returns the numbers, but as text in an array
    – Beginner
    Commented Jul 8 at 21:08

1 Answer 1

0

aqi.getText() must be returning a number as string,

So try this:

var array = []
for (const aqi of aqiData) {
    const a = aqi.getText();
    array.push(parseInt(a));
}
console.log(array);
3
  • Now it returns adding each number in next array and last array has all the numbers. Do you know how to call only the last array? [ 186 ] [ 186, 162 ] [ 186, 162, 153 ] [ 186, 162, 153, 145 ] [ 186, 162, 153, 145, 138 ] [ 186, 162, 153, 145, 138, 122 ] [ 186, 162, 153, 118 ] [ 186, 162, 153, 145, 138, 122, 118, 110 ] [ 186, 162, 153, 145, 138, 122, 118, 110, 109 ] [ 186, 162, 153, 145, 138, 122, 118, 110, 109, 105 ]
    – Beginner
    Commented Jul 8 at 22:20
  • you must have added console.log to the loop but i added that after the loop Commented Jul 8 at 22:25
  • It worked. Thank you so much
    – Beginner
    Commented Jul 8 at 22:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.