Bird
Raised Fist0
Selenium Pythontesting~10 mins

First Selenium script in Selenium Python - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to open a Chrome browser using Selenium.

Selenium Python
from selenium import webdriver

driver = webdriver.[1]()
driver.get('https://example.com')
driver.quit()
Drag options to blanks, or click blank then click option'
AChrome
BFirefox
CSafari
DEdge
Attempts:
3 left
💡 Hint
Common Mistakes
Using a browser name that is not installed or supported.
Forgetting to capitalize the browser name.
2fill in blank
medium

Complete the code to find an element by its ID.

Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get('https://example.com')
element = driver.find_element(By.[1], 'submit-button')
driver.quit()
Drag options to blanks, or click blank then click option'
AID
BTAG_NAME
CCLASS_NAME
DNAME
Attempts:
3 left
💡 Hint
Common Mistakes
Using By.NAME when the element has an ID.
Using lowercase instead of uppercase for By.ID.
3fill in blank
hard

Fix the error in the code to click a button after finding it.

Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get('https://example.com')
button = driver.find_element(By.ID, 'submit')
button.[1]()
driver.quit()
Drag options to blanks, or click blank then click option'
Asubmit
Bclick
Csend_keys
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using submit() on a button element that is not a form.
Using send_keys() which is for typing text.
4fill in blank
hard

Fill both blanks to wait until an element is visible before clicking it.

Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get('https://example.com')
wait = WebDriverWait(driver, [1])
element = wait.until(EC.[2]((By.ID, 'submit')))
element.click()
driver.quit()
Drag options to blanks, or click blank then click option'
A10
B5
Cvisibility_of_element_located
Delement_to_be_clickable
Attempts:
3 left
💡 Hint
Common Mistakes
Using too long or zero wait time.
Using visibility_of_element_located when the element is not clickable yet.
5fill in blank
hard

Fill all three blanks to create a dictionary of element texts for all buttons with class 'btn'.

Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get('https://example.com')
buttons = driver.find_elements(By.[1], 'btn')
texts = [2]: button.[3] for button in buttons
print(texts)
driver.quit()
Drag options to blanks, or click blank then click option'
ACLASS_NAME
Bbutton.text
Ctext
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using find_element instead of find_elements.
Calling text() as a method instead of property.

Practice

(1/5)
1. What is the main purpose of a Selenium script in software testing?
easy
A. To automate browser actions for testing websites
B. To write manual test cases on paper
C. To design website layouts
D. To create databases for applications

Solution

  1. Step 1: Understand Selenium's role

    Selenium is a tool used to control browsers automatically for testing purposes.
  2. Step 2: Match purpose with options

    Only To automate browser actions for testing websites describes automating browser actions to test websites, which is Selenium's main use.
  3. Final Answer:

    To automate browser actions for testing websites -> Option A
  4. Quick Check:

    Selenium automates browsers = B [OK]
Hint: Selenium controls browsers automatically for testing [OK]
Common Mistakes:
  • Confusing Selenium with manual testing
  • Thinking Selenium designs websites
  • Mixing Selenium with database creation
2. Which of the following is the correct way to open a Chrome browser using Selenium in Python?
easy
A. driver = webdriver.open('chrome')
B. driver = webdriver.Firefox()
C. driver = webdriver.Chrome()
D. driver = webdriver.start('chrome')

Solution

  1. Step 1: Recall Selenium syntax for Chrome

    In Selenium Python, opening Chrome uses webdriver.Chrome() constructor.
  2. Step 2: Check options for correct syntax

    Only driver = webdriver.Chrome() uses the correct constructor. Others are either for different browsers or invalid methods.
  3. Final Answer:

    driver = webdriver.Chrome() -> Option C
  4. Quick Check:

    Open Chrome with webdriver.Chrome() = C [OK]
Hint: Use webdriver.Chrome() to open Chrome browser [OK]
Common Mistakes:
  • Using webdriver.Firefox() for Chrome
  • Calling non-existent methods like open() or start()
  • Missing parentheses after Chrome
