Rodrigo
Rodrigo

Reputation: 61

Iteration through a list using Selenium

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

Upvotes: 0

Views: 199

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193078

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

Upvotes: 1

Related Questions