I'm trying to write data in a for loop in a excel spreadsheet, i ultimately, want to have the code like the following:
f.write(ln.text + "," + av.text + "\n")
however, it will not work out the way i want it, so i have to do it this way, here the code below
Main.py
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import csv
from selenium.webdriver.support.ui import WebDriverWait
import unittest
class Test():
# filename = "list.csv"
# f = open(filename,"w")
# headers = "listing name, aval \n"
# f.write(headers)
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.get("https://orlando.craigslist.org/search/apa?s=100")
self.filename = "list.csv"
self.f = open(self.filename,"w")
self.headers = "listing name, aval \n"
self.f.write(self.headers)
def task(self):
driver = self.driver
filename = self.filename
f = self.f
headers = self.headers
ln = driver.find_elements_by_xpath('//*[@id="sortable-results"]/ul/li/p/a')
size = len(driver.find_elements_by_xpath('//*[@id="sortable-results"]/ul/li/p/a'))
for i in range(0, size):
ln = driver.find_elements_by_xpath('//*[@id="sortable-results"]/ul/li/p/a')
ln = ln[i]
f.write(ln.text + ",")
self.getLn(ln)
ln = ln.click()
av = driver.find_element_by_xpath('/html/body/section/section/section/div[1]/p[1]/span[2]')
f.write(av.txt + "\n")
self.getAv(av)
back = driver.find_element_by_xpath('/html/body/section/header/nav/ul/li[3]/p/a').click()
if i == 5:
f.close()
break
def getLn(self,ln):
driver = self.driver
ln = ln
if ln:
print (ln.text)
else:
print ("No listing name")
def getAv(self,av):
driver = self.driver
if av:
print (av.text)
else:
print ("No listing name")
def initialize():
return Test
def teardown(self):
self.driver.quit()
run = Test()
run.setUp()
tas = run.task()
run.teardown()
if __name__ == '__main__':
unittest.main()
It works, but it does't export on the csv like i want it too.
I ultimately want the output together in the same line like this:
f.write(ln.text + "," + av.text + "\n")
How would i do this ?
csvmodule... What is your question here? What do you want, and what are you getting?lnoravcontainsNone, i.e. the element you're searching for withdriver.find_elements_by_xpathisn't finding what you're looking for.ln = ln.click(). After you do that,lndoesn't contain the element you found. Don't reuse variables like that, give it a different name. Or maybe you don't need to assignln.click()to anything at all, since it doesn't return anything useful.