You have a login button with HTML: <button id='loginBtn' class='btn primary'>Login</button>. Which locator is the most reliable and efficient to use in a Selenium page class?
Think about uniqueness and speed of locating elements.
Using By.ID is the fastest and most reliable because IDs are unique on the page. Class names or CSS selectors might match multiple elements, and XPath is slower.
You want to check if the page title is exactly 'Welcome Page' after login. Which assertion is correct in a Python unittest test case?
Check which assertion method compares values for equality properly.
assertEqual checks if two values are exactly equal. assertTrue works but is less clear. assertIn only checks substring presence. assertIs checks object identity, not value equality.
Given the following page class snippet, what will print(len(page.buttons)) output if the page has 3 buttons with class 'btn'?
from selenium.webdriver.common.by import By class Page: def __init__(self, driver): self.driver = driver @property def buttons(self): return self.driver.find_elements(By.CLASS_NAME, 'btn') # Assume driver is a Selenium WebDriver instance with 3 buttons having class 'btn' page = Page(driver) print(len(page.buttons))
Remember that find_elements returns a list of matching elements.
find_elements returns a list of all matching elements. Since there are 3 buttons with class 'btn', the length is 3. It does not raise an exception if no elements are found; it returns an empty list.
What is wrong with this locator in the page class?
from selenium.webdriver.common.by import By class LoginPage: def __init__(self, driver): self.driver = driver username_input = (By.ID, 'username') def enter_username(self, name): self.driver.find_element(*self.username_input).send_keys(name)
Check how find_element is called with locators.
find_element requires two arguments: locator strategy and locator value. Passing a tuple as a single argument causes a TypeError. The correct call is find_element(*self.username_input) to unpack the tuple.
In a Selenium test suite using Python, which feature of the pytest framework helps manage page class locators efficiently and promotes clean test code?
Think about how to reuse page objects across tests.
Pytest fixtures allow setup code to create and provide page objects with locators to tests, promoting reuse and clean code. Parametrize is for data-driven tests, raises is for exception testing, skip is for ignoring tests.