Looping through a dynamic table - python

Viewed 154

Please Help. I have been working on this for days and I cannot figure out where I am getting this wrong. I am trying to loop through a table but i am only getting the first row and nothing else. What am i doing wrong? Im guessing my loop might be the culprit but i am still new to python and cannot figure it out. I want to end up with everything in an excel document

from numpy import fabs
from selenium import webdriver
from selenium.webdriver.common.by import By
import pandas as pd
driver = webdriver.Chrome(r"C:\Users\noree\OneDrive\Documents\chromedriver.exe")

driver.get('https://www.depositaccounts.com/banks/assets.aspx?instType=&stateType=hq&state=')
driver.maximize_window()

#get url largest banks and credit unions by assets

#Show all entries - xpath for show all button
show_all_button = driver.find_element(By.XPATH,'//*[@id="results"]/div/a')

# Click 'Show all' Button
show_all_button.click()   

#scrape the tables
rank = driver.find_elements(By.XPATH, '//*[@id="assetsTable"]/tbody/tr[2]/td[1]')
financial_institution = driver.find_elements(By.XPATH,'//table[@id="assetsTable"]/tbody/tr[2]/td[2]/a')
headquarters = driver.find_elements(By.XPATH, '//tbody/tr[2]/td[3]')
assets = driver.find_elements(By.XPATH, '//tbody/tr[2]/td[4]')
asset_growth = driver.find_elements(By.XPATH, '//tbody/tr[2]/td[5]')
branches = driver.find_elements(By.XPATH, '//tbody/tr[2]/td[6]')
states_with_branches = driver.find_elements(By.XPATH, '//tbody/tr[2]/td[7]')
employees = driver.find_elements(By.XPATH, '//tbody/tr[2]/td[7]')
customer_accounts = driver.find_elements(By.XPATH, '//tbody/tr[2]/td[8]')

#create empty list
bank_results = []
for i in range(len(rank)):
    temporary_data={
        'Rank': rank[i].text,
        'Financial Institution': financial_institution[i].text,
        'Headquarters': headquarters[i].text,
        'Assets': assets[i].text,
        'Asset Growth': asset_growth[i].text,
        'Branches': branches[i].text,
        'States with Branches': states_with_branches[i].text,
        'Employees': employees[i].text,
        'Customer Accounts': customer_accounts[i].text
    }
    bank_results.append(temporary_data)

df_data = pd.DataFrame(bank_results)
df_data
1 Answers

your error is coming from the fact you just select one record

if you want a solution nearest to yours:

from selenium.webdriver.support import expected_conditions as EC
 :
 :
url='https://www.depositaccounts.com/banks/assets.aspx?instType=&stateType=hq&state='
driver.get(url)
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="results"]/div/a'))).click()
time.sleep(3)

records = driver.find_elements(By.XPATH, "//table[@id='assetsTable']//tr[not(./th)]")
nbr_records = len(records)

bank_results = []
for i in range(nbr_records):
    temporary_data={
        'Rank': records[i].find_element(By.XPATH, "./td[1]").text,
        'Financial Institution': records[i].find_element(By.XPATH, "./td[2]").text,
        'Headquarters': records[i].find_element(By.XPATH, "./td[3]").text,
        'Assets': records[i].find_element(By.XPATH, "./td[4]").text,
        'Asset Growth': records[i].find_element(By.XPATH, "./td[5]").text,
        'Branches': records[i].find_element(By.XPATH, "./td[6]").text,
        'States with Branches': records[i].find_element(By.XPATH, "./td[7]").text,
        'Employees': records[i].find_element(By.XPATH, "./td[8]").text,
        'Customer Accounts': records[i].find_element(By.XPATH, "./td[9]").text
    }
    bank_results.append(temporary_data)

xpath = "//table[@id='assetsTable']//tr[not(./th)]"

means

select all trs which dont contain a tag th and have a parent table with id = assetsTable

Related