0

I have a working script but when I changed the page.on in playwright it actually runs the network response a certain number of times as per the loop count. I have been trying to figure out why that happens.

For example at i=0 it gives one response.url print but at i=10 it prints response.url 10 times and then send 10 duplicate data to mongodb. I have no idea why this is happening. The link being sent based on the print are all the same.

Would be a great help if anyone can let me know what it is that I am doing wrong that is causing this issue.

Pls see sample code here.


#imports here

today = datetime.today().strftime("%m%d%Y")
filenamearr = []

mongousername = 'XXX'
mongopassword = 'XXXX'


client = MongoClient("mongodb+srv://%s:%s@XXXXX.XXXX.mongodb.net/?retryWrites=true&w=majority"%(mongousername,mongopassword))
db = client.DB1

logg = []
async def runbrowser(playwright,url):

    async def handle_response(response,buttonnumber): 
        
        l = str(response.url)
        para = 'param'
    
        if para in l:

            print(response.url)

            
            
            textdata = await response.text()
            subtask = asyncio.create_task(jsonparse(textdata))
            done, pending = await asyncio.wait({subtask})
            
            if subtask in done:
                print("Success in Json parser")
                result = await subtask
                status = [buttonnumber,result]
                logg.append(status)
            print(status)
            logdf = pd.DataFrame(logg)
            logdf.columns = ['BUTTON','RESULT']
            fname = 'XXXX' + today +".csv"
            logdf.to_csv(fname,index=False)
                   
    async def jsonparse(textdata):

            try:
                #parsing happens here to output to MongoDB

                return "Success"

            except Exception as e:
                print("Failled parsing")
                
                return e
                
                
    browser = await playwright.firefox.launch(
        headless=True,
    ) 
    context = await browser.new_context(
    locale='en-US',
    ignore_https_errors = True,
    )

    
    page = await context.new_page()
    
    await page.goto(url,timeout=0)
    button = page.locator("xpath=//button[@event-list-item='']")
    bcount = button.locator(":scope",has_text="Locator")
    count = await bcount.count()
    print(count)
    
    for i in range(count):

        print("\n\n\n\n\nSleeping 10 seconds before clicking button")

        buttonnumber = i
        await asyncio.sleep(10)

        print("Clickking Button: ", i)

        cbtn = bcount.nth(i)
        await cbtn.hover()
        await asyncio.sleep(4)
        await cbtn.click()
        
        if i==0:
            print("i=0")
            
            await page.reload(timeout=0)
     
        retry = page.on("response",lambda response: handle_response(response,buttonnumber))
       
       
        title = await page.title()
        print(title)     
        
        print("Heading back to the main page.")
        await page.go_back(timeout=0)
        await page.reload()
 
    await page.wait_for_timeout(5000)
    await page.close()
    print("Closing Tab")
 
    await browser.close()
    
async def main():

    tasks = []

    async with async_playwright() as playwright:

        url = 'https://samplelink.com'
        tasks.append(asyncio.create_task(runbrowser(playwright,url)))
                
    
        for t in asyncio.as_completed(tasks):

            print(await t)

    await asyncio.gather(*tasks)


asyncio.run(main())

0 Answers0