简体   繁体   中英

How to type data from list to a website using python and selenium?

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("example@email.com")
inputElement = driver.find_element_by_xpath('//*[@id="password"]')
inputElement.send_keys("passwordexample")

Im trying to replace "example@email.com" "passwordexample" with data from the list list, if the data format is something like:

  • example@email.com:passwordexample
  • example2@email.com:passwordexample2

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

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 = {example@email.com: passwordexample, 
           example2@email.com: passwordexample2,
           example3@email.com: 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

no clarity from where this 'example@email.com:passwordexample' comes from assuming it is as string you can achieve it usng split() method

data = "example@email.com: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 example@email.com
inputElement.send_keys("datalist[0]")

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

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM