-2

I want to select all the countries names in the tbody

enter image description here

website: https://www.worldometers.info/coronavirus/

i tried

document.querySelector("#main_table_countries > tbody:nth-child(2) > tr:nth-of-type(1n) > td:nth-of-type(1n) > a").innerText

it gives only the first country

i think i need to use loop to fetch lines but i don't know how

user11845248
  • 131
  • 8

5 Answers5

1

querySelector returns the first matching element. You need to use querySelectorAll to get all matching elements, then you can iterate the collection based on your requirements.

Amit
  • 45,440
  • 9
  • 78
  • 110
  • Uncaught TypeError: Failed to execute 'querySelectorAll' on 'Document': 1 argument required, but only 0 present. at :1:10 – user11845248 Mar 15 '20 at 21:33
  • You need to provide the css selector as an argument (the same value you used in the question) – Amit Mar 15 '20 at 21:34
  • @Amit i did `document.querySelectorAll("#main_table_countries > tbody:nth-child(2) > tr:nth-of-type(1n) > td:nth-of-type(1n) > a").innerText` – user11845248 Mar 15 '20 at 21:36
  • 2
    It returns a list of elements, you need to loop over them to get all their `innerText`. – Barmar Mar 15 '20 at 21:37
  • @Barmar sorry, i got `undefined` – user11845248 Mar 15 '20 at 21:40
  • because you need to loop. See https://stackoverflow.com/questions/10693845/what-do-queryselectorall-getelementsbyclassname-and-other-getelementsby-method – Barmar Mar 15 '20 at 21:41
  • I@Amit `document.querySelectorAll` will return an array, you need to iterate (loop) over those results. – rhys_stubbs Mar 15 '20 at 21:43
  • @rhys_stubbs it returns a NodeList, not an array. – VLAZ Mar 15 '20 at 21:44
  • i'm wondering why you don't just post the answer if you already know it, i'm not a javascript dev, and trying to work with selectors just hard for me atleast for now – user11845248 Mar 15 '20 at 21:47
  • @VLAZ Indeed it does, the OP is clearly a new to programming so I thought I would add a simple explanation that they may better understand. For the sake of simplicity, the OP may want to use `Array.from` to convert the NodeList to a normal array – rhys_stubbs Mar 15 '20 at 21:49
  • @rhys_stubbs `document.querySelectorAll("#main_table_countries > tbody:nth-child(2) > tr:nth-of-type(1n) > td:nth-of-type(1n) > a")` give NodeList that there text contain the country name, how can extract those countries using loop ? – user11845248 Mar 15 '20 at 21:50
1

You have to use querySelectorAll instead of querySelector.

UPDATE

Most country elements do not have links, so you have to just get the td without the a tag. Please check the updated code with the updated selector #main_table_countries > tbody:nth-child(2) > tr > td:nth-child(1).

To print the values:

const selector = "#main_table_countries > tbody:nth-child(2) > tr > td:nth-child(1)";

document.querySelectorAll(selector).forEach(el => console.log(el.textContent.trim()));

To get an array with the values:

const selector = "#main_table_countries > tbody:nth-child(2) > tr > td:nth-child(1)";

const counties = [...document.querySelectorAll(selector)].map(el => el.textContent.trim());
Christos Lytras
  • 36,310
  • 4
  • 80
  • 113
  • it doesn't give all the list, because the other countries have different seletor, without a tag – user11845248 Mar 15 '20 at 22:06
  • You have to change the selector and exclude the anchor tag `a`, because most countries do not have links. Please check my updated code and selector. **TIP**: You can add `.trim()` at the end of `el.textContent.trim()` to get rid of the spaces. – Christos Lytras Mar 15 '20 at 22:20
0
const nodeList = document.querySelectorAll("#main_table_countries > tbody:nth-child(2) > tr:nth-of-type(1n) > td:nth-of-type(1n) > a");

// iterate over the NodeList
nodeList.forEach(element => {

    // access each element here
    console.log(element.innerText);

});

// OR iterate over an array
const elementsArray = Array.from(nodeList);

elementsArray.forEach(element => {

    // access each element here
    console.log(element.innerText);

});
rhys_stubbs
  • 538
  • 6
  • 16
0

First, you will have to use querySelectorAll to return a NodeList of elements and use selector that selects all first column from table with id main_table_countries as below

let countries = [...document.querySelectorAll('table#main_table_countries tbody:nth-child(2) tr td:nth-child(1)')].map(e=>e.innerText);
console.log(countries);
xxMrPHDxx
  • 657
  • 5
  • 11
0

Combining @xxMrPHDxx and @rhys_stubbs answers, i finally figure it out

const nodeList = document.querySelectorAll("table#main_table_countries tr td:nth-child(1)");

// iterate over the NodeList
nodeList.forEach(element => {

    // access each element here
    console.log(element.innerText);

});

// OR iterate over an array
const elementsArray = Array.from(nodeList);

elementsArray.forEach(element => {

    // access each element here
    console.log(element.innerText);

});
user11845248
  • 131
  • 8