Python脚本不重新启动/重试循环

2024-04-20 06:21:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个带循环的Python脚本。 如果代码找不到指定的元素:(driver.find\元素\u by\ xpath),他应该打印:“没有按钮”。然后他应该重新开始循环。你知道吗

但是当我运行代码时,它会打印“No button”,但是它会继续脚本而不是retry/loop。你知道吗

not_found=True
while not_found:

    try:
        browser=browser.open(link)
    except HTTPError, e:
        print 'The server couldn\'t fulfill the request.'
    except URLError, e:
        print 'Connection reset by peer'
        sys.stdout.write('\a')
        sys.stdout.flush()
        new=0
        time.sleep(5)


    search_k="just some text to look for"
    source="output.txt"

    if not search_string(search_k, source):
        print "this is a message reminder"       

        startstr='search text"'
        endstr='" />'
        file='output.txt'
        prijs=float(get_html_string(file,startstr,endstr))

        search_s="just some text in .txt"
        filez="blee.txt"
        search_z="texttt"
        search_q="texttttttt"

        if search_string(search_s, filez) and not search_string(search_z, filez) and not search_string(search_q, filez) and rule <= maxprijs:

            driver.get(link)
            try:
                elem = driver.find_element_by_xpath("//input[@type='submit' and @value='submit']")
                elem.click()
            except NoSuchElementException:
                print"No Button"              
                time.sleep(2)     
                new=0 #LOOP SCRIPT IGNORES?

Tags: and代码texttxt脚本元素searchstring