For循环破解Python Splinter Browser实例

时间:2017-12-07 16:09:53

标签: python selenium for-loop splinter

我正在尝试使用Splinter和Python循环浏览文本文件,并根据文件中的内容将某些值上传到网站。当我对其进行硬编码时,我可以使用一个文件执行此操作,但无论出于何种原因,每当我尝试使用for循环或while循环遍历所有文本文件时,我刚刚关闭的浏览器实例。没有错误,它只是停止。

def postads(browser):
    for i in range(1,adcount): #<----Code Breaks right here
        count = 0
        temp = open("ads/%s.txt" %i,"r")
        name = temp.readline()
        cat = temp.readline()
        price = temp.readline()
        temp.readline()
        temp.readline()
        desc = ""
        templ = temp.readline()
        while templ.strip()!="":
            print(templ)
            desc+=templ
            templ = temp.readline()
        pics = []
        templ = temp.readline()
        while templ!="":
            print(templ)
            pics.append(templ[:-1])
            templ = temp.readline()
        link = "Website"+str(cat)+"&amount"+str(price)+"&title="+str(name)+"&description="+desc+""
        print(name)
        print(cat)
        print(price)
        print(desc)
        print(pics)
        print(link)
        browser.visit(link)
        for i in pics:
            response = requests.get(i)
            if response.status_code == 200:
                f = open(filet, 'wb')
                f.write(response.content)
                f.close()
            browser.attach_file("files[]",filet)
        button = browser.find_by_id('pstad-frmsubmit')
        while(True==(browser.is_text_present('Please wait'))):
            break
        button.click()
        while(False==(browser.is_text_present('Verified'))):
            break


def browserSesh():
    browser = Browser()
    # Visit URL
    url = "url"
    browser.visit(url)
    browser.find_by_id('login-email').fill('username')
    browser.find_by_id('login-password').fill('password')
    button = browser.find_by_id('btn-submit-login')
    button.click()
    while(False==(browser.is_text_present('Valid text'))):
        break
    postads(browser)

browserSesh()

没有明确的错误消息,但这些是控制台中的最后一行:

2017-12-08 09:34:46 [selenium.webdriver.remote.remote_connection] DEBUG: POST http://127.0.0.1:61908/session/50bcaca3-83dc-40c3-8acb-a73f4bd9378d/element {"using": "css selector", "value": "body", "sessionId": "50bcaca3-83dc-40c3-8acb-a73f4bd9378d"}
2017-12-08 09:34:46 [selenium.webdriver.remote.remote_connection] DEBUG: Finished Request
2017-12-08 09:34:46 [selenium.webdriver.remote.remote_connection] DEBUG: GET http://127.0.0.1:61908/session/50bcaca3-83dc-40c3-8acb-a73f4bd9378d/element/eb1de3b0-b547-4637-8d1e-2e9d4c31e123/text {"id": "eb1de3b0-b547-4637-8d1e-2e9d4c31e123", "sessionId": "50bcaca3-83dc-40c3-8acb-a73f4bd9378d"}
2017-12-08 09:34:46 [selenium.webdriver.remote.remote_connection] DEBUG: Finished Request

我尝试了不同的迭代(使用“with”方法打开浏览器,for循环,while循环,函数中的函数),我真的迷失了。我无法在stackoverflow上找到任何答案,所以我认为值得发帖。

我怀疑它与python解释有关,for-loop的开头表示不再需要浏览器。

0 个答案:

没有答案