简体   繁体   English

如何让 Python 回到要求输入的状态

[英]How to make Python go back to asking for input

So I want the program to go back to asking for the input once it has completed.所以我希望程序在完成后返回要求输入。

I've asked this in reddit and gone through quite a many similar threads here and so far the answer seems to be loops if true perform x.我在 reddit 上问过这个问题,并在这里浏览了很多类似的线程,到目前为止,如果 true 执行 x,答案似乎是循环。 But what is the command for the program to go back to asking for the input on line 5?但是程序返回到第 5 行要求输入的命令是什么?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep

card = input()

driver = webdriver.Firefox()
driver.get("http://www.mtgprice.com/")
elem = driver.find_element_by_xpath("//form/input")
elem.send_keys(card) # input
driver.implicitly_wait(5) # seconds
driver.find_element_by_class_name('btn-blue').click()

Everyone you asked is pretty much right.你问的每个人都非常正确。 Additionally I'd put the chunk of code in a function just because.此外,我会将代码块放在一个函数中,只是因为。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep

def web_stuff(card):
    driver = webdriver.Firefox()
    driver.get("http://www.mtgprice.com/")
    elem = driver.find_element_by_xpath("//form/input")
    elem.send_keys(card) # input
    driver.implicitly_wait(5) # seconds
    driver.find_element_by_class_name('btn-blue').click()


while loop_condition:
    card = input()
    web_stuff(card)

Since you don't say what you want to happen over and over again and if you ever want to stop, a starting point is just to put all the driver code into a loop:由于您不会一遍又一遍地说出想要发生的事情,并且如果您想停止,那么起点只是将所有驱动程序代码放入循环中:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep

while True:
    card = input()

    driver = webdriver.Firefox()
    driver.get("http://www.mtgprice.com/")
    elem = driver.find_element_by_xpath("//form/input")
    elem.send_keys(card) # input
    driver.implicitly_wait(5) # seconds
    driver.find_element_by_class_name('btn-blue').click()

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

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