0

Im a complete beginner in coding and i want to enter data to a specific website from a list and keep track of that data, using python and selenium. Current code that is used is>

inputElement = driver.find_element_by_xpath('//*[@id="username"]')
inputElement.send_keys("[email protected]")
inputElement = driver.find_element_by_xpath('//*[@id="password"]')
inputElement.send_keys("passwordexample")

Im trying to replace "[email protected]" "passwordexample" with data from the list list, if the data format is something like:

I dont know how to make that list and how to take data line by line from that list one by one

2
  • is the q answered? Commented Oct 28, 2018 at 5:50
  • 1
    Yeah i got it working but in a different way. Commented Oct 28, 2018 at 7:07

3 Answers 3

2

I'm going to guess that you mean that you want to create a storage (in this case a dictionary) for the e-mails and passwords.

my_dict = {[email protected]: passwordexample, 
           [email protected]: passwordexample2,
           [email protected]: passwordexample3}

Again, not sure what you mean by "list" (doesn't seem like you are referring to an actual python list). Also not sure what you mean by "take data."

You are also going to want to change the variable name for either the username or password box. So maybe more like this:

inputElement_user = driver.find_element_by_xpath('//*[@id="username"]')
inputElement_pass = driver.find_element_by_xpath('//*[@id="password"]')
# Loop through every user & pass and do what you wish
for k, v in my_dict.items():
    inputElement_user.send_keys(k)
    inputElement_pass.send_keys(v)
    # Insert whatever else you want to do on the page
Sign up to request clarification or add additional context in comments.

Comments

1

no clarity from where this '[email protected]:passwordexample' comes from assuming it is as string you can achieve it usng split() method

data = "[email protected]:passwordexample"

#seperate values using : seperator from the string
datalist = data.split(':')

inputElement = driver.find_element_by_xpath('//*[@id="username"]')
#use index on list to get required value datalist[0] will give you [email protected]
inputElement.send_keys("datalist[0]")

inputElement = driver.find_element_by_xpath('//*[@id="password"]')
#datalist[1] will give you passwordexample
inputElement.send_keys("datalist[1]")

2 Comments

Yes that data comes is just a string atm and its for testing purposes atm. Lets say i do have 1000 lines of data to input. I could make an variable for data and split it the way u just showed, but it would be clearly too time consuming to index every piece of data manually.
@syinncc use for loop
0

How did i solve this problem? - First there is file with username:password format. I made 2 other seperate text files called for example, username.txt and password.txt

Then i used regex to count words in a line, then using counted value to split the text where i wanted, since my text could be something like :

  • user name:password
  • us er name:password
  • us_er.name:password

and so on. Since password never has any space between words, always last word is password.

import regex

lineinfo = open("allaccounts.txt", "r")

username = open("username.txt", "w")
password = open("password.txt", "w")
info = lineinfo.readline()
info2 = info.replace(':', ' ')
count = len(regex.findall(r'\S+', info2))
if count == 2:
    info3 = info2.split()[0]
    username.write(info3)
    info4 = info2.split()[1]
    password.write(info4)

if count == 3:
    info3 = info2.split()[0]
    info5 = info2.split()[1]
    username.write(info3 + " " + info5)
    info4 = info2.split()[2]
    password.write(info4)

username.close() and password.close()
username = open("username.txt", "r")
password = open("password.txt", "r")
real_username = username.readline()
real_password = password.readline()
username.close()
password.close()

Yeah maybe this can be done in a much easier way but i started learining code like 4 days ago and this is what i could come up in the spot.

so the username will be rea_username and password real_password and i used them like

inputElement = driver.find_element_by_xpath('//*[@id="username"]')
inputElement.send_keys("real_username")
inputElement = driver.find_element_by_xpath('//*[@id="password"]')
inputElement.send_keys("real_password")

1 Comment

If you don't do this replace - replace(':', ' '), you can do the split on the colon - split(':'), and the first element is going to be the user, and the second - the pass. The regex is also not going to be needed.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.