How to download certain columns of a table using Selenium and Python?

Question:

I want to get 2 columns(Symbol, Name) of a table on this website : https://www.nasdaq.com/market-activity/quotes/nasdaq-ndx-index
enter image description here

It seemed that each row has its class name as "nasdaq-ndx-index__row" so I tried

from selenium.webdriver.common.by import By
from selenium import webdriver
driver = webdriver.Chrome('D:\PROGRAM\chromedriver.exe')

url = "https://www.nasdaq.com/market-activity/quotes/nasdaq-ndx-index"
driver.get(url)
driver.implicitly_wait(1)

xpath = "/html/body/div[2]/div/main/div[2]/article/div[2]/div/div[3]/div[3]/div[2]/table/tbody/tr[2]"

tb = driver.find_elements(By.CLASS_NAME, "nasdaq-ndx-index__row")

but tb is just a list of

<selenium.webdriver.remote.webelement.WebElement (session="f66740af2c8b92f4c81f30e893044cdc", element="57a9c748-bbdc-4626-8e31-2cb6f7b680f4")>
<selenium.webdriver.remote.webelement.WebElement (session="f66740af2c8b92f4c81f30e893044cdc", element="b1c72b3b-9934-418a-a743-1aedf5dcd65c")>
<selenium.webdriver.remote.webelement.WebElement (session="f66740af2c8b92f4c81f30e893044cdc", element="c68764ea-d82a-42f1-8f3d-0801d9604eae")>
<selenium.webdriver.remote.webelement.WebElement (session="f66740af2c8b92f4c81f30e893044cdc", element="a2dad31a-ad7b-4b41-ba58-d6424b093b96")>

What am I missing and how could I get the 2 columns?

Asked By: maynull

||

Answers:

If you inspect the rows themselves you can see that the first two columns are ‘th’ elements.

You can use this snippet to get the two first columns:

for row in tb:
    cols = row.find_elements(By.TAG_NAME, "th")
    print(cols[0].text, cols[1].text)
Answered By: Salad
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.