ultrafunkamsterdam / undetected-chromedriver

Custom Selenium Chromedriver | Zero-Config | Passes ALL bot mitigation systems (like Distil / Imperva/ Datadadome / CloudFlare IUAM)

Home Page:https://github.com/UltrafunkAmsterdam/undetected-chromedriver

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[NoDriver] Fetch.requestPaused not working

namename-123 opened this issue · comments

The code below simply hangs instead of continuing.

async def receive_handler(event: cdp.fetch.RequestPaused):
  print(event.resource_type)
  return cdp.fetch.continue_request(request_id=event.request_id)

async def main():
  options = Options()
  service = Service('C:\Program Files\Google\Chrome\Application\chrome.exe')
  driver = await start(
    service=service,
    options=options,
  )

  cdp.fetch.enable()
  tab = driver.main_tab
  tab.add_handler(cdp.fetch.RequestPaused, receive_handler)

  page = await driver.get('https://abrahamjuliot.github.io/creepjs/')
  
  input("Press Enter to continue...")
  await page.close()

if __name__ == '__main__':

  loop().run_until_complete(main())
commented

Return cdp.fetch.continue_request(request_id=event.request_id)

Needs to be awaited. Not tested but 99% sure

Edit: you are returning a bare CDP command as well. You need to tab.send it as well

commented

The code below simply hangs instead of continuing.

async def receive_handler(event: cdp.fetch.RequestPaused, tab):
  print(event.resource_type)
  return await tab.send(cdp.fetch.continue_request(request_id=event.request_id))

async def main():
  options = Options()
  service = Service('C:\Program Files\Google\Chrome\Application\chrome.exe')
  driver = await start(
    service=service,
    options=options,
  )

  await cdp.fetch.enable()
  tab = driver.main_tab
  tab.add_handler(lambda e: receive_handler(e, tab))

  page = await driver.get('https://abrahamjuliot.github.io/creepjs/')
  
  input("Press Enter to continue...")
  await page.close()

if __name__ == '__main__':

  loop().run_until_complete(main())

And forgot some awaits. Untested corrected code above

Doesn't work, it says RuntimeWarning: coroutine 'receive_handler' was never awaited

Alright I just moved the receive_handler function into the main function and it works now, it's probably better to make a whole class instead.
Thanks for help!

Hmm this is odd, it "should" work now but the site never loads, rather it's stuck on loading indefinitely.
I wonder what's causing this?

commented

this works

from nodriver import *
import asyncio



def receive_handler(event: cdp.fetch.RequestPaused, tab):
  print(event.resource_type)
  asyncio.ensure_future(tab.send(cdp.fetch.continue_request(request_id=event.request_id)))

async def main():
  driver = await start(
  )
  tab = driver.main_tab
  driver.main_tab.add_handler(cdp.fetch.RequestPaused, lambda e: receive_handler(e, tab))
  await tab.get('https://abrahamjuliot.github.io/creepjs/')
  input("Press Enter to continue...")
  await tab.close()


if __name__ == '__main__':
  loop().run_until_complete(main())
  

ofcourse it would make more sense to encapsulate it in a class

commented

image

cdp.fetch.continue_request it cant change url and headers

class RequestInterceptor:
    def __init__(self):
        self.driver = None
        self.tab = None

    async def start(self, driver):
        self.driver = driver
        self.tab = self.driver.main_tab
        self.tab.add_handler(cdp.fetch.RequestPaused, lambda e: self.receive_handler(e))
        await self.enable_fetch()

    async def enable_fetch(self):
        await self.tab.send(cdp.fetch.enable(patterns=[{"urlPattern": "*"}]))

    def receive_handler(self, event: cdp.fetch.RequestPaused):
        print(f"Intercepted {event.resource_type} request")
        asyncio.ensure_future(self.modify_request(event))


    async def modify_request(self, event: cdp.fetch.RequestPaused):
        try:
            url_fragment = event.request.url_fragment
            request_id = event.request_id

            # Convert headers to a dictionary if necessary
            headers = event.request.headers
            if hasattr(headers, 'to_dict'):
                headers_dict = headers.to_dict()
            else:
                headers_dict = {header: value for header, value in headers.items()}

            # Update headers
            for key, value in my_headers.items():
                headers_dict[key] = value

            # Format headers for continue_request
            formatted_headers = [{'name': key, 'value': value} for key, value in headers_dict.items()]

            if url_fragment and "tgWebAppPlatform=web" in url_fragment:
                print(f"Intercepted request with URL fragment: {url_fragment}")

                # Change URL params
                new_url_fragment = url_fragment.replace("tgWebAppPlatform=web", "tgWebAppPlatform=android")
                new_url = event.request.url.split('#')[0] + '#' + new_url_fragment

                print(f"New URL: {new_url}")
                print(f"Modified Headers: {formatted_headers}")

                # Log before sending the request
                print(f"Continuing request {request_id} with new URL and headers...")

                # Continue the request with updated URL and headers
                await self.tab.send(cdp.fetch.continue_request(
                    request_id=request_id, 
                    url=new_url, 
                    headers=formatted_headers
                ))

                # Log after sending the request
                print(f"Request {request_id} continued with modified URL and headers")
            else:
                print(f"Request URL: {event.request.url}")
                print(f"Original Headers: {formatted_headers}")

                # Log before sending the request
                print(f"Continuing request {request_id} with original URL and modified headers...")

                # Continue the request with the original URL and modified headers
                await self.tab.send(cdp.fetch.continue_request(
                    request_id=request_id, 
                    headers=formatted_headers
                ))

                # Log after sending the request
                print(f"Request {request_id} continued with original URL and modified headers")
        
        except Exception as e:
            print(f"Error in intercepting request: {e}")
            traceback.print_exc()


    async def close(self):
        await self.tab.close()