Scraping data from collapsable tables using selenium, there are two tables where the structure differs: there are rows inside another tr. I want to access tds by index. However, this code first outputs those tables fully, then thrice the indices.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
website = 'https://egov.kz/cms/ru/articles/2Ftechnical_spisok'
path = r'C:Program Files (x86)chromedriver.exe'
service = Service(executable_path = 'chromedriver.exe')
driver = webdriver.Chrome()
driver.get(website)
elements = driver.find_elements(By.XPATH, "//h3[@class = 'slidedown-title toggle']")
WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.XPATH, "//h3[@class = 'slidedown-title toggle']")))
for element in elements:
element.click()
college_names = []
college_address = []
college_type = []
govermental = True
rows = driver.find_elements(By.XPATH,'//tr')
parentRows = driver.find_elements(By.XPATH, "//tr[.//tr]")
for row in rows:
if row in parentRows:
rows_ins = row.find_elements(By.TAG_NAME,'tr')
for ins in rows_ins:
print(ins.find_element(By.XPATH, './td[1]').text)
else:
print(row.find_element(By.XPATH, './td[1]').text)
Tried writing XPATH in row.find_elements :
"//tr[.//tr]"
for row in rows:
if row in parentRows:
rows_ins = row.find_elements(By.TAG_NAME,'tr')
New contributor
Jennie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.