繁体   English   中英

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

[英]how to use while loop to repeat task in 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名称后,它要求添加更多内容,然后按“ ”,我们必须输入其他数据。 但是在此之前,我们必须执行以前的输入数据..这就是我要解决的问题。

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

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

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语句退出该循环。

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

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

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

暂无
暂无

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

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