-2

So I'm using Python and I have a JSON file that has usernames and passwords in the following format

{
"username": {
    "0": "user1",
    "1": "user2",
    },
"password": {
    "0": "user1pass",
    "1": "user2pass",
    }
}

I'm trying to import credentials from the JSON file into Selenium

username = driver.find_element(By.NAME, "username")
password = driver.find_element(By.NAME, "password")
username.send_keys("test")
password.send_keys("test")

so for example it would pull data of User 1 from the JSON file and pass it into the username field, login then do smth,, then log out and pull the second user etc

I want to do a loop that can extract data from JSON and also work with Selenium to use the data and go to next data etc..not just pulling the data.

What should I be looking for exactly?

6
  • Is it JSON or Python dict? Commented Aug 31, 2022 at 16:32
  • @JaSON it's a JSON dictionary Commented Aug 31, 2022 at 16:37
  • I'm not sure what JSON dictionary is...) but I hope you already convert JSON to dict Commented Aug 31, 2022 at 16:42
  • @JaSON sorry I meant it's just a JSON file yes Commented Aug 31, 2022 at 17:06
  • Does this answer your question? Extract part of data from JSON file with python Commented Aug 31, 2022 at 17:41

1 Answer 1

1

You need to have a loop of numbers which then in each iteration finds the login form and using loop variable chooses username and password then do things that you want.

credentials_dict = {
"username": {
    "0": "user1",
    "1": "user2",
},
"password": {
    "0": "user1pass",
    "1": "user2pass",
}
for i in range(0,2):
    username = driver.find_element(By.NAME, "username")
    password = driver.find_element(By.NAME, "password")
    username.send_keys(credentials_dict['username'][str(i)])
    password.send_keys(credentials_dict['password'][str(i)])
    # do things that you want to do.
Sign up to request clarification or add additional context in comments.

1 Comment

This solved it, except that it's JSON file not a python dictionary but I got the idea now how to work with the loop thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.