-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?

Mohamed Darwesh
  • 659
  • 4
  • 17

1 Answers1

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.
Mojtaba
  • 583
  • 1
  • 5
  • 15
  • 1
    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 – Mohamed Darwesh Aug 31 '22 at 17:48