如何遍历列表中的各个行并使用表单输入?蟒蛇

时间:2017-04-30 21:46:32

标签: python selenium

我有以下代码:

from selenium import webdriver

#My list of [5,n] dimensional values 
ls = [ [3,10,19,20,25], [4,5,6,10,12]... [n] ]


#This is a website form that takes 5 values as input
element = driver.find_element_by_name("n1")
element1 = driver.find_element_by_name("n2")
element2 = driver.find_element_by_name("n3")
element3 = driver.find_element_by_name("n4")
element4 = driver.find_element_by_name("n5")

#After I find and store elements, I want to send 5 values from the list         
element.send_keys(str(ls[0]))
element1.send_keys(str(ls[1]))
element2.send_keys(str(ls[2]))
element3.send_keys(str(ls[3]))
element4.send_keys(str(ls[4]))

#Next I will submit form. Then loop again to retrieve next row of values 
#into form.

目前我只能从值列表中选择一行。我希望能够创建一个for循环,允许我在程序运行后选择下一行值。我发现嵌套循环可能会有所帮助,但不确定如何使其工作。

更新

这是我想要完成的一部分。我尝试了一些作为反馈提供的代码,但循环不能正常工作。

from selenium import webdriver


ls = [[1,5,10,16,19], [3,10,19,20,25], [1,2,11,16,29]]
for iter in range(3):
   driver = webdriver.Chrome('chromedriver.exe')

   driver.get("http://lottery.com/site/winningNumberSearch?")

   select = Select(driver.find_element_by_name('searchTypeIn'))
   select.select_by_visible_text("Number")
   select = Select(driver.find_element_by_name('gameNameIn'))
   select.select_by_visible_text("Fantasy 5")

   elements = [driver.find_element_by_name("n" + str(i) + "In") for i in 
   range(1,6)]

   for values in ls:
       if len(elements) != len(values):
           #if theres an error, do something
            pass
       for element, value in zip(elements, values):
           element.send_keys(str(value))
           driver.find_element_by_id("submit").click()
   driver.quit()

我让它像这样工作

driver = webdriver.Chrome()

driver.get("http://lottery.com/site/winningNumberSearch?")

select = Select(driver.find_element_by_name("searchTypeIn"))
select.select_by_visible_text("Number")
select = Select(driver.find_element_by_name('gameNameIn'))
select.select_by_visible_text("Fantasy 5")

elements = [driver.find_element_by_name("n" + str(i) + "In") for i in 
range(1, 6)]



for k in ls:
    if len(elements) != len(k):
        pass
    for j, l in zip(elements, k):
        j.send_keys(str(l))
        #sleep(1)

    sleep(1)    
    driver.find_element_by_id("submit").click()
    sleep(1)
    driver.get("http://flalottery.com/site/winningNumberSearch?")
    sleep(1)
    select = Select(driver.find_element_by_name("searchTypeIn"))
    select.select_by_visible_text("Number")
    select = Select(driver.find_element_by_name('gameNameIn'))
    select.select_by_visible_text("Fantasy 5")
    elements = [driver.find_element_by_name("n" + str(i) + "In") for i in 
    range(1, 6)]

不完美,但它为我工作。非常感谢。

3 个答案:

答案 0 :(得分:0)

ls是列表清单。我想以下就是你需要的

element.send_keys(str(ls[0][0]))
element1.send_keys(str(ls[0][1]))
element2.send_keys(str(ls[0][2]))
element3.send_keys(str(ls[0][3]))
element4.send_keys(str(ls[0][4]))

以下是您可以使用的循环示例:

for values in ls:
   for i in range(len(values)):
       print(values[i])

答案 1 :(得分:0)

您希望将元素作为元素[1]来处理,而不是元素1:

elements = [driver.find_element_by_name("n" + str(i)) for i in range(1, 6)]

for values in ls:
    if len(elements) != len(values):
        # if you consider this an error, do something
        pass
    for element, value in zip(elements, values):
        element.send_keys(str(value))

    submit_form()

答案 2 :(得分:0)

您可以使用循环

for values in ls:
    for i in range(1, len(values) + 1):
        element = driver.find_element_by_name("n{}".format(i))
        element.send_keys(str(values[i-1]))