Selenium with Python Basics Quiz Quiz

Test your knowledge of essential Selenium commands, element locators, and basic test automation concepts using Python. This beginner-friendly quiz covers driver initialization, finding elements, test frameworks, file operations, browser navigation, frames, and more.

  1. Driver Initialization

    Which command should you use first in your Selenium script to open a browser using Python?

    1. find_element_by_id('login')
    2. import driver.selenium
    3. from selenium import webdriver
    4. driver.get('https://example.com')

    Explanation: The correct answer is 'from selenium import webdriver' because importing the webdriver module is the first step before creating a driver instance. 'driver.get()' is used after the driver is created, and 'find_element_by_id()' is for finding elements on a page. 'import driver.selenium' is not a valid import statement.

  2. Locating Elements by ID

    Which method helps you locate a single element on a page by its unique ID attribute in Selenium with Python?

    1. find_element_by_class
    2. find_element_by_id
    3. locate_element_id
    4. find_elements_by_tag

    Explanation: The correct answer is 'find_element_by_id', which locates an element using its unique ID. 'find_elements_by_tag' is used for locating elements by their tag name, and 'find_element_by_class' is incorrect since the actual method is 'find_element_by_class_name'. 'locate_element_id' is not a valid Selenium method.

  3. Pytest Test Discovery

    When using pytest with Selenium, how should test function names be prefixed for automatic discovery?

    1. sel_
    2. start_
    3. auto_
    4. test_

    Explanation: Pytest discovers test functions with names starting with 'test_'. Prefixes like 'sel_', 'auto_', or 'start_' won't be automatically detected by pytest as test cases. Using the 'test_' prefix ensures your functions are recognized as tests by the framework.

  4. Working with CSV Files

    Before reading a CSV file in a Selenium script using Python, which module must you import?

    1. jsons
    2. csv
    3. xlsx
    4. excel

    Explanation: 'csv' is the correct module to import when working with CSV files. 'xlsx' and 'excel' do not exist as standard Python modules for CSV operations, and 'jsons' is related to JSON, not CSV data.

  5. Browser Navigation

    Which command is used to navigate the browser to a specific URL in Selenium with Python?

    1. driver.get('https://example.com')
    2. driver.request('https://example.com')
    3. driver.open_url('https://example.com')
    4. driver.locate('https://example.com')

    Explanation: 'driver.get()' is the correct command for navigating to a URL. 'driver.open_url()', 'driver.locate()', and 'driver.request()' are not standard Selenium methods for this operation. Only 'driver.get()' will open the desired webpage in the browser.

  6. Toggling Between Browser Windows

    Which property gives you a list of all browser window handles currently open in Selenium?

    1. driver.get_handles
    2. driver.window_handles
    3. driver.all_windows
    4. driver.window_list

    Explanation: 'driver.window_handles' provides a list of all open window handles. The other options like 'driver.all_windows', 'driver.window_list', and 'driver.get_handles' do not exist in Selenium and would cause errors if used.

  7. Test Class Setup in unittest

    In Python's unittest framework, which method sets up resources once before any test methods in a class are run?

    1. startClass
    2. setUp
    3. setUpClass
    4. beginTest

    Explanation: 'setUpClass' is the correct method for setting up resources before any tests in the class run, and it should be decorated with @classmethod. 'startClass' and 'beginTest' are not valid unittest methods, while 'setUp' runs before each test method, not just once.

  8. Finding Elements with CSS Selectors

    If you want to select an element using its CSS selector in Selenium, which method should you use?

    1. find_element_by_css_selector
    2. select_element_by_css
    3. locate_by_css
    4. find_css_element

    Explanation: 'find_element_by_css_selector' is the correct Selenium method to select an element by CSS selector. 'find_css_element', 'locate_by_css', and 'select_element_by_css' are not valid method names in Selenium Python.

  9. Uploading a File

    To upload a file with Selenium, what must you do after finding the relevant input element?

    1. Press the Enter key only
    2. Run an upload_file() method
    3. Double click the input field
    4. Send the file path using send_keys

    Explanation: You use 'send_keys' to send the file path to the file input element. Double clicking does not upload files, and there is no 'upload_file()' method in Selenium. Simply pressing Enter will not upload the file without specifying its path.

  10. Switching to a Frame

    What Selenium method enables you to switch context into an iframe on a web page?

    1. driver.set_frame
    2. driver.go_to_frame
    3. driver.enter_frame
    4. driver.switch_to.frame

    Explanation: 'driver.switch_to.frame' is the standard way in Selenium to switch into a specific frame. The options 'driver.go_to_frame', 'driver.enter_frame', and 'driver.set_frame' are not actual Selenium methods and would result in errors.

  11. Going Back in Browser History

    Which command allows you to navigate back to the previous page in the browser history using Selenium Python?

    1. driver.history()
    2. driver.reverse()
    3. driver.back()
    4. driver.previous()

    Explanation: 'driver.back()' directs the browser to go back. The commands 'driver.history()', 'driver.reverse()', and 'driver.previous()' are not defined in Selenium's API for navigating browser history.

  12. Reading an Excel File

    Which Python library should be imported for reading Excel .xlsx files in Selenium test scripts?

    1. csv
    2. openpyxl
    3. xlxs
    4. json

    Explanation: 'openpyxl' is the correct library for handling Excel .xlsx files in Python. 'csv' is for CSV files, 'json' is for JSON data, and 'xlxs' is a common typo for the correct term 'xlsx' but is not a library.

  13. Locating Elements by Link Text

    If you want to find a clickable hyperlink by its visible text in Selenium, which method should you use?

    1. find_element_by_tagname
    2. find_element_by_link_text
    3. find_element_by_href
    4. find_element_by_text

    Explanation: 'find_element_by_link_text' focuses on locating anchor (link) elements by their displayed text. 'find_element_by_href' and 'find_element_by_text' are not Selenium methods. 'find_element_by_tagname' is for locating elements by tag, not link text.

  14. Pytest Fixtures

    What is the decorator used in pytest to create a fixture for resource setup or teardown?

    1. @pytest.fixture
    2. @fixture
    3. @setup.fixture
    4. @py.fixture

    Explanation: '@pytest.fixture' is the decorator for registering a function as a fixture in pytest. '@fixture', '@setup.fixture', and '@py.fixture' are invalid decorators and will not be recognized by pytest.

  15. Refreshing the Browser Page

    Which command refreshes the current browser page in Selenium with Python?

    1. driver.refresh()
    2. driver.restart()
    3. driver.reload()
    4. driver.update()

    Explanation: 'driver.refresh()' reloads the current page in Selenium. 'driver.reload()', 'driver.update()', and 'driver.restart()' are not recognized Selenium commands for this operation and do not refresh the page.

  16. Class Inheritance in unittest

    When creating test classes in Python's unittest framework using Selenium, which class should your new test class inherit from?

    1. Test.UnitCase
    2. BaseTest
    3. unittest.TestCase
    4. unit.Testing

    Explanation: Test classes in unittest must inherit from 'unittest.TestCase' to work properly. 'unit.Testing', 'Test.UnitCase', and 'BaseTest' are incorrect and will not provide test discovery or assertion features.