After import logging in Python 3, you can use logging.basicConfig to configure logging e.g. to log only certain levels, specify a logging format or specify the log file. Here is the first Selenium Python logging example, which is in Logs.py file in the
. The explanations are in the comments within the code (lines or phrases starting with #).
# Selenium WebDriver Python coding
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as W
from selenium.webdriver.support import expected_conditions as E
import CheckboxFunctions as C
# Import Utilities in order to call the log function.
import Utilities as U
exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
URL = "
https://inderpsingh.blogspot.com/2013/01/HTMLCSSQuiz1.html"
wait_time_out = 15
check_name_locator = "option"
# Specify the log file file path and file name with your computer's folder. Escape the backslashes.
# The following is an example only.
log_file = "E:\\Training\\SeleniumPython\\Logs\\LogCheckBoxQuiz.txt"
# Specify the log message for pass and fail.
pass_message = "Answered correctly - Question Number"
fail_message = "Answered incorrectly - Question Number"
driver = webdriver.Firefox(executable_path=exec_path)
wait = W(driver, wait_time_out)
driver.get(URL)
i = 0
while i<10:
i += 1
driver.execute_script("window.scrollBy(0,120)","")
check_element_1 = wait.until(E.presence_of_element_located((By.NAME, check_name_locator + str(i) + "1")))
check_element_2 = wait.until(E.presence_of_element_located((By.NAME, check_name_locator + str(i) + "2")))
check_element_3 = wait.until(E.presence_of_element_located((By.NAME, check_name_locator + str(i) + "3")))
check_element_1.click()
check_element_2.click()
check_element_3.click() # checkboxes 1, 2 & 3 are selected
if C.answered(driver, i):
# Python Selenium logging if the question is answered successfully
U.log("INFO", pass_message + str(i), log_file)
continue
check_element_1.click() # checkboxes 2 & 3 are selected
if C.answered(driver, i):
# Python Selenium logging if the question is answered successfully
U.log("INFO", pass_message + str(i), log_file)
continue
check_element_1.click()
check_element_2.click() # checkboxes 1 & 3 are selected
if C.answered(driver, i):
# Python Selenium logging if the question is answered successfully
U.log("INFO", pass_message + str(i), log_file)
continue
check_element_2.click()
check_element_3.click() # checkboxes 1 & 2 are selected
if C.answered(driver, i):
# Python Selenium logging if the question is answered successfully
U.log("INFO", pass_message + str(i), log_file)
continue
check_element_2.click() # only checkbox 1 is selected
if C.answered(driver, i):
# Python Selenium logging if the question is answered successfully
U.log("INFO", pass_message + str(i), log_file)
continue
check_element_1.click()
check_element_2.click() # only checkbox 2 is selected
if C.answered(driver, i):
# Python Selenium logging if the question is answered successfully
U.log("INFO", pass_message + str(i), log_file)
continue
check_element_2.click()
check_element_3.click()# only checkbox 3 is selected
if C.answered(driver, i):
# Python Selenium logging if the question is answered successfully
U.log("INFO", pass_message + str(i), log_file)
continue
# Selenium Python logging if the question is still unanswered.
U.log("ERROR", fail_message + str(i), log_file)
The above Selenium Python script calls the answered user defined function that I wrote in CheckboxFunctions.py file below.