【问题标题】:looping through several columns and rows from csv to fill a form循环遍历 csv 中的多列和多行以填写表格
【发布时间】:2021-04-03 15:49:33
【问题描述】:

一直在尝试模仿之前发布的示例,但被卡住了。

我有一个简单的网络表单:姓氏、姓名、电子邮件、密码、确认密码。

还有一个 .csv,有 4 列,对应于表单

Last name         Name             Email   Password
0    Brown        Stan     brown@stan.com   12345678
1    White        Eagle   white@eagle.com  123456789
2    Dante        Aligr     adant@mail.au   98765432 

所以,我只想将 3 个条目输入表单,然后在每个条目后单击“发送”。

我从这里复制了一个似乎通过但我不断收到的代码


  File "C:\Users\untitled2.py", line 43, in <module>
    last.send_keys(lname)

AttributeError: 'list' object has no attribute 'send_keys' 

我尝试过的代码

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

options = Options()
options.binary_location = FirefoxBinary(r"C:\Program Files\Mozilla Firefox\firefox.exe")

driver = webdriver.Firefox(executable_path=r'C:\WebDriver\bin\geckodriver.exe', firefox_options=options)

import time
import pandas as pd

reg = pd.read_csv('Form.csv', header=0, delimiter=';', sep=r'\s*;\s*')

print(reg)

lname = reg['Last name'].tolist()
name = reg['Name'].tolist()
mail = reg['Email'].tolist()
password = reg['Password'].tolist()
password_c = reg['Password'].tolist()


driver.get('url')

first = driver.find_elements_by_xpath("/html/body/div/div[3]/form/div[2]/div/div[2]/div[2]/div/div/div[2]/div/div[1]/div/div[1]/input")
last = driver.find_elements_by_xpath("/html/body/div/div[3]/form/div[2]/div/div[2]/div[1]/div/div/div[2]/div/div[1]/div/div[1]/input")
mail =  driver.find_elements_by_xpath("/html/body/div/div[3]/form/div[2]/div/div[2]/div[3]/div/div/div[2]/div/div[1]/div/div[1]/input")
password =  driver.find_elements_by_xpath("/html/body/div/div[3]/form/div[2]/div/div[2]/div[4]/div/div/div[2]/div/div[1]/div/div[1]/input")
password_confirm = driver.find_elements_by_xpath("/html/body/div/div[3]/form/div[2]/div/div[2]/div[4]/div/div/div[2]/div/div[1]/div/div[1]/input")

submit = driver.find_elements_by_xpath('/html/body/div/div[3]/form/div[2]/div/div[3]/div[1]/div/div/span/span')

for lname, name, mail, password, password_c in zip(lname, name, mail, password, password_c):
    
    last.send_keys(lname)
    time.sleep(1)
    
    first.send_keys(name)
    time.sleep(1)
    
    mail.send_keys(mail)
    time.sleep(1)
    
    password.send_keys(password)
    time.sleep(1)
    
    password_confirm.send_keys(password_c)
    time.sleep(1)
    
    submit.click()
    time.sleep(3)

任何向正确方向轻推都会受到高度赞赏,因为我已经看到很多使用 send_keys() 的示例

谢谢!

【问题讨论】:

    标签: python pandas selenium


    【解决方案1】:

    错误消息表明您正在使用 send_keys() 和普通 python lists。

    根据Selenium docsfind_elements_by_xpath 确实返回了一个列表。

    您可能打算使用find_element_by_xpath(不带“s”后的元素)?

    【讨论】:

    • 好吧,事实上它在某种程度上起作用了——它可以解决列表问题。另外我发现了一些错误 -
      ``` mail = reg['Email'].tolist() password = reg['Password'].tolist() ```需要是
      ``` mail_ = reg['Email'].tolist() password_ = reg['Password'].tolist()
    • 现在我开始自动化 = 关闭。如果我得到一个,将上传解决方案
    【解决方案2】:

    无论如何,

    执行部分应该是这样的

    for lnames in lname:
        count = 0
    
    for names in name:
        count_name = 0
    
    for mails in mail_:
        count_mail = 0
    
    for value in last:
        value.send_keys(lname[count])
        count +=1 
       
    for x in mail:
        x.send_keys(mail_[count_mail])
        count_mail +=1 
        
    for y in first:
        y.send_keys(name[count_name])
        count_name +=1
                                                                                                                                                                                             
    submit = driver.find_element_by_xpath('//*[@id="mG61Hd"]/div[2]/div/div[3]/div[1]/div/div/span/span')
    submit.click()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-14
      • 1970-01-01
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多