如何制作仅在出现“返回”按钮时才停止的循环?

时间:2019-04-09 19:35:39

标签: python-3.x selenium selenium-webdriver

我用python创建了一个脚本来进入Siafi网站。此脚本有一个循环,可查找要为当天付款的约会,要为之付款,我必须单击“全部选中”,每页十个。我们单击执行,打开一个弹出窗口,询问您是否要执行或计划,键入“ r”执行,然后单击“确认”,然后打开一个弹出窗口以输入代码400,然后单击“确认”,一个小的弹出窗口“等待”。然后我们必须输入400并确认十次。第十次之后,出现一个返回按钮,然后循环继续。我创建了一个“ for i in range(10):”,用于执行400码的十位数字并确认。问题是,当网站运行缓慢时,会出现错误,如果我让它在一次键入和另一次键入之间等待几秒钟,则会浪费时间。我们必须做七千多个,十分之十。我的脚本有一个循环,可以搜索约会,并在付款范围(10)内做一个。我想创建一个附加循环来替换“ for i in range(10)”,并且该循环仅在出现“ return”按钮时停止,因为当该按钮出现时,十个已经被键入。

以下是此代码段:

 for i in range(10):
try:
    vinc = wait.until(EC.presence_of_element_located((By.ID, 'formComp:repeatCompromissoLista:0:tableRealizacao:0:subtableVinculacoes:0:vinculacao_input')))
    vinc = browser.find_element_by_id('formComp:repeatCompromissoLista:0:tableRealizacao:0:subtableVinculacoes:0:vinculacao_input')
    vinc.send_keys('400')
    enterElem5 = wait.until(EC.element_to_be_clickable((By.ID, 'formComp:buttonConfirmar')))
    enterElem5 = browser.find_element_by_id('formComp:buttonConfirmar')
    enterElem5.send_keys(Keys.ENTER)#digitar a vinculação e dar enter, fazer isso dez vezes, ver função range
    time.sleep(int(segundosv))
except:
    time.sleep(6)
    vinc = wait.until(EC.presence_of_element_located((By.ID, 'formComp:repeatCompromissoLista:0:tableRealizacao:0:subtableVinculacoes:0:vinculacao_input')))
    vinc = browser.find_element_by_id('formComp:repeatCompromissoLista:0:tableRealizacao:0:subtableVinculacoes:0:vinculacao_input')
    vinc.send_keys('400')
    enterElem5 = wait.until(EC.element_to_be_clickable((By.ID, 'formComp:buttonConfirmar')))
    enterElem5 = browser.find_element_by_id('formComp:buttonConfirmar')
    enterElem5.send_keys(Keys.ENTER)#digitar a vinculação e dar enter, fazer isso dez vezes, ver função range
    time.sleep(int(segundosv))

enterElem6 = wait.until(EC.presence_of_element_located((By.ID, 'formComp:buttonRetornar')))
enterElem6 = wait.until(EC.element_to_be_clickable((By.ID, 'formComp:buttonRetornar')))
enterElem6 = browser.find_element_by_id('formComp:buttonRetornar')
enterElem6.send_keys(Keys.ENTER)
rp = wait.until(EC.presence_of_element_located((By.ID, 'formComp:tipoDocHabil_input')))
time.sleep(int(segundos))

first

third

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作,并在单击:)

时退出while循环。
counter = 1
while counter < 200:
    try:
        browser.find_element_by_id("button")).click()
        """ Break out of our loop """
        break
    except NoSuchElementException
        """ Increment counter, remove if needed """
        print("Retry: %s"% str(counter))
        counter += 1