繁体   English   中英

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

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

我是编码方面的初学者,我想使用python和selenium将数据从列表输入到特定网站,并跟踪该数据。 当前使用的代码是>

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

我试图用列表中的数据替换“ example@email.com”“ passwordexample”,如果数据格式如下:

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

我不知道如何制作该列表以及如何从该列表中逐行获取数据

我猜想您的意思是您想为电子邮件和密码创建存储(在本例中为字典)。

my_dict = {example@email.com: passwordexample, 
           example2@email.com: passwordexample2,
           example3@email.com: passwordexample3}

同样,不确定“列表”是什么意思(似乎不是在指代实际的python列表)。 同样也不确定“获取数据”的含义。

您还将要更改用户名或密码框的变量名。 所以也许更像这样:

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

假设“ 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]")

我如何解决这个问题? -首先是使用username:password格式的文件。 我还制作了另外两个单独的文本文件,例如username.txt和password.txt

然后我使用正则表达式对一行中的单词进行计数,然后使用计数值在需要的地方分割文本,因为我的文本可能像这样:

  • 用户名密码
  • 用户名密码
  • 用户名密码

等等。 由于密码在单词之间永远不会有空格,因此最后一个单词始终是密码。

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

是的,也许可以用一种更简单的方法来完成,但是我四天前开始讲授代码,这就是我可以立即提出的想法。

所以用户名将是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.

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