3. What will be the output of this Selenium Python code snippet?
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get('https://example.com')
element = driver.find_element(By.TAG_NAME, 'h1')
print(element.text)
driver.quit()
medium
A. Prints nothing because element.text is empty
B. Throws an error because By.TAG_NAME is invalid
C. Prints the URL 'https://example.com'
D. Prints the text inside the first <h1> tag on example.com

Solution

  1. Step 1: Understand code actions

    The code opens Chrome, navigates to example.com, finds the first <h1> element, prints its text, then closes the browser.
  2. Step 2: Analyze expected output

    example.com has an <h1> with text 'Example Domain', so element.text prints that text.
  3. Final Answer:

    Prints the text inside the first <h1> tag on example.com -> Option D
  4. Quick Check:

    find_element(By.TAG_NAME, 'h1') gets <h1> text = D [OK]
Hint: find_element with By.TAG_NAME gets first matching tag text [OK]
Common Mistakes:
  • Thinking By.TAG_NAME is invalid
  • Expecting URL printed instead of element text
  • Assuming element.text is empty without checking page
4. Identify the error in this Selenium Python script:
from selenium import webdriver

driver = webdriver.Chrome
driver.get('https://example.com')
driver.quit()
medium
A. Missing parentheses after webdriver.Chrome to create driver instance
B. Incorrect URL format in driver.get() method
C. driver.quit() is called before opening browser
D. Indentation error on driver.get() line is invalid in Python

Solution

  1. Step 1: Check webdriver.Chrome usage

    webdriver.Chrome is a class; to create a driver object, parentheses are needed: webdriver.Chrome()
  2. Step 2: Review other lines for errors

    URL format is correct, driver.quit() is after get().
  3. Step 3: Determine main error

    Missing parentheses causes driver to be a class, not an instance, so driver.get() will fail.
  4. Final Answer:

    Missing parentheses after webdriver.Chrome to create driver instance -> Option A
  5. Quick Check:

    webdriver.Chrome() needs parentheses = A [OK]
Hint: Always add () after webdriver.Chrome to create driver [OK]
Common Mistakes:
  • Forgetting parentheses after webdriver.Chrome
  • Thinking URL format is wrong
  • Confusing the order of driver.get() and driver.quit()
5. You want to write a Selenium script that opens Chrome, goes to 'https://example.com', checks if the page title is 'Example Domain', and then closes the browser. Which code snippet correctly does this with an assertion?
hard
A. from selenium import webdriver driver = webdriver.Chrome() driver.get('https://example.com') assert driver.title = 'Example Domain' driver.quit()
B. from selenium import webdriver driver = webdriver.Chrome() driver.get('https://example.com') assert driver.title == 'Example Domain' driver.quit()
C. from selenium import webdriver driver = webdriver.Chrome() driver.get('https://example.com') assert driver.title != 'Example Domain' driver.quit()
D. from selenium import webdriver driver = webdriver.Chrome() driver.get('https://example.com') assert driver.getTitle() == 'Example Domain' driver.quit()

Solution

  1. Step 1: Check correct assertion syntax

    In Python, assert uses '==' to compare values, so 'assert driver.title == "Example Domain"' is correct.
  2. Step 2: Verify method and property names

    driver.title is the correct property for page title; driver.getTitle() is incorrect in Python Selenium.
  3. Step 3: Confirm logic correctness

    from selenium import webdriver driver = webdriver.Chrome() driver.get('https://example.com') assert driver.title == 'Example Domain' driver.quit() asserts title equals 'Example Domain', which matches the requirement.
  4. Final Answer:

    Correct assertion with driver.title == 'Example Domain' -> Option B
  5. Quick Check:

    Use assert driver.title == 'Example Domain' = A [OK]
Hint: Use assert with '==' and driver.title property [OK]
Common Mistakes:
  • Using single '=' instead of '==' in assert
  • Using driver.getTitle() instead of driver.title
  • Asserting title not equal instead of equal
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard