1

I'm trying to use Selenium to pull the stock symbols from this page and insert them into a list: https://www.barchart.com/stocks/highs-lows/highs?timeFrame=1y

My code:

StockList = []

driver = webdriver.Chrome()
url = "https://www.barchart.com/stocks/highs-lows/highs?timeFrame=1y"
driver.get(url)

stock_list = driver.find_elements_by_tag_name("tr")
for stock in stock_list:
    stock.find_element_by_name("data-current-symbol=")
    print(stock)

I receive a NoSuchElementException.

When I inspect the page, each tr has the following: "data-current-symbol="ACY

How can I pull out the stock symbol?

2
  • I guess, the = should not be there in instock.find_element_by_name("data-current-symbol=") Commented Sep 27, 2021 at 4:16
  • Removed = and changed to stock.find_element_by_name("data-current-symbol") - it didn't work. Commented Sep 27, 2021 at 4:19

1 Answer 1

1

Use this selector:

tr[data-current-symbol]

And utilize .get_attribute method:

StockList = []

driver = webdriver.Chrome()
url = "https://www.barchart.com/stocks/highs-lows/highs?timeFrame=1y"
driver.get(url)

stock_list = driver.find_elements_by_css_selector("tr[data-current-symbol]")

for stock in stock_list:
    symbol = stock.get_attribute('data-current-symbol')
    print(symbol)
    StockList.append(symbol)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.