How to use launch_persistent_context method in Playwright Python

Best Python code snippet using playwright-python

scraper.py

Source:scraper.py Github

copy

Full Screen

...312 with sync_playwright() as p:313 dir_path = browser_dir.get('chrome_dir')314 if not os.path.exists(dir_path):315 os.makedirs(dir_path)316 browser = p.chromium.launch_persistent_context(user_data_dir='./data',headless=True,permissions=['geolocation'],geolocation={'latitude':37.8136,"longitude":144.9631})317 # else:318 # shutil.rmtree(f'./{dir_path}')319 # browser = p.webkit.launch_persistent_context(user_data_dir=f'{dir_path}',headless=False)320 # browser = p.webkit.launch(headless=False)321 # elif choise==2:322 # dir_path = browser_dir.get('chrome_dir')323 # if not os.path.exists(dir_path):324 # os.makedirs(dir_path)325 # # browser = p.webkit.launch(headless=False)326 # # browser = p.webkit.launch_persistent_context(user_data_dir=f'{dir_path}',headless=False)327 # # browser = p.firefox.launch(headless=False)328 # else:329 # dir_path = browser_dir.get('chrome_dir')330 # if not os.path.exists(dir_path):331 # os.makedirs(dir_path)332 # # browser = p.webkit.launch(headless=False)333 # browser = p.webkit.launch_persistent_context(user_data_dir=f'{dir_path}',headless=False)334 # # browser = p.webkit.launch(headless=False)335 sc = Scraper(browser=browser,url='https://www.facebook.com/marketplace/melbourne/vehicles?sortBy=creation_time_descend&exact=false',db=db)336 elements = sc.scrape_newest()337def main():338 start = time.time()339 browser_dir = {340 'firefox_dir' : "./data/firefox",341 'chrome_dir' : "./data/chrome",342 'webkit_dir' : "./data/webkit"343 }344 choise = random.randint(1,3)345 print(choise)346 browserRun(browser_dir=browser_dir,choise=choise)347 end = time.time()...

Full Screen

Full Screen

test_defaultbrowsercontext.py

Source:test_defaultbrowsercontext.py Github

copy

Full Screen

...22 async def _launch(**options):23 nonlocal context24 if context:25 raise ValueError("can only launch one persitent context")26 context = await browser_type.launch_persistent_context(27 str(tmpdir), **{**launch_arguments, **options}28 )29 return (context.pages[0], context)30 yield _launch31 await context.close()32async def test_context_cookies_should_work(server, launch_persistent, is_firefox):33 (page, context) = await launch_persistent()34 await page.goto(server.EMPTY_PAGE)35 document_cookie = await page.evaluate(36 """() => {37 document.cookie = 'username=John Doe';38 return document.cookie;39 }"""40 )41 assert document_cookie == "username=John Doe"42 assert await page.context.cookies() == [43 {44 "name": "username",45 "value": "John Doe",46 "domain": "localhost",47 "path": "/",48 "expires": -1,49 "httpOnly": False,50 "secure": False,51 "sameSite": "None",52 }53 ]54async def test_context_add_cookies_should_work(server, launch_persistent):55 (page, context) = await launch_persistent()56 await page.goto(server.EMPTY_PAGE)57 await page.context.add_cookies(58 [{"url": server.EMPTY_PAGE, "name": "username", "value": "John Doe"}]59 )60 assert await page.evaluate("() => document.cookie") == "username=John Doe"61 assert await page.context.cookies() == [62 {63 "name": "username",64 "value": "John Doe",65 "domain": "localhost",66 "path": "/",67 "expires": -1,68 "httpOnly": False,69 "secure": False,70 "sameSite": "None",71 }72 ]73async def test_context_clear_cookies_should_work(server, launch_persistent):74 (page, context) = await launch_persistent()75 await page.goto(server.EMPTY_PAGE)76 await page.context.add_cookies(77 [78 {"url": server.EMPTY_PAGE, "name": "cookie1", "value": "1"},79 {"url": server.EMPTY_PAGE, "name": "cookie2", "value": "2"},80 ]81 )82 assert await page.evaluate("document.cookie") == "cookie1=1; cookie2=2"83 await page.context.clear_cookies()84 await page.reload()85 assert await page.context.cookies([]) == []86 assert await page.evaluate("document.cookie") == ""87async def test_should_not_block_third_party_cookies(88 server, launch_persistent, is_chromium, is_firefox89):90 (page, context) = await launch_persistent()91 await page.goto(server.EMPTY_PAGE)92 await page.evaluate(93 """src => {94 let fulfill;95 const promise = new Promise(x => fulfill = x);96 const iframe = document.createElement('iframe');97 document.body.appendChild(iframe);98 iframe.onload = fulfill;99 iframe.src = src;100 return promise;101 }""",102 server.CROSS_PROCESS_PREFIX + "/grid.html",103 )104 document_cookie = await page.frames[1].evaluate(105 """() => {106 document.cookie = 'username=John Doe';107 return document.cookie;108 }"""109 )110 await page.wait_for_timeout(2000)111 allows_third_party = is_chromium or is_firefox112 assert document_cookie == ("username=John Doe" if allows_third_party else "")113 cookies = await context.cookies(server.CROSS_PROCESS_PREFIX + "/grid.html")114 if allows_third_party:115 assert cookies == [116 {117 "domain": "127.0.0.1",118 "expires": -1,119 "httpOnly": False,120 "name": "username",121 "path": "/",122 "sameSite": "None",123 "secure": False,124 "value": "John Doe",125 }126 ]127 else:128 assert cookies == []129async def test_should_support_viewport_option(launch_persistent, utils):130 (page, context) = await launch_persistent(viewport={"width": 456, "height": 789})131 await utils.verify_viewport(page, 456, 789)132 page2 = await context.new_page()133 await utils.verify_viewport(page2, 456, 789)134async def test_should_support_device_scale_factor_option(launch_persistent, utils):135 (page, context) = await launch_persistent(device_scale_factor=3)136 assert await page.evaluate("window.devicePixelRatio") == 3137async def test_should_support_user_agent_option(launch_persistent, server):138 (page, context) = await launch_persistent(user_agent="foobar")139 assert await page.evaluate("() => navigator.userAgent") == "foobar"140 [request, _] = await asyncio.gather(141 server.wait_for_request("/empty.html"),142 page.goto(server.EMPTY_PAGE),143 )144 assert request.getHeader("user-agent") == "foobar"145async def test_should_support_bypass_csp_option(launch_persistent, server):146 (page, context) = await launch_persistent(bypass_csp=True)147 await page.goto(server.PREFIX + "/csp.html")148 await page.add_script_tag(content="window.__injected = 42;")149 assert await page.evaluate("() => window.__injected") == 42150async def test_should_support_javascript_enabled_option(launch_persistent, is_webkit):151 (page, context) = await launch_persistent(java_script_enabled=False)152 await page.goto('data:text/html, <script>var something = "forbidden"</script>')153 with pytest.raises(Error) as exc:154 await page.evaluate("something")155 if is_webkit:156 assert "Can't find variable: something" in exc.value.message157 else:158 assert "something is not defined" in exc.value.message159async def test_should_support_http_credentials_option(server, launch_persistent):160 (page, context) = await launch_persistent(161 http_credentials={"username": "user", "password": "pass"}162 )163 server.set_auth("/playground.html", b"user", b"pass")164 response = await page.goto(server.PREFIX + "/playground.html")165 assert response.status == 200166async def test_should_support_offline_option(server, launch_persistent):167 (page, context) = await launch_persistent(offline=True)168 with pytest.raises(Error):169 await page.goto(server.EMPTY_PAGE)170async def test_should_support_has_touch_option(server, launch_persistent):171 (page, context) = await launch_persistent(has_touch=True)172 await page.goto(server.PREFIX + "/mobile.html")173 assert await page.evaluate('() => "ontouchstart" in window')174@pytest.mark.skip_browser("firefox")175async def test_should_work_in_persistent_context(server, launch_persistent):176 # Firefox does not support mobile.177 (page, context) = await launch_persistent(178 viewport={"width": 320, "height": 480}, is_mobile=True179 )180 await page.goto(server.PREFIX + "/empty.html")181 assert await page.evaluate("() => window.innerWidth") == 980182async def test_should_support_color_scheme_option(server, launch_persistent):183 (page, context) = await launch_persistent(color_scheme="dark")184 assert (185 await page.evaluate('() => matchMedia("(prefers-color-scheme: light)").matches')186 is False187 )188 assert await page.evaluate(189 '() => matchMedia("(prefers-color-scheme: dark)").matches'190 )191async def test_should_support_timezone_id_option(launch_persistent):192 (page, context) = await launch_persistent(timezone_id="America/Jamaica")193 assert (194 await page.evaluate("() => new Date(1479579154987).toString()")195 == "Sat Nov 19 2016 13:12:34 GMT-0500 (Eastern Standard Time)"196 )197async def test_should_support_locale_option(launch_persistent):198 (page, context) = await launch_persistent(locale="fr-CH")199 assert await page.evaluate("() => navigator.language") == "fr-CH"200async def test_should_support_geolocation_and_permission_option(201 server, launch_persistent202):203 (page, context) = await launch_persistent(204 geolocation={"longitude": 10, "latitude": 10}, permissions=["geolocation"]205 )206 await page.goto(server.EMPTY_PAGE)207 geolocation = await page.evaluate(208 """() => new Promise(resolve => navigator.geolocation.getCurrentPosition(position => {209 resolve({latitude: position.coords.latitude, longitude: position.coords.longitude});210 }))"""211 )212 assert geolocation == {"latitude": 10, "longitude": 10}213async def test_should_support_ignore_https_errors_option(214 https_server, launch_persistent215):216 (page, context) = await launch_persistent(ignore_https_errors=True)217 response = await page.goto(https_server.EMPTY_PAGE)218 assert response.ok219async def test_should_support_extra_http_headers_option(server, launch_persistent):220 (page, context) = await launch_persistent(extra_http_headers={"foo": "bar"})221 [request, _] = await asyncio.gather(222 server.wait_for_request("/empty.html"),223 page.goto(server.EMPTY_PAGE),224 )225 assert request.getHeader("foo") == "bar"226async def test_should_accept_user_data_dir(server, tmpdir, launch_persistent):227 (page, context) = await launch_persistent()228 # Note: we need an open page to make sure its functional.229 assert len(os.listdir(tmpdir)) > 0230 await context.close()231 assert len(os.listdir(tmpdir)) > 0232@flaky233async def test_should_restore_state_from_userDataDir(234 browser_type, launch_arguments, server, tmp_path_factory235):236 user_data_dir1 = tmp_path_factory.mktemp("test")237 browser_context = await browser_type.launch_persistent_context(238 user_data_dir1, **launch_arguments239 )240 page = await browser_context.new_page()241 await page.goto(server.EMPTY_PAGE)242 await page.evaluate('() => localStorage.hey = "hello"')243 await browser_context.close()244 browser_context2 = await browser_type.launch_persistent_context(245 user_data_dir1, **launch_arguments246 )247 page2 = await browser_context2.new_page()248 await page2.goto(server.EMPTY_PAGE)249 assert await page2.evaluate("() => localStorage.hey") == "hello"250 await browser_context2.close()251 user_data_dir2 = tmp_path_factory.mktemp("test")252 browser_context3 = await browser_type.launch_persistent_context(253 user_data_dir2, **launch_arguments254 )255 page3 = await browser_context3.new_page()256 await page3.goto(server.EMPTY_PAGE)257 assert await page3.evaluate("() => localStorage.hey") != "hello"258 await browser_context3.close()259async def test_should_restore_cookies_from_userDataDir(260 browser_type,261 launch_arguments,262 tmp_path_factory,263 server,264 is_chromium,265 is_win,266 is_mac,267):268 if is_chromium and (is_win or is_mac):269 pytest.skip()270 userDataDir = tmp_path_factory.mktemp("1")271 browser_context = await browser_type.launch_persistent_context(272 userDataDir, **launch_arguments273 )274 page = await browser_context.new_page()275 await page.goto(server.EMPTY_PAGE)276 document_cookie = await page.evaluate(277 """() => {278 document.cookie = 'doSomethingOnlyOnce=true; expires=Fri, 31 Dec 9999 23:59:59 GMT';279 return document.cookie;280 }"""281 )282 assert document_cookie == "doSomethingOnlyOnce=true"283 await browser_context.close()284 browser_context2 = await browser_type.launch_persistent_context(285 userDataDir, **launch_arguments286 )287 page2 = await browser_context2.new_page()288 await page2.goto(server.EMPTY_PAGE)289 assert await page2.evaluate("() => document.cookie") == "doSomethingOnlyOnce=true"290 await browser_context2.close()291 userDataDir2 = tmp_path_factory.mktemp("2")292 browser_context3 = await browser_type.launch_persistent_context(293 userDataDir2, **launch_arguments294 )295 page3 = await browser_context3.new_page()296 await page3.goto(server.EMPTY_PAGE)297 assert await page3.evaluate("() => document.cookie") != "doSomethingOnlyOnce=true"298 await browser_context3.close()299async def test_should_have_default_url_when_launching_browser(launch_persistent):300 (page, context) = await launch_persistent()301 urls = list(map(lambda p: p.url, context.pages))302 assert urls == ["about:blank"]303@pytest.mark.skip_browser("firefox")304async def test_should_throw_if_page_argument_is_passed(305 browser_type, server, tmpdir, launch_arguments306):307 options = {**launch_arguments, "args": [server.EMPTY_PAGE]}308 with pytest.raises(Error) as exc:309 await browser_type.launch_persistent_context(tmpdir, **options)310 assert "can not specify page" in exc.value.message311async def test_should_fire_close_event_for_a_persistent_context(launch_persistent):312 (page, context) = await launch_persistent()313 fired_event = asyncio.Future()314 context.on("close", lambda: fired_event.set_result(True))315 await context.close()...

Full Screen

Full Screen

main.py

Source:main.py Github

copy

Full Screen

...79 ],80 "creation": creation,81 "lifespan": randint(15, 20), # minutes82}83p_browser = play.chromium.launch_persistent_context(84 "./", headless=BROWSER["headless"], args=BROWSER["args"]85)86BROWSER["browser"] = p_browser87# get global battle details88BATTLE = {89 "card_details": CARDS_DETAILS,90 "battle_interval": int(os.getenv("BATTLE_INTERVAL")),91 "ecr_min": float(int(os.getenv("ECR_MIN")) / 100),92 "ecr_max": float(int(os.getenv("ECR_MAX")) / 100),93 "prioritize_quest": os.getenv("PRIORITIZE_QUEST").lower() == "true",94}95def main():96 try:97 # Step 0: Build Page98 page = Page(console, BROWSER)99 sleep(uniform(1, 2))100 browser_status = True101 # Step 1: Setup Player102 player = User(console, USER)103 sleep(uniform(1, 2))104 while True:105 try:106 # Step 2: Checks if player is logged in; if not, logs them in107 with console.status(108 "[bold blue]Checking if player is logged in"109 ) as status:110 player_login_status = page.is_logged_in(player.username)111 sleep(randint(1, 3))112 if not player_login_status:113 console.log(114 "[bold white]Login Status: [bold red]Currently logged out"115 )116 page.logout_account()117 # Step 2.1: Attempts to login player118 player_login = page.login(player)119 sleep(1)120 if player_login == None:121 console.log("[bold red]Login attempt failed :x:")122 sleep(randint(3, 5))123 break124 else:125 console.log(126 "[bold white] Login Status: [bold green]Login attempt successful :white_check_mark:"127 )128 else:129 console.log(130 "[bold white]Login Status: [bold yellow]Currently logged in"131 )132 sleep(1)133 # Step 3: Check if player is already in battle134 if not page.is_mid_battle():135 # Step 3.1: Calculate how long we should wait for ECR to reset136 ecr_wait = 0137 with console.status("[bold blue]Calculating Current ECR") as status:138 ecr_wait = page.calculate_ecr_wait(139 BATTLE["ecr_min"], BATTLE["ecr_max"]140 )141 if ecr_wait > 0:142 # Step 3.2: If have to wait, close browser and user data143 BROWSER["browser"].close()144 with console.status(145 "[bold blue]Waiting "146 + str((ecr_wait / 60) / 60)147 + " Hours",148 spinner="pong",149 ) as status:150 sleep(ecr_wait)151 p_browser = play.chromium.launch_persistent_context(152 "./", headless=BROWSER["headless"], args=BROWSER["args"]153 )154 BROWSER["browser"] = p_browser155 page = Page(console, BROWSER)156 sleep(uniform(1, 2))157 continue158 else:159 for _ in range(2):160 page.close_modal()161 # Step 4: Initiate battle162 with console.status(163 "[bold blue]Initiating battle", spinner="shark"164 ) as status:165 battle_initiation = page.initiate_battle()166 if not battle_initiation:167 console.log("[bold red]Something occurred during battle initiation")168 break169 sleep(randint(1, 3))170 # Step 5 Create battle instance with battle details171 with console.status("[bold blue]Getting battle details") as status:172 battle_details = page.get_battle_details()173 sleep(1)174 battle = Battle(175 console, player, CARDS_DETAILS, battle_details, BATTLEBASE176 )177 # Step 6: Choose deck from battle deck logs178 with console.status(179 "[bold blue]Picking deck", spinner="growVertical"180 ) as status:181 deck = battle.get_deck(BATTLE["prioritize_quest"])182 sleep(uniform(1, 2))183 # Step 7: Click cards on the page184 page.click_cards(deck)185 # Step 8: Start the battle on the page186 page.start_battle()187 # Step 9: Check who won the battle188 winner = battle.check_winner()189 if winner == USER["username"]:190 player.battle_streak += 1191 console.log("[bold white]Winner: [bold green]" + winner)192 player.battles_won += 1193 elif winner == "DRAW":194 console.log("[bold white]Winner: [bold yellow]" + winner)195 player.battles_drawn += 1196 else:197 player.battle_streak = 0198 player.battles_lost += 1199 console.log("[bold white]Winner: [bold red]" + winner)200 player.battles_played += 1201 sleep(uniform(1, 2))202 # Step 9: Attempts to claim rewards203 # if (204 # CLAIM_REWARD_QUEST205 # and player.quest["claimed"] == None206 # and player.quest["quest_total"] == player.quest["completed_total"]207 # ):208 # page.claim_reward("quest")209 # if CLAIM_REWARD_SEASON:210 # page.claim_reward("season")211 console.log(212 "[bold white]Battle Stats: [bold green]"213 + str(player.battles_won)214 + "[white]/[bold yellow]"215 + str(player.battles_drawn)216 + "[white]/[bold red]"217 + str(player.battles_lost)218 + " [white]([bold blue]"219 + str(player.battles_played)220 + "[white])"221 )222 if player.battle_streak > 1:223 sleep(uniform(1, 2))224 console.log(225 "[bold white]Battle Win Streak: [bold green]"226 + str(player.battle_streak)227 )228 sleep(uniform(1, 2))229 except Exception:230 console.print_exception(show_locals=True)231 console.log(232 "[bold white]Browser Status: [bold red]Error Occurred During Battle Process"233 )234 del page235 BROWSER["browser"].close()236 browser_status = False237 # Step 10: Sleep for set interval238 console.log(239 "[bold white]Rest Period: [bold hot_pink]"240 + str(BATTLE["battle_interval"])241 + " [bold white]Seconds"242 )243 print()244 # Step 10.1: Close browser if older than browser_life_allowed minutes or was closed245 if (246 BROWSER["creation"]247 < datetime.now() - timedelta(minutes=BROWSER["lifespan"])248 and browser_status != False249 ):250 del page251 BROWSER["browser"].close()252 browser_status = False253 sleep(BATTLE["battle_interval"])254 # Step 10.2: Open browser if old one was closed255 if browser_status == False:256 p_browser = play.chromium.launch_persistent_context(257 "./", headless=BROWSER["headless"], args=BROWSER["args"]258 )259 browser_status = True260 BROWSER["browser"] = p_browser261 BROWSER["creation"] = datetime.now()262 page = Page(console, BROWSER)263 sleep(uniform(1, 2))264 except KeyboardInterrupt:265 console.log("[bold red]Shutdown requested...")266 sleep(3)267 except:268 console.print_exception(show_locals=True)269 sys.exit()270if __name__ == "__main__":...

Full Screen

Full Screen

server.py

Source:server.py Github

copy

Full Screen

...23 isMobile = True24 # print(windowSize, isMobile)25 # 不能不加载图片 不然 vue 的渲染会不完整,猜测是因为 vue 的渲染会用到加载图片的事件26 # browser = p.chromium.launch(args=['--blink-settings=imagesEnabled=false'], headless=False)27 # browser_context = p.chromium.launch_persistent_context(user_data_dir='./playwright_temp/user', devtools=True, headless=False, viewport=windowSize, is_mobile=isMobile)28 # browser_context = p.chromium.launch_persistent_context(user_data_dir='./playwright_temp/user', viewport=windowSize, is_mobile=isMobile)29 if devtools == True and isMobile == True:30 windowSize['width'] = windowSize['width'] * 231 if user_agent != None:32 ua = user_agent33 if images_enabled == True:34 args = ['--blink-settings=imagesEnabled=false']35 else:36 args = None37 browser_context = p.chromium.launch_persistent_context(args=args, executable_path=executablePath, devtools=devtools, headless=headless, user_data_dir='./playwright_temp/user', user_agent=ua, viewport=windowSize, is_mobile=isMobile)38 browser = browser_context39 page = browser.new_page()40 if load_script != None:41 page.on("load", lambda :page.evaluate(load_script))42 page.goto(url)43 time.sleep(waittime) # 等待页面渲染完成44 if after_script != None:45 page.evaluate(after_script)46 html = page.content()47 browser.close()48 return html49class myHTTPServerRequestHandler(BaseHTTPRequestHandler):50 def do_GET(self):51 # print(self)...

Full Screen

Full Screen

Playwright tutorial

LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.

Chapters:

  1. What is Playwright : Playwright is comparatively new but has gained good popularity. Get to know some history of the Playwright with some interesting facts connected with it.
  2. How To Install Playwright : Learn in detail about what basic configuration and dependencies are required for installing Playwright and run a test. Get a step-by-step direction for installing the Playwright automation framework.
  3. Playwright Futuristic Features: Launched in 2020, Playwright gained huge popularity quickly because of some obliging features such as Playwright Test Generator and Inspector, Playwright Reporter, Playwright auto-waiting mechanism and etc. Read up on those features to master Playwright testing.
  4. What is Component Testing: Component testing in Playwright is a unique feature that allows a tester to test a single component of a web application without integrating them with other elements. Learn how to perform Component testing on the Playwright automation framework.
  5. Inputs And Buttons In Playwright: Every website has Input boxes and buttons; learn about testing inputs and buttons with different scenarios and examples.
  6. Functions and Selectors in Playwright: Learn how to launch the Chromium browser with Playwright. Also, gain a better understanding of some important functions like “BrowserContext,” which allows you to run multiple browser sessions, and “newPage” which interacts with a page.
  7. Handling Alerts and Dropdowns in Playwright : Playwright interact with different types of alerts and pop-ups, such as simple, confirmation, and prompt, and different types of dropdowns, such as single selector and multi-selector get your hands-on with handling alerts and dropdown in Playright testing.
  8. Playwright vs Puppeteer: Get to know about the difference between two testing frameworks and how they are different than one another, which browsers they support, and what features they provide.
  9. Run Playwright Tests on LambdaTest: Playwright testing with LambdaTest leverages test performance to the utmost. You can run multiple Playwright tests in Parallel with the LammbdaTest test cloud. Get a step-by-step guide to run your Playwright test on the LambdaTest platform.
  10. Playwright Python Tutorial: Playwright automation framework support all major languages such as Python, JavaScript, TypeScript, .NET and etc. However, there are various advantages to Python end-to-end testing with Playwright because of its versatile utility. Get the hang of Playwright python testing with this chapter.
  11. Playwright End To End Testing Tutorial: Get your hands on with Playwright end-to-end testing and learn to use some exciting features such as TraceViewer, Debugging, Networking, Component testing, Visual testing, and many more.
  12. Playwright Video Tutorial: Watch the video tutorials on Playwright testing from experts and get a consecutive in-depth explanation of Playwright automation testing.

Run Playwright Python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful