如何使用while循环在硒中重复执行任务

时间:2019-01-12 13:08:08

标签: python selenium

我很容易理解。我正在尝试在此处运行while循环以继续添加更多数据的内容。但不幸的是我想不出办法!

while (True):
    id = input("Enter ID: ")
    name = input("Enter Name: ")
    break;

    def start(k):
        # Selenium Driver Code Here
        file_id = id(k)
        file_name = name(k)

print("ID: ", id)
print("Name: ", name)

while (True):
    restart = input("Add More (y/n): ")
    if restart == "yes" or restart == "y":
        print("Restarting")
    elif restart == "no" or restart == "no":
        print("Not Restarting");

调用该函数后,它会使用先前输入的输入再次重新启动。.我尝试在 start(k)之前使用 driver.quit(),但没有希望!在基础知识上有些困惑。

while (True):
     restart = input("Do you have another file (y/n): ")
     if restart == "yes" or restart == "y":
         start(k)
     else: restart == "no" or restart == "n"
         break;

我完整的代码结构

keys = []
while (True):
    id = input("Enter ID: ")
    name = input("Enter Name: ")
    restart = input("\nAdd More (y/n): ")
    if restart == "yes" or restart == "y":
        print("Restarting")
        continue
    elif restart == "no" or restart == "n":
        print("Not Restarting")
        break;
keys = {
     "id": file_id,
     "name": file_name
}
def start(k):
 chrome_options = webdriver.ChromeOptions()
 chrome_options.add_argument("")
 ....
 ....
 ....
if __name__ == '__main__':
    start(keys)

输入我的 id 名称后,它要求添加更多内容,然后按“ ”,我们必须输入其他数据。但是在此之前,我们必须执行以前的输入数据。这就是我要解决的问题。

现在,它实际上实际上忽略了我们的第一个输入数据,仅执行了最后一个输入。

2 个答案:

答案 0 :(得分:0)

您似乎使逻辑过于复杂。就这么简单:

while True:
    ID = input("Enter ID: ")
    name = input("Enter name: ")


    print("ID: ", ID)
    print("Name: ", name)   

    restart = input("\nAdd More (y/n): ")
    if restart == "yes" or restart == "y":
        print("Restarting")
        continue
    elif restart == "no" or restart == "n":
        print("Not Restarting")
        break

continue语句再次启动while循环,而break语句退出该循环。

答案 1 :(得分:0)

您可以通过其他代码完成您想做的事情,尝试这种简单有效的方法:

#create a list that store the name and list for ID
names = []
ids = []

#create flag that tell me if user won't enter any other data or he want to
#enter more data
flag = True

#while loop take a flag and will be in the loop while flag is true
while(flag):

    #take the name from user and store it
    names.append(input("Enter The Name: "))

    #Take The ID
    ids.append(input("Enter The ID:"))

    #ask user if he want to continue
    ask = input("Do you want to continue : ")
    if(ask.lower() == 'no' or ask.lower() == 'n') :
        flag = False

#at the end of the loop print the data that were stored in name list and id list
print("Names Is " , names)
print("IDs Is " , ids)

您现在已经存储了可以对数据进行任何操作的数据