async def test_memory_objects() -> None: async with async_playwright() as p: browser = await p.chromium.launch() page = await browser.new_page() await page.goto("https://example.com") page.on("dialog", lambda dialog: dialog.dismiss()) for _ in range(100): await page.evaluate("""async () => alert()""") await page.route("**/", lambda route, _: route.fulfill(body="OK")) for _ in range(100): response = await page.evaluate( """async () => (await fetch("/")).text()""") assert response == "OK" await browser.close() gc.collect() df_dicts = pd.DataFrame() df_dicts["dicts"] = objgraph.by_type("dict") df_dicts["pw_types"] = df_dicts["dicts"].apply(lambda x: x.get("_type")) head = df_dicts["pw_types"].value_counts().head(20) assert "Dialog" not in head.items() assert "Request" not in head.items() assert "Route" not in head.items()
async def test_should_cancel_underlying_protocol_calls(browser_name: str, launch_arguments: Dict): handler_exception = None def exception_handlerdler(loop, context) -> None: nonlocal handler_exception handler_exception = context["exception"] asyncio.get_running_loop().set_exception_handler(exception_handlerdler) async with async_playwright() as p: browser = await p[browser_name].launch(**launch_arguments) page = await browser.new_page() task = asyncio.create_task(page.wait_for_selector("will-never-find")) # make sure that the wait_for_selector message was sent to the server (driver) await asyncio.sleep(0.1) task.cancel() with pytest.raises(asyncio.CancelledError): await task await browser.close() # The actual 'Future exception was never retrieved' is logged inside the Future destructor (__del__). gc.collect() assert handler_exception is None asyncio.get_running_loop().set_exception_handler(None)
async def init(force): if exist_state_file(): state_file_path = Path(app_path / STATE_FILE_NAME) if not force: raise StateFileAlreadyExists( f"State file already exists. Remove it, add --force flag or execute update command instead. " f"Path is {state_file_path}") state_file_path.unlink() async with async_playwright() as p: browser = await p.chromium.launch() page = await browser.new_page() try: await login(page) position = await Position.create(page) position = await position.update(page) position.save() except PlayWrightTimeout as e: await page.screenshot(path=before_timeout_screenshot_path) logging.exception(e) except Error as e: await page.screenshot(path=before_error_screenshot_path) logging.exception(e) finally: await browser.close()
async def update(force): async with async_playwright() as p: browser = await p.chromium.launch() page = await browser.new_page() try: await login(page) new_position = await Position.create(page) if force: new_position = await new_position.update(page) new_position.save() else: old_position = Position.load() new_position = (await new_position.update(page) if old_position != new_position else new_position.touch()) new_position.save() except PlayWrightTimeout as e: await page.screenshot(path=before_timeout_screenshot_path) logging.exception(e) except Error as e: await page.screenshot(path=before_error_screenshot_path) logging.exception(e) finally: await browser.close()
async def main(): async with async_playwright() as p: browser = await p.chromium.launch() page = await browser.new_page() await page.goto("http://playwright.dev") print(await page.title()) await browser.close()
async def main(): check_if_table_exists() parsed_results = [] raw_results = [] async with async_playwright() as p: browser = await p.chromium.launch() page = await browser.new_page() await page.set_viewport_size({"width": 800, "height": 650}) await page.set_extra_http_headers( { "User-Agent": settings.USER_AGENT, "Accept-Language": "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7,la;q=0.6", } ) await page.goto( url=settings.DOCTOLIB_SEARCH_URL, ) page.on( "response", lambda x: asyncio.ensure_future( parse_response(x, parsed_results, raw_results) ), ) while True: await scroll(page)
async def start_spider(platform, start_url, slow_mo): async with async_playwright() as playwright: chromium = playwright.chromium browser = await chromium.launch(headless=False, slow_mo=slow_mo) page = await browser.new_page() await page.goto(start_url) await run(page, platform)
async def main(): async with async_playwright() as p: browser = await p.chromium.launch() page = await browser.new_page() await page.goto("http://whatsmyuseragent.org/") await page.screenshot(path="example.png") await browser.close()
async def main(): global BROWSER REQUEST_LIMITS['pushplus.plus'] = Limit(SleepTime(120)) async with contextlib.AsyncExitStack() as stack: play = await stack.enter_async_context(async_playwright()) BROWSER = await play.chromium.launch() stack.push_async_callback(play.stop) stack.push_async_callback(BROWSER.close) scheduler = AsyncIOScheduler(job_defaults={ 'misfire_grace_time': 3600, 'coalesce': True }, timezone=str(tzlocal.get_localzone())) loop = asyncio.get_running_loop() for s in (signal.SIGINT, signal.SIGTERM, signal.SIGQUIT): loop.add_signal_handler(s, lambda: create_task(shutdown())) event = asyncio.Event() SHUTDOWN_VARIABLES['event'] = event if RUN_IMMEDIATELY: try: task = create_task(run(return_cancelled_error=True)) TASKS.add(task) await task TASKS.discard(task) LOGGER.warning('immediately running finished') except CancelledError: return scheduler.add_job(run, 'cron', hour=7) scheduler.add_job(run, 'cron', hour=12) scheduler.add_job(run, 'cron', hour=19, minute=30) scheduler.start() stack.callback(scheduler.shutdown) stack.push_async_callback(event.wait)
async def test_include_source_docs_option_unchecked(self): # Check to make sure that the search does the right thing when Include Source # Docs is not checked async with async_playwright() as playwright: await self.setup_http_server() await self.setup_browser(playwright) # Just use some example search string searchbar = await self.page.wait_for_selector('#search-input', state='visible') search_string = "Component" await searchbar.type(search_string + "\n", delay=50) # Have to wait until the search results are completely displayed await self.page.wait_for_selector('#search-results h2:has-text("Search Results")') # make sure there weren't any source docs in the search results search_results_all = await self.page.query_selector_all('ul.search li a') for a in search_results_all: href = await a.get_attribute("href") self.assertNotIn("_srcdocs", href, "Search did not include source docs but a source doc found") await self.cleanup()
async def laec(self, ctx, *search): """ Fait une recherche sur laec.fr """ url = self.search.format("+".join( ("+".join(el.split()) for el in search))) await log( 'Recherche !laec par {} sur {}:\n<{}>'.format( ctx.author.mention, ctx.channel.mention, url), MNT) async with async_playwright() as p: browser = await p.chromium.launch(headless=True) page = await browser.new_page() await page.goto(url) await page.wait_for_selector("id=contenu") resultats = await page.query_selector("id=resultats") links = await resultats.query_selector_all("cite") try: link = await links[0].inner_text() await ctx.message.reply(link) except Exception as e: await ctx.message.reply(self.no_result) print(e) await browser.close() return
async def test_memory_objects(server: Server, browser_name: str) -> None: async with async_playwright() as p: browser = await p[browser_name].launch() page = await browser.new_page() await page.goto(server.EMPTY_PAGE) page.on("dialog", lambda dialog: dialog.dismiss()) for _ in range(100): await page.evaluate("""async () => alert()""") await page.route("**/", lambda route, _: route.fulfill(body="OK")) for _ in range(100): response = await page.evaluate("""async () => (await fetch("/")).text()""") assert response == "OK" await browser.close() gc.collect() pw_objects: defaultdict = defaultdict(int) for o in objgraph.by_type("dict"): name = o.get("_type") if not name: continue pw_objects[name] += 1 assert "Dialog" not in pw_objects assert "Request" not in pw_objects assert "Route" not in pw_objects
async def hello_world(): async with async_playwright() as p: browser = await p.chromium.launch(headless=False) page = await browser.new_page() await page.goto("http://www.baidu.com") await page.fill('#kw', "playwright") await page.click('#su') await browser.close()
async def run_and_eval(script, stealth_conf: Optional[StealthConfig] = None): async with async_playwright() as pw: browser = await pw.chromium.launch(headless=True) page: Page = await browser.new_page() if stealth_conf: await stealth_async(page, stealth_conf) await page.goto('http://httpbin.org/html') return await page.evaluate(script)
async def test_example2(): async with async_playwright() as playwright: browser = await playwright.chromium.launch() page = await browser.new_page() await page.goto("https://httpbin.org/", timeout=60 * 1000) content = await page.text_content("h2") assert "httpbin" in content
async def page_with_har_tracer( ) -> AsyncGenerator[Tuple[Page, HarTracer], None]: async with async_playwright() as p: browser = await p.chromium.launch() browser_name = p.chromium.name context = await browser.new_context() tracer = HarTracer(context=context, browser_name=browser_name) page = await context.new_page() try: yield page, tracer finally: await context.close() await browser.close()
async def main(): async with async_playwright() as p: browser = await p.chromium.launch() page = await browser.new_page() await page.goto("http://raspberrypi.local/admin/index.php?login") # 输入密码 await page.type("#loginpw", os.getenv("PIHOLEPASSWORD")) # 登录 await page.click(".btn.form-control") # 登出 await page.click('a[href="?logout"]') await browser.close()
async def main(): async with async_playwright() as p: browser = await p.chromium.launch() context = await browser.new_context() tracer = HarTracer(context=context, browser_name=p.chromium.name) page = await context.new_page() await page.goto("http://whatsmyuseragent.org/") har = await tracer.flush() await context.close() await browser.close() logger.info(har)
async def get_client_with_clearance(proxy: str = None, playwright: AsyncPlaywright = None, logs=False, test=False): if playwright: ua, cookies_for_httpx = await get_one_clearance(proxy, playwright, logs=logs) else: async with async_playwright() as p: ua, cookies_for_httpx = await get_one_clearance(proxy, p, logs=logs) client = await build_client_with_clearance(ua, cookies_for_httpx, test=test) return client
async def __aenter__(self) -> DisplayFixture: es = self._exit_stack = AsyncExitStack() browser: Browser | BrowserContext if not hasattr(self, "page"): if not hasattr(self, "_browser"): pw = await es.enter_async_context(async_playwright()) browser = await pw.chromium.launch() else: browser = self._browser self.page = await browser.new_page() if not hasattr(self, "backend"): self.backend = BackendFixture() await es.enter_async_context(self.backend) return self
async def _(): async with async_playwright() as p: browser = await new_browser(False, p.chromium) driver = await new_driver(browser) try: await login(driver, args.username, args.password) court_num_or_name = args.court_name or args.court_num await schedule( driver, court_num_or_name, args.date, args.start_time, args.end_time, ) finally: await driver.screenshot(path="test.png", type="png") await driver.close()
async def main(): async with async_playwright() as p: browser = await p.chromium.launch() page = await browser.new_page() await page.goto("http://raspberrypi.local/admin/index.php?login") # 输入密码 await page.type("#loginpw", os.getenv("PIHOLEPASSWORD")) # 登录 await page.click(".btn.form-control") await page.click('a[href="groups-domains.php?type=black"]') await page.type("#new_domain", "www.black.com") await page.type("#new_domain_comment", "this is an invalid black domain for testing.") await page.click("#add2black") await page.click('[role="alert"] button') # 进入白名单配置页面 await page.click('a[href="groups-domains.php?type=white"]') # 添加 await page.type("#new_domain", "www.white.com") await page.type("#new_domain_comment", "this is an invalid white domain for testing.") await page.click("#add2white") await page.click('[role="alert"] button') # 删除黑名单 await page.click('a[href="groups-domains.php?type=black"]') await page.click("#domainsTable tbody tr td button") await page.click('[role="alert"] button') # 删除白名单 await page.click('a[href="groups-domains.php?type=white"]') await page.click("#domainsTable tbody tr td button") await page.click('[role="alert"] button') # 登出 await page.click('a[href="?logout"]') await browser.close()
async def main() -> None: async with async_playwright() as p: browser = await p.chromium.launch() page = await browser.new_page() await page.goto("https://example.com") page.on("dialog", lambda dialog: dialog.dismiss()) for _ in range(100): await page.evaluate("""async () => alert()""") await page.route("**/", lambda route, request: route.fulfill(body="OK")) for i in range(100): response = await page.evaluate("""async () => (await fetch("/")).text()""") assert response == "OK" await browser.close() print_memory_objects() print("PW-OK")
async def do_render( self, url: str, viewport: Optional[dict] = None, target: Optional[str] = None, operation: Optional[Callable[[Page], Awaitable[None]]] = None, ) -> Optional[bytes]: async with self.lock: async with async_playwright() as playwright: self.browser = await self.get_browser(playwright) self._inter_log("open browser") if viewport: constext = await self.browser.new_context( viewport={ "width": viewport["width"], "height": viewport["height"], }, device_scale_factor=viewport.get("deviceScaleFactor", 1), ) page = await constext.new_page() else: page = await self.browser.new_page() if operation: await operation(page) else: await page.goto(url) self._inter_log("open page") if target: target_ele = page.locator(target) if not target_ele: return None data = await target_ele.screenshot(type="jpeg") else: data = await page.screenshot(type="jpeg") self._inter_log("screenshot") await page.close() self._inter_log("close page") await self.close_browser() self._inter_log("close browser") assert isinstance(data, bytes) return data
async def test_gen_gui(self, basename = "create_generic_model.py"): if (basename[:2] == "__"): return print("\n" + LINE_STR + "\n" + basename + "\n" + LINE_STR) self.current_test_desc = '' self.current_model = basename self.generate_html_file() async with async_playwright() as playwright: await self.run_gui_tests(playwright) if not DEBUG: try: os.remove(self.diagram_file) except OSError: # Don't want the test to fail if the test file is # already removed pass
async def main(): urls = [ ("sannysoft", "https://bot.sannysoft.com/"), # ('areuhead', 'http://arh.antoinevastel.com/bots/areyouheadless') ] # mix browser_type, use_head, use_stealth — all combinations mix = product( ("chromium", "firefox"), # browser type (True, False), # to use headless mode or not (True, False), # to use stealth or not urls) mix = (('chromium', True, True, urls[0]), ) async with async_playwright() as pw: for browser, use_head, use_stealth, url in mix: url_name, url = url await capture( pw, f'results/{browser}_head{"less" if use_head else "full"}_{"stealth_" if use_stealth else ""}{url_name}', use_stealth=use_stealth, headless=use_head, url=url, )
async def test_n2_gui(self, basename): if (basename[:2] == "__"): return print("\n" + LINE_STR + "\n" + basename + "\n" + LINE_STR) self.current_test_desc = '' self.current_model = basename self.generate_n2_file() async with async_playwright() as playwright: await self.run_gui_tests(playwright) if not DEBUG: try: for n2html in self.n2files: os.remove(self.n2files[n2html]) except OSError: # Don't want the test to fail if the test file is # already removed pass
async def test_include_source_docs_option_checked(self): # Check to make sure that the search does the right thing when Include Source # Docs is checked async with async_playwright() as playwright: await self.setup_http_server() await self.setup_browser(playwright) # Include source docs in the search search_source_checkbox = await self.page.wait_for_selector('#search-source', state='visible') await search_source_checkbox.click(delay=50) searchbar = await self.page.wait_for_selector('#search-input', state='visible') search_string = "Component" await searchbar.type(search_string + "\n", delay=50) # Have to wait until the search results are completely displayed await self.page.wait_for_selector('#search-results h2:has-text("Search Results")') # Look through all the search results search_results_all = await self.page.query_selector_all('ul.search li a') # Need to find at least one href which includes "_srcdocs" srcdocs_found = False for a in search_results_all: href = await a.get_attribute("href") if "_srcdocs" in href: srcdocs_found = True break self.assertTrue(srcdocs_found, "Include source docs checkbox checked but no srcdocs " "found in search results") await self.cleanup()
async def playwright(): async with async_playwright() as playwright_object: yield playwright_object
import random from Levenshtein import distance from fastapi import FastAPI, HTTPException from pydantic import BaseModel from playwright.async_api import async_playwright, Playwright, Browser, Page import shortuuid import cv2 import numpy as np from typing import Any, Dict, List, Optional, Tuple app = FastAPI() playwright = async_playwright() qr_decoder = cv2.QRCodeDetector() sessions: Dict[str, Tuple[Playwright, Browser, Page, str]] = {} class SessionInput(BaseModel): session_id: Optional[str] = None phone: str class LoginInput(BaseModel): code: str class OrderInput(BaseModel): query: str