Iteration through a list using Selenium

162
March 21, 2022, at 09:50 AM

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

Answer 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])  
Rent Charter Buses Company
READ ALSO
Automate forking a github repository

Automate forking a github repository

Here is a script in Python that is used to clone repositories given the github account name (source_account), the name of the source repo (source_repo), and the source branch (source_branch)Is there a way I could change this in order to Fork all public...

108
Variable inconsistency in Python?

Variable inconsistency in Python?

I've run into a strange inconsistencyThis is a simplified version of the problem:

89
How can create a nested directory?

How can create a nested directory?

What is to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:

140
owl carousel stop showing images

owl carousel stop showing images

Add owl carousel css and js:

122