0

I am working on a project here a work and I have print lines being displayed for 5+ elements. I need to add all the outputs into a txt file. I was told I can use a for loop but not sure how. What are my best options.

import unittest
from selenium import webdriver
import time
import sys

driver = webdriver.Chrome()

driver.get('website')
driver.maximize_window()

# Displays Time Sheet for Current Day
element = driver.find_element_by_xpath('//*[@id="page-wrapper"]/div[1]/h1')  
# this element is visible

print(element.text)

# Displays Start Time
element = driver.find_element_by_xpath('//*
[@id="pageinner"]/div/div[1]/div/div/div/div[1]/div[2]/div[1]')
print("Start time was at:", element.text)

# Displays End Time
element = driver.find_element_by_xpath('//*
[@id="pageinner"]/div/div[1]/div/div/div/div[4]/div[2]/div[1]')
print("Clocked out as of:", element.text)

# Displays when out to Lunch
element = driver.find_element_by_xpath('//*[@id="page-
inner"]/div/div[1]/div/div/div/div[2]/div[2]/div[1]/h3')
print("I left for Lunch at:", element.text)

# Displays when back from Lunch
element = driver.find_element_by_xpath('//*[@id="page-
inner"]/div/div[1]/div/div/div/div[3]/div[2]/div[1]/h3')
print("I arrived back from Lunch at:", element.text)

# Total Hours for The Day
element = driver.find_element_by_xpath('//*[@id="page-
inner"]/div/div[1]/div/div/div/div[5]/div[2]/div[1]')
print("I was at work for:", element.text)

'''
# Save to txt
sys.stdout = open('file.txt', 'w')
print(element.text)
'''

# Screenshot
# driver.save_screenshot('screenshot.png')


driver.close()

if __name__ == '__main__':
    unittest.main()

At the end Here I need to save all the prints into a txt file for documentation.

  • 3
    Do you mean 'move `stdout` into a `.txt` file'? You can just `python myfile.py > out.txt`. – yyny Sep 06 '17 at 23:02
  • Otherwise, visit https://docs.python.org/2/tutorial/inputoutput.html and Ctrl+F for `file`. – yyny Sep 06 '17 at 23:13
  • I've tried that but it doesnt work – Diante Cross Sep 06 '17 at 23:18
  • It just says invalid syntax – Diante Cross Sep 06 '17 at 23:19
  • 1
    You are likely using Python 3 then, where print is a function with a file keyword. – Alexander Huszagh Sep 06 '17 at 23:27
  • 2
    Welcome to Stack Overflow! See: [How do I do X?](https://meta.stackoverflow.com/questions/253069/whats-the-appropriate-new-current-close-reason-for-how-do-i-do-x) The expectation on SO is that the user asking a question not only does research to answer their own question but also shares that research, code attempts, and results. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! See also: [ask] – JeffC Sep 06 '17 at 23:28
  • 2
    Possible duplicate of [Python Print String To Text File](https://stackoverflow.com/questions/5214578/python-print-string-to-text-file) – JeffC Sep 06 '17 at 23:29
  • I am using PyCharm to write and run the code. – Diante Cross Sep 07 '17 at 16:22
  • This kind of sucks - I wish I could do something about that. – EJoshuaS - Stand with Ukraine Sep 13 '17 at 17:48

1 Answers1

1

Selenium 3.5 with Python 3.6.1 bindings provides a simpler way to redirect all the Console Outputs into a log file.

You can create a sub-directory within your Project space by the name Log and start redirecting the Console Outputs into a log file as follows:

# Firefox
driver = webdriver.Firefox(executable_path=r'C:\your_path\geckodriver.exe', log_path='./Log/geckodriver.log')

# Chrome
driver = webdriver.Chrome(executable_path=r'C:\your_path\chromedriver.exe', service_log_path='./Log/chromedriver.log')

# IE
driver = webdriver.Ie(executable_path=r'C:\your_path\IEDriverServer.exe', log_file='./Log/IEdriver.log')

It is worth to mention that the verbosity of the webdriver is easily configurable.

undetected Selenium
  • 151,581
  • 34
  • 225
  • 281