0
0
Selenium Pythontesting~20 mins

Right click (context_click) in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Right Click Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Selenium Python code snippet?
Consider the following Selenium Python code that performs a right-click on a web element. What will be printed if the context click is successful and the alert text is 'Context menu opened'?
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By

# Assume driver is already initialized and navigated to the page
button = driver.find_element(By.ID, 'menu-button')
actions = ActionChains(driver)
actions.context_click(button).perform()
alert = driver.switch_to.alert
print(alert.text)
alert.accept()
ASyntaxError
BNoSuchElementException
CTimeoutException
DContext menu opened
Attempts:
2 left
💡 Hint
Think about what the alert text would be after a successful right-click triggers a context menu alert.
assertion
intermediate
2:00remaining
Which assertion correctly verifies a right-click context menu is displayed?
You want to assert that after a right-click on an element, a context menu with id 'context-menu' becomes visible. Which assertion is correct?
Selenium Python
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Assume driver and actions are set up
button = driver.find_element(By.ID, 'menu-button')
actions.context_click(button).perform()
wait = WebDriverWait(driver, 10)
Aassert wait.until(EC.element_to_be_clickable((By.ID, 'menu-button')))
Bassert wait.until(EC.visibility_of_element_located((By.ID, 'context-menu')))
Cassert wait.until(EC.invisibility_of_element_located((By.ID, 'context-menu')))
Dassert wait.until(EC.alert_is_present())
Attempts:
2 left
💡 Hint
You want to check if the context menu is visible after right-click, not invisible or clickable.
locator
advanced
2:00remaining
Which locator is best to find a context menu item labeled 'Delete' for right-click testing?
You want to locate the 'Delete' option inside a context menu that appears after right-click. The menu items are
  • elements inside a
      with id 'context-menu'. Which locator is best?
  • ABy.XPATH, "//ul[@id='context-menu']/li[text()='Delete']"
    BBy.CSS_SELECTOR, "ul#context-menu > li:contains('Delete')"
    CBy.ID, 'Delete'
    DBy.CLASS_NAME, 'delete-item'
    Attempts:
    2 left
    💡 Hint
    Remember CSS selectors do not support :contains, and IDs must be unique and present.
    🔧 Debug
    advanced
    2:00remaining
    Why does this right-click test fail with ElementNotInteractableException?
    Given this code snippet, the test fails with ElementNotInteractableException. What is the most likely cause?
    Selenium Python
    button = driver.find_element(By.ID, 'menu-button')
    actions = ActionChains(driver)
    actions.context_click(button).perform()
    AThe element is hidden or not visible on the page
    BThe driver is not initialized
    CThe locator ID is incorrect
    DThe context_click method is deprecated
    Attempts:
    2 left
    💡 Hint
    ElementNotInteractableException usually means the element cannot be clicked because it is not visible or enabled.
    framework
    expert
    3:00remaining
    How to integrate right-click context menu test in pytest with setup and teardown?
    You want to write a pytest test function that opens a browser, navigates to a page, performs a right-click on an element with ID 'menu-button', verifies the context menu appears, and then closes the browser. Which code snippet correctly implements this?
    A
    import unittest
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.action_chains import ActionChains
    
    class TestContextClick(unittest.TestCase):
        def setUp(self):
            self.driver = webdriver.Chrome()
    
        def test_right_click(self):
            self.driver.get('http://example.com')
            button = self.driver.find_element(By.ID, 'menu-button')
            actions = ActionChains(self.driver)
            actions.context_click(button).perform()
            self.assertTrue(self.driver.find_element(By.ID, 'context-menu').is_displayed())
    
        def tearDown(self):
            self.driver.quit()
    B
    import pytest
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.action_chains import ActionChains
    
    def test_right_click_context_menu():
        driver = webdriver.Chrome()
        driver.get('http://example.com')
        button = driver.find_element(By.ID, 'menu-button')
        actions = ActionChains(driver)
        actions.context_click(button).perform()
        assert driver.find_element(By.ID, 'context-menu').is_displayed()
        driver.quit()
    C
    import pytest
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    @pytest.fixture
    def driver():
        driver = webdriver.Chrome()
        yield driver
        driver.quit()
    
    def test_right_click_context_menu(driver):
        driver.get('http://example.com')
        button = driver.find_element(By.ID, 'menu-button')
        actions = ActionChains(driver)
        actions.context_click(button).perform()
        wait = WebDriverWait(driver, 10)
        assert wait.until(EC.visibility_of_element_located((By.ID, 'context-menu')))
    D
    import pytest
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.action_chains import ActionChains
    
    def setup_module():
        global driver
        driver = webdriver.Chrome()
    
    def teardown_module():
        driver.quit()
    
    def test_right_click_context_menu():
        driver.get('http://example.com')
        button = driver.find_element(By.ID, 'menu-button')
        actions = ActionChains(driver)
        actions.context_click(button).perform()
        assert driver.find_element(By.ID, 'context-menu').is_displayed()
    Attempts:
    2 left
    💡 Hint
    Use pytest fixtures for setup and teardown to manage the driver lifecycle cleanly.