A. Change 'sendkeys' to 'send_keys' to fix the method call
B. Replace 'By.NAME' with 'By.ID' to locate the element correctly
C. Add 'self' parameter to the method definition
D. Use 'find_elements' instead of 'find_element' to get multiple elements
Solution
Step 1: Check method call spelling
The correct Selenium method to send text is 'send_keys', not 'sendkeys'. This typo causes an AttributeError.
Step 2: Verify other parts of the method
The method has self parameter and uses By.NAME correctly, so no changes needed there.
Final Answer:
Change 'sendkeys' to 'send_keys' to fix the method call -> Option A
Quick Check:
Correct method name is send_keys [OK]
Hint: Selenium method is send_keys, not sendkeys [OK]
Common Mistakes:
Misspelling send_keys method
Changing locator type unnecessarily
Removing self parameter incorrectly
5. You want to create an action method in your page class that selects an option from a dropdown by visible text. Which of the following is the best implementation?
hard
A. def select_option(self, text):
self.driver.find_element(By.ID, 'menu').click()
self.driver.find_element(By.XPATH, f'//option[text()="{text}"]').click()
B. def select_option(self, text):
dropdown = self.driver.find_element(By.ID, 'menu')
dropdown.select_by_visible_text(text)
C. def select_option(self, text):
dropdown = Select(self.driver.find_element(By.ID, 'menu'))
dropdown.select_by_visible_text(text)
D. def select_option(self, text):
self.driver.find_element(By.ID, 'menu').send_keys(text)
Solution
Step 1: Understand how to handle dropdowns in Selenium Python
Selenium provides a Select class to handle dropdowns, which must be initialized with the dropdown element.
Step 2: Check each option for correct usage
def select_option(self, text):
dropdown = Select(self.driver.find_element(By.ID, 'menu'))
dropdown.select_by_visible_text(text) correctly creates a Select object and calls select_by_visible_text. def select_option(self, text):
dropdown = self.driver.find_element(By.ID, 'menu')
dropdown.select_by_visible_text(text) tries to call select_by_visible_text directly on a WebElement, which is invalid. def select_option(self, text):
self.driver.find_element(By.ID, 'menu').click()
self.driver.find_element(By.XPATH, f'//option[text()="{text}"]').click() manually clicks options but is less clean. def select_option(self, text):
self.driver.find_element(By.ID, 'menu').send_keys(text) uses send_keys which may not work reliably.