0

I'm trying to iterate a limited number of <span> elements from html, which are actually lists generated from webdriver.

I've tried doing this:

for i in range(9):
    print(driver.find_element(By.CSS_SELECTOR, "thead > tr th:nth-child("+[i]+") > span").text)

And I received the following error:

print(driver.find_element(By.CSS_SELECTOR, "tbody > tr td:nth-child("+[i]+") > span").text) TypeError: can only concatenate str (not "list") to str

Here is the html snippet:

<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>

It's all from this page: https://finance.yahoo.com/quote/BTC-EUR/history

Rodrigo
  • 61
  • 1
  • 12
  • seems like that should be just "i" not "[i]". You can probably just use find_elements and let the locator do the work for you. Then iterate over the webelement array that find_elements returns. – pcalkins Feb 08 '22 at 00:20
  • But with find_elements I cant put a limited number of iterations and I need it. – Rodrigo Feb 08 '22 at 00:36
  • You find the elements and iterate them. Try `driver.find_elements` this will return a list of we element and iterate through it – rick Feb 08 '22 at 04:13
  • Even better would just use position()<10(index starts at 1) as an xpath to find all the td . – Arundeep Chohan Feb 08 '22 at 06:45
  • @ArundeepChohan what do you mean? Is that position a function from selenium or the css property? – Rodrigo Feb 10 '22 at 22:23
  • @MahmodulHaque I need to find only ten span elements, sorry, i forgot to mention that. `.find_elements` gets everything, Didnt see how to limit it in the documentation. – Rodrigo Feb 10 '22 at 22:37

1 Answers1

1

As suggested in the Update section of this answer to extract the text only from the first <span> which is a decendent of the first <th> and you are restricting the Locator Strategy as:

driver.find_element(By.CSS_SELECTOR, "thead > tr th:nth-child(1) > span")

This usecase

To iterate through all the <span> elements from html of the website you can use List Comprehension and you can use the following Locator Strategy:

driver.get('https://finance.yahoo.com/quote/BTC-EUR/history')
print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "thead > tr th > span")))])

Console Output:

['Date', 'Open', 'High', 'Low', 'Close*', 'Adj Close**', 'Volume', 'Symbol', 'Last Price', 'Change', '% Change']

To retrict the number of elements within the List to 9 elements you can use:

driver.get('https://finance.yahoo.com/quote/BTC-EUR/history')
print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "thead > tr th > span")))][:9])  
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352