May 24, 2020

Selenium Python Tutorial 3 | selenium python Checkbox | Example to click Check Boxes Combinations

This is the third tutorial in the Selenium Python Tutorials for Beginners. This Selenium Python beginner tutorial explains how to work with selenium python checkbox. A checkbox is a web element that belongs in a group of checkboxes. You can select none, any or all check boxes in that group.. First, view the Selenium Python Checkbox tutorial. Then read below.
Here is my Selenium Python example to click checkboxes combination until the answer is correct, for each question in my HTML Quiz.

# 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 the checkbox user defined function file below.
import CheckboxFunctions as C

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"
driver = webdriver.Firefox(executable_path=exec_path)
wait = W(driver, wait_time_out)
# Navigate to the Quiz web page.
driver.get(URL)

i = 0
while i < 10:
    # The counter variable, i, represents the question number in the quiz.
    i += 1
    # Selenium Python scrolling the web page down by 120 pixels
    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 each checkbox by clicking it. 
    check_element_1.click()
    check_element_2.click()
    check_element_3.click() # Check checkbox 1, 2 and 3.
    # C is the alias for CheckboxFunctions Python script.
    # If answered returns True, skip the remainder of the while loop with continue statement.
    if C.answered(driver, i): continue
    check_element_1.click() # Check checkbox 2 and 3 only.
    if C.answered(driver, i): continue
    check_element_1.click()
    check_element_2.click() # Check checkbox 1 and 3 only.
    if C.answered(driver, i): continue
    check_element_2.click()
    check_element_3.click() # Check checkbox 1 and 2 only.
    if C.answered(driver, i): continue
    check_element_2.click() # Check checkbox 1 only.
    if C.answered(driver, i): continue
    check_element_1.click()
    check_element_2.click() # Check checkbox 2 only.
    if C.answered(driver, i): continue
    check_element_2.click()
    check_element_3.click() # Check checkbox 3 only.
    if C.answered(driver, i): continue
#driver.quit()

The above Test Automation code calls the answered user defined function that I wrote in CheckboxFunctions.py file below.

# Selenium WebDriver Python coding
from selenium.webdriver.common.by import By
# The following two import statements are for webdriverwait Python (explicit wait).
from selenium.webdriver.support.ui import WebDriverWait as W
from selenium.webdriver.support import expected_conditions as E
import time
wait_time_out = 15

def answered(d, question_number):
    # function documentation on the next line
    """Locate the answer element. Return True or False based on the value attribute."""
    wait_variable = W(d, wait_time_out)
    answer_element = wait_variable.until(E.presence_of_element_located((By.NAME, "answer" + str(question_number))))
    time.sleep(0.25)
    if "Correct." in answer_element.get_attribute("value"):
        return True
    else:
        return False

Want to understand the Selenium Python code better and see it working? Please view my Selenium Python Checkbox tutorial. Thank you.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.