简体   繁体   English

如何使用python和selenium将数据从列表输入网站?

[英]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. 我是编码方面的初学者,我想使用python和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”,如果数据格式如下:

  • example@email.com:passwordexample example@email.com:passwordexample
  • example2@email.com:passwordexample2 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). 同样,不确定“列表”是什么意思(似乎不是在指代实际的python列表)。 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 假设“ example@email.com:passwordexample”为字符串形式,您可以使用split()方法实现它,但不清楚

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. -首先是使用username:password格式的文件。 I made 2 other seperate text files called for example, username.txt and password.txt 我还制作了另外两个单独的文本文件,例如username.txt和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 所以用户名将是rea_username和密码real_password,我像这样使用它们

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python + Selenium从网站保存数据 - How to save data from website by using python + selenium 如何使用python selenium从网站获取数据 - How to get data from a website using python selenium 如何使用 selenium 从网站抓取数据 - How to scrape data from website using selenium 如何使用 selenium、python 列出网站中的所有可点击链接? - How to list all clickable links in a website using selenium, python? Python selenium 如何从网站上抓取值列表 - Python selenium how to scrape list of values from website 在python中使用硒从动态网站获取数据:如何发现数据库查询的完成方式? - Using selenium in python to get data from dynamic website: how to discover the way databases querys are done? 如何使用硒python一步一步单击以从网站获取数据 - How to click one by one to get data from website by using selenium python 如何在python中使用硒和beautifulsoup从网站上抓报纸? - How to scrape newspaper articles from website using selenium and beautifulsoup in python? 如何使用 Python + Selenium 从网站下载特定文件 - How to download specific files from a website using Python + Selenium 如何在 Python 3 中使用 Selenium 从网站的某个部分获取文本 - How to get text from a section of a website using Selenium in Python 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM