0

I want to pick only one html <span> element from a <th> class and print on terminal. I've tried using [1] at the end of the <span> but it didn't work out.

I've tried this with selenium:

for element in driver.find_elements(By.XPATH, '//div/table/thead/tr/th/span[1]'):
    print(element.text)

The html is the following:

<thead><tr class="C($tertiaryColor) Fz(xs) Ta(end)"><th class="Ta(start) W(100px) Fw(400) Py(6px)"><span>Date</span></th><th class="Fw(400) Py(6px)"><span>Open</span></th><th class="Fw(400) Py(6px)"><span>High</span></th><th class="Fw(400) Py(6px)"><span>Low</span></th><th class="Fw(400) Py(6px)"><span>Close*</span></th><th class="Fw(400) Py(6px)"><span>Adj Close**</span></th><th class="Fw(400) Py(6px)"><span>Volume</span></th></tr></thead>

Needing more information, leave a comment.

Rodrigo
  • 61
  • 1
  • 12
  • when you print out driver.find_elements(By.XPATH, '//div/table/thead/tr/th/span[1]') do you get any results? could you give us the site where the span is located? to better assist? – Andrew Ryan Feb 07 '22 at 20:57
  • Your question is missing basic debugging details. We need to see a link to that page or entire XML of it in order to help here – Prophet Feb 07 '22 at 21:01
  • This is the page: https://finance.yahoo.com/quote/BTC-EUR/history – Rodrigo Feb 07 '22 at 21:15

1 Answers1

1

The SPAN s are decendents of the TH s. Similarly the TH s are decendents of the TR. So you can use either of the following Locator Strategies:

  • xpath:

    for element in driver.find_elements(By.XPATH, '//thead/tr//th/span'):
            print(element.text)
    
  • css-selectors:

    for element in driver.find_elements(By.XPATH, 'thead > tr th > span'):
            print(element.text)
    

Update

To identify the first <span> you can use either of the following Locator Strategies:

  • xpath:

    print(driver.find_element(By.CSS_SELECTOR, "thead > tr th:nth-child(1) > span").text)
    
  • css-selectors:

    print(driver.find_element(By.XPATH, "//thead/tr//following::th[1]/span").text)
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • There are some 6 spans at the th class, I need to pick only one span, and cannot be by text. I will parse numbers in another "th class" and these numbers changes everyday. – Rodrigo Feb 07 '22 at 21:20
  • Which specific `` are you looking for? – undetected Selenium Feb 07 '22 at 21:21
  • The first `` – Rodrigo Feb 07 '22 at 21:22
  • @Rodrigo Checkout the updated answer and let me know the status. – undetected Selenium Feb 07 '22 at 22:03
  • I received this error: `for element in driver.find_elements(By.XPATH, "//thead/tr//following::th[1]/span").text:` AttributeError: 'list' object has no attribute 'text' Then, I changed to `for element in driver.find_elements(By.XPATH, "//thead/tr//following::th[1]/span"): print(element.text)` and still prints all spans and now without a "Date" span: Open High Low Close* Adj Close** Volume – Rodrigo Feb 07 '22 at 22:12
  • 1
    @Rodrigo Neither you need a _list_ nor you need _`find_elements`_. You need the first `` and you need _`find_element()`_ – undetected Selenium Feb 07 '22 at 22:14