2

I am attempting to write the 2nd value in text_entries to a file, but I do not want to include the first value in my .csv -- such as index2, index3, etc.

How can I write new rows without adding index headers to my CSV output? Code below:

import csv
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://www.example.se/list')

def get_elements_by_xpath1(driver, xpath):
        return [entry.text for entry in driver.find_elements_by_xpath(xpath)]

text_entries = [
    ("index2", "//div[@class='apartment-fact' and contains(span, '')][1]"),
    ("index3", "//div[@class='apartment-fact' and contains(span, '')][2]"),
    ("index4", "//div[@class='apartment-fact' and contains(span, '')][3]"),
    ("index5", "//div[@class='apartment-fact' and contains(span, '')][4]")]

with open('output.csv', 'ab') as fd:
    csv_output = csv.writer(fd)
    csv_output.writerow([name for name, xpath in text_entries])
    entries = []
    for name, xpath in text_entries:
        entries.append(get_elements_by_xpath1(driver, xpath))
    csv_output.writerows(zip(*entries))
martineau
  • 119,623
  • 25
  • 170
  • 301
Neverend
  • 35
  • 8
  • `csv_output.writerow([name for xpath in text_entries])`? `name` isn't defined. – roganjosh Oct 15 '19 at 20:34
  • Sorry; [name for name, xpath in text_entries] :) – Neverend Oct 15 '19 at 20:36
  • Could you elaborate a bit on what your expected / actual results are? – CEH Oct 15 '19 at 20:37
  • Then please fix with an [edit] – roganjosh Oct 15 '19 at 20:38
  • @Christine The webpage is with login & password and in a language most of you won't understand so I think it'll be difficult.. But when I read your question I think my term is wrong: My problem is that when the output is saved onto the CSV the names (i.e. index1, index2, index3) always come along for each new row meaning I get two rows (one row with index names and another with my xpath's). I want the output to only write the xpath's for each new added row and I've been trying to work it out for an hour now and can't seem to figure it out :( – Neverend Oct 15 '19 at 20:39
  • This sounds more like a csv / write-to-file issue than it is a Selenium issue. I would like to assist you, but I'm not well-versed in Python write-to-file conventions. – CEH Oct 15 '19 at 20:42
  • @Christine Alright, thank you and sorry for that – Neverend Oct 15 '19 at 20:44
  • No problem, just trying to categorize your question more efficiently so that the right people can assist you. – CEH Oct 15 '19 at 20:45
  • @Christine Deeply appreciated and excuse me if I was being misleading :) – Neverend Oct 15 '19 at 20:47
  • You need to [edit] your question and add a definition of the `get_elements_by_xpath1()` function you're using to provide a [mre] for others to use to reproduce the problem (and possibly fix it without having to guess). – martineau Oct 15 '19 at 21:21
  • 1
    @martineau The definition for `get_elements_by_xpath1(driver, xpath)` is there `return` (which brings text in this case, and in another case brings hrefs). As for the minimu reproducible example I'll add a few more lines of imports and code which are missing and might be helpful. Thanks for the tip! – Neverend Oct 15 '19 at 21:29
  • Sorry, I misspoke, I meant that `driver` isn't defined — but it sounds like you got my point. – martineau Oct 15 '19 at 21:30

1 Answers1

1

This seems to do what you want:


import csv

def get_elements_by_xpath1(driver, xpath):
    return [entry.text for entry in driver.find_elements_by_xpath(xpath)]

text_entries = [
    ("index2", "//div[@class='apartment-fact' and contains(span, '')][1]"),
    ("index3", "//div[@class='apartment-fact' and contains(span, '')][2]"),
    ("index4", "//div[@class='apartment-fact' and contains(span, '')][3]"),
    ("index5", "//div[@class='apartment-fact' and contains(span, '')][4]")]

with open('output.csv', 'a') as fd:
    csv_output = csv.writer(fd)
    for _, xpath in text_entries:
        csv_output.writerow([xpath] + get_elements_by_xpath1(driver, xpath))
Chris W.
  • 302
  • 1
  • 3
  • 10
  • W Thanks for the tip but now I get each line of code printed in separate rows (instead of the results from the Xpath). The good news is that it did remove the index header names! – Neverend Oct 15 '19 at 21:34
  • @Neverend Edited. I'm not exactly sure what you're looking for in each row. – Chris W. Oct 15 '19 at 21:42
  • 1
    My bad... I was looking to get values derived from the Xpath's. But I did some tweaking with your code and got it to work like this: 'with open('output.csv', 'a') as fd: csv_output = csv.writer(fd) entries = [] for name, xpath in text_entries: entries.append(get_elements_by_xpath1(driver, xpath)) csv_output.writerows(zip(*entries))' So problem solved! Thank you for that, deeply appreciated! – Neverend Oct 15 '19 at 21:46