Thursday, May 14, 2020

Selenium Python Tutorial 2 | Python Automation Testing | Selenium Python Radio Button

This is the second tutorial in the Selenium Python Tutorials for Beginners. This Selenium Python beginner tutorial explains how to work with radiobuttons. First, view the Selenium Python Radio Button tutorial. Then read below.
 
A radiobutton is a web element that belongs in a group of radiobuttons. Exactly one radiobutton can be selected in that group, meaning the radio button selection is mutually exclusive. Here is my first Selenium Python example to answer Question 1 of my Selenium WebDriver 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

exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
URL = "https://inderpsingh.blogspot.com/2013/04/SeleniumWebDriverQuiz4.html"
wait_time_out = 5
# Give the locator for Selenium Python Radio Button
answer1_radio_id_locator = "13"
answer1_name_locator = "answer1"

driver = webdriver.Firefox(executable_path=exec_path)
wait = W(driver, wait_time_out)
driver.get(URL)
# Use explicit wait (webdriverwait python) to find the radiobutton.
radio_element = wait.until(E.presence_of_element_located((By.ID, answer1_radio_id_locator)))
radio_element.click()
answer1_element = wait.until(E.presence_of_element_located((By.NAME, answer1_name_locator)))

# Validate the test result.
if "Correct." in answer1_element.get_attribute("value"):
    print ("Test passed.")
else:
    print ("Test failed.")

Here is my second Selenium Python example, that answers the quiz automatically using Python browser automation.

# 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 time

exec_path = r"E:\Training\SeleniumPython\Downloads\geckodriver-v0.26.0-win32\geckodriver.exe"
URL = "https://inderpsingh.blogspot.com/2013/04/SeleniumWebDriverQuiz4.html"
wait_time_out = 5
answer_name_locator = "answer"
score_id_locator = "score"
driver = webdriver.Firefox(executable_path=exec_path)
wait = W(driver, wait_time_out)
driver.get(URL)
# Run a loop for the question number 1 through 6.
for q in range(1, 7):
    # Run a loop for the answer number 1 through 4.
    for a in range(1, 5):
        # Craft the locator for Selenium Python Radio Button.
        radio_element = wait.until(E.presence_of_element_located((By.ID, str(q) + str(a))))
        radio_element.click()
        time.sleep(1)
        answer_element = wait.until(E.visibility_of_element_located((By.NAME, answer_name_locator + str(q))))
        if "Correct." in answer_element.get_attribute("value"): break

#validation
score_element = wait.until(E.visibility_of_element_located((By.ID, score_id_locator)))
if "6/6" in score_element.text:
    print ("Test is passed.")
else:
    print ("Test is failed.")

That is all in this Python Automation Testing tutorial. Want to see the above Selenium Python code working in the PyCharm IDE? Please see my Selenium Python Radio Button tutorial. Thank you.

1 comment: