Bird
0
0

You want to use JSON test data to fill a login form in Selenium Python. The JSON file login.json contains:

hard📝 Application Q15 of 15
Selenium Python - Data-Driven Testing
You want to use JSON test data to fill a login form in Selenium Python. The JSON file login.json contains:
{"username": "user1", "password": "pass1"}

Which code snippet correctly reads the JSON and uses Selenium to fill the form fields with ids user and pass?
Aimport json with open('login.json') as f: data = json.load(f) driver.find_element_by_name('user').send_keys(data['username']) driver.find_element_by_name('pass').send_keys(data['password'])
Bimport json with open('login.json') as f: data = json.load(f) driver.find_element('id', 'user').send_keys(data['username']) driver.find_element('id', 'pass').send_keys(data['password'])
Cimport json with open('login.json') as f: data = json.loads(f) driver.find_element_by_id('user').send_keys(data['username']) driver.find_element_by_id('pass').send_keys(data['password'])
Dimport json with open('login.json') as f: data = json.load(f) driver.find_element('name', 'user').send_keys(data['username']) driver.find_element('name', 'pass').send_keys(data['password'])
Step-by-Step Solution
Solution:
  1. Step 1: Correctly read JSON file

    json.load(f) reads JSON from file object correctly; json.loads() expects a string, so import json with open('login.json') as f: data = json.loads(f) driver.find_element_by_id('user').send_keys(data['username']) driver.find_element_by_id('pass').send_keys(data['password']) is wrong.
  2. Step 2: Use correct Selenium locator method

    driver.find_element('id', 'user') is the modern syntax to find element by id; find_element_by_id() is deprecated.
  3. Step 3: Match locators with element ids

    The form fields have ids 'user' and 'pass', so locating by id is correct, not by name.
  4. Final Answer:

    import json with open('login.json') as f: data = json.load(f) driver.find_element('id', 'user').send_keys(data['username']) driver.find_element('id', 'pass').send_keys(data['password']) -> Option B
  5. Quick Check:

    json.load + find_element('id', ...) = correct usage [OK]
Quick Trick: Use json.load() and find_element('id', ...) for form filling [OK]
Common Mistakes:
  • Using json.loads() on file object
  • Using deprecated find_element_by_id()
  • Using wrong locator type (name instead of id)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Python Quizzes