【问题标题】:How to make Python go back to asking for input如何让 Python 回到请求输入
【发布时间】:2015-08-27 16:30:38
【问题描述】:

所以我希望程序在完成后返回请求输入。

我已经在 reddit 中问过这个问题,并且在这里经历了很多类似的线程,到目前为止,如果真的执行 x,答案似乎是循环。但是程序返回到第 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()

【问题讨论】:

标签: python loops input line back


【解决方案1】:

你问的每个人都非常正确。另外,我将这段代码放在一个函数中只是因为。

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)

【讨论】:

    【解决方案2】:

    既然你不一遍又一遍地说你想发生什么,如果你想停下来,一个起点就是把所有的驱动程序代码放到一个循环中:

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

    【讨论】:

    • 好的,谢谢。我想我不只是知道如何应用它。我只想让它不断地执行搜索功能。
    猜你喜欢
    • 2014-05-14
    • 2022-11-11
    • 2015-07-28
    • 2021-01-08
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    相关资源
    最近更新 更多