Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to take a screenshot using Selenium in Python.
Selenium Python
driver = webdriver.Chrome() driver.get('https://example.com') driver.save_screenshot('[1]') driver.quit()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unsupported image formats like .gif or .bmp
Forgetting to provide a filename
Using incorrect method names
✗ Incorrect
The save_screenshot method saves the screenshot to the given filename. Using 'fullpage.png' is a common and correct filename for a PNG screenshot.
2fill in blank
mediumComplete the code to scroll the page before taking a screenshot.
Selenium Python
driver = webdriver.Chrome() driver.get('https://example.com') driver.execute_script('[1]') driver.save_screenshot('fullpage.png') driver.quit()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Scrolling to the top instead of bottom
Using negative scroll values incorrectly
Not scrolling at all before screenshot
✗ Incorrect
Scrolling to the bottom of the page with 'window.scrollTo(0, document.body.scrollHeight)' ensures the entire page is loaded before taking the screenshot.
3fill in blank
hardFix the error in the code to correctly take a screenshot using Selenium in Python.
Selenium Python
driver = webdriver.Chrome() driver.get('https://example.com') full_screenshot = driver.get_screenshot_as_file('[1]') print('Screenshot saved:', full_screenshot) driver.quit()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unsupported file extensions
Confusing method names for screenshots
Not checking the return value of the screenshot method
✗ Incorrect
The get_screenshot_as_file method requires a valid image filename with a supported extension like .png. 'fullpage.png' is correct.
4fill in blank
hardFill both blanks to create a dictionary comprehension that stores element IDs and their text content for all elements with class 'item'.
Selenium Python
elements = driver.find_elements(By.CLASS_NAME, '[1]') texts = {element.get_attribute('[2]'): element.text for element in elements}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong attribute names like 'class' or 'name'
Mixing up class name and id attribute
Not using dictionary comprehension syntax correctly
✗ Incorrect
We find elements by class name 'item' and get their 'id' attribute as keys in the dictionary with their text as values.
5fill in blank
hardFill all three blanks to filter elements with text length greater than 5 and save their IDs and texts in a dictionary.
Selenium Python
elements = driver.find_elements(By.CLASS_NAME, '[1]') filtered = {element.get_attribute('[2]'): element.text for element in elements if len(element.text) [3] 5}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators
Using wrong attribute names
Not filtering elements correctly
✗ Incorrect
We find elements with class 'item', get their 'id' attribute, and filter those whose text length is greater than 5.