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 fine-tuned the script to click all the divs on the first page and extract data. But when I've tried to output the scraped data to CSV; It only writes the last item data.
here is my code:
i = 1
while i < 10:
myxpath = "/html/body/div[1]/div[3]/div/div[1]/leads-list/div/ul/li["+str(i)+"]"
print(myxpath)
time.sleep(5)
item = (By.XPATH, '//*[@id="description"]')
WebDriverWait(driver, 50).until(EC.element_to_be_clickable(item))
Itemlinks = driver.find_elements_by_xpath(myxpath)
print('total items displayed in current page', len(Itemlinks))
for items in Itemlinks:
items.click()
print("item clicked")
time.sleep(10)
sections = driver.find_elements_by_xpath('//*[@id="longDescriptionHTML"]')
with open('zbuyer6.csv', 'w') as file:
writer = csv.writer(file)
for section in sections:
name = section.find_element_by_xpath(
"/html/body/div[1]/div[3]/div/div[2]/lead-detail/div[4]/div[1]/div[2]/h1").text
email = section.find_element_by_xpath(
"/html/body/div[1]/div[3]/div/div[2]/lead-detail/div[4]/div[1]/ul[1]/li[1]/div[1]/ul/li[2]").text
print(name, email)
writer.writerow((name, email))
driver.find_element_by_class_name("back-link.pull-left.col-md-3").click()
driver.execute_script("window.scrollTo(0, 50)")
time.sleep(5)
i += 1
continue
I expect a CSV file with rows and separate columns for every field. Currently, I only get the last item data. Thanks.
