Luca838
Luca838

Reputation: 47

Writing contents of a table to csv in selenium in python

I am trying to automate (selenium) the process of writing the contents of my transcript to a csv file with python. So far I have automated the clicks to reach the page with the table. Currently I am only able to display the first row of the table, but the full content of that row appears in one cell.

This is currently the code I have pertinent to writing the contents of my transcript.

id = 1 enter image description here

This is how the HTML table code looks like:

enter image description here

For every table row, the id number increases by 1. For example, VAR_STC_COURSE_NAME_1, VAR_STC_COURSE_NAME_2, etc. The number of rows changes as well depending on the number of courses taken.

I expect a csv file with rows and separate columns for every field. Currently I get the contents of one row in one cell.

Upvotes: 0

Views: 626

Answers (1)

KunduK
KunduK

Reputation: 33384

Import csv first and then use csv.writer and writerow([]).

It will write data for each row.The code covers for csv part only.

import csv

with open('testfile.csv','w', newline='') as d:
  for row in main_table:
       csv.writer(d).writerow([course_name.text,grade.text,course_credits.txt,ceus.text,repeat_list.txt,term_list.txt])

Upvotes: 1

Related Questions