how do scientist know that the chemical elements here on earth are the same as on the planets in space and not some different elements

By observing starlight, we know what elements are in the stars far beyond. The are the same as here.