HOME/Articles/

selenium Example3DropDownMenu (snippet)

Article Outline

Python web automation, selenium example 'Example3DropDownMenu'

Modules used in program:

  • import time
  • import unittest

Example3DropDownMenu

Python selenium example: Example3DropDownMenu

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import unittest
import time


class Example(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Chrome("C:\\Users\\benpe\\DevMountain\\testing-resources\\chromedriver.exe")
        self.driver.implicitly_wait(20)
        self.driver.set_page_load_timeout(20)
        self.driver.maximize_window()

    def tearDown(self):
        self.driver.close()

    def test_challenge(self):
        self.driver.get("https://fs2.formsite.com/meherpavan/form2/index.html?1537702596407")
        self.driver.implicitly_wait(10)

        element = self.driver.find_element_by_id("RESULT_RadioButton-9")
        drp = Select(element)
        #select by visible text
        drp.select_by_visible_text('Morning') #Morning
        time.sleep(2)

        #select by index number
        drp.select_by_index(2) #Afternoon
        time.sleep(2)

        #select by value
        drp.select_by_value("Radio-2") #Evening
        time.sleep(2)

        print(len(drp.options))

        #capture all the options and print(them as output)
        all_options = drp.options
        for option in all_options:
            print(option.text)

        time.sleep(2)


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