Best Python code snippet using playwright-python
test_popup.py
Source:test_popup.py
...99 http_credentials={"username": "user", "password": "pass"}100 )101 page = await context.new_page()102 await page.goto(server.EMPTY_PAGE)103 async with page.expect_popup() as popup_info:104 await page.evaluate(105 "url => window._popup = window.open(url)", server.PREFIX + "/title.html"106 )107 popup = await popup_info.value108 await popup.wait_for_load_state("domcontentloaded")109 assert await popup.title() == "Woof-Woof"110 await context.close()111async def test_should_inherit_touch_support_from_browser_context(112 browser: Browser, server113):114 context = await browser.new_context(115 viewport={"width": 400, "height": 500}, has_touch=True116 )117 page = await context.new_page()118 await page.goto(server.EMPTY_PAGE)119 has_touch = await page.evaluate(120 """() => {121 win = window.open('')122 return 'ontouchstart' in win123 }"""124 )125 assert has_touch126 await context.close()127async def test_should_inherit_viewport_size_from_browser_context(128 browser: Browser, server129):130 context = await browser.new_context(viewport={"width": 400, "height": 500})131 page = await context.new_page()132 await page.goto(server.EMPTY_PAGE)133 size = await page.evaluate(134 """() => {135 win = window.open('about:blank')136 return { width: win.innerWidth, height: win.innerHeight }137 }"""138 )139 assert size == {"width": 400, "height": 500}140 await context.close()141async def test_should_use_viewport_size_from_window_features(browser: Browser, server):142 context = await browser.new_context(viewport={"width": 700, "height": 700})143 page = await context.new_page()144 await page.goto(server.EMPTY_PAGE)145 size = None146 async with page.expect_popup() as popup_info:147 size = await page.evaluate(148 """() => {149 win = window.open(window.location.href, 'Title', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=600,height=300,top=0,left=0')150 return { width: win.innerWidth, height: win.innerHeight }151 }"""152 )153 popup = await popup_info.value154 await popup.set_viewport_size({"width": 500, "height": 400})155 await popup.wait_for_load_state()156 resized = await popup.evaluate(157 "() => ({ width: window.innerWidth, height: window.innerHeight })"158 )159 await context.close()160 assert size == {"width": 600, "height": 300}161 assert resized == {"width": 500, "height": 400}162async def test_should_respect_routes_from_browser_context(context, server):163 page = await context.new_page()164 await page.goto(server.EMPTY_PAGE)165 def handle_request(route, request, intercepted):166 asyncio.create_task(route.continue_())167 intercepted.append(True)168 intercepted = []169 await context.route(170 "**/empty.html",171 lambda route, request: handle_request(route, request, intercepted),172 )173 async with page.expect_popup():174 await page.evaluate(175 "url => window.__popup = window.open(url)", server.EMPTY_PAGE176 )177 assert len(intercepted) == 1178async def test_browser_context_add_init_script_should_apply_to_an_in_process_popup(179 context, server180):181 await context.add_init_script("window.injected = 123")182 page = await context.new_page()183 await page.goto(server.EMPTY_PAGE)184 injected = await page.evaluate(185 """() => {186 const win = window.open('about:blank');187 return win.injected;188 }"""189 )190 assert injected == 123191async def test_browser_context_add_init_script_should_apply_to_a_cross_process_popup(192 context, server193):194 await context.add_init_script("window.injected = 123")195 page = await context.new_page()196 await page.goto(server.EMPTY_PAGE)197 async with page.expect_popup() as popup_info:198 await page.evaluate(199 "url => window.open(url)", server.CROSS_PROCESS_PREFIX + "/title.html"200 )201 popup = await popup_info.value202 assert await popup.evaluate("injected") == 123203 await popup.reload()204 assert await popup.evaluate("injected") == 123205async def test_should_expose_function_from_browser_context(context, server):206 await context.expose_function("add", lambda a, b: a + b)207 page = await context.new_page()208 await page.goto(server.EMPTY_PAGE)209 added = await page.evaluate(210 """async () => {211 win = window.open('about:blank')212 return win.add(9, 4)213 }"""214 )215 assert added == 13216async def test_should_work(context):217 page = await context.new_page()218 async with page.expect_popup() as popup_info:219 await page.evaluate('window.__popup = window.open("about:blank")')220 popup = await popup_info.value221 assert await page.evaluate("!!window.opener") is False222 assert await popup.evaluate("!!window.opener")223async def test_should_work_with_window_features(context, server):224 page = await context.new_page()225 await page.goto(server.EMPTY_PAGE)226 async with page.expect_popup() as popup_info:227 await page.evaluate(228 'window.__popup = window.open(window.location.href, "Title", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=780,height=200,top=0,left=0")'229 )230 popup = await popup_info.value231 assert await page.evaluate("!!window.opener") is False232 assert await popup.evaluate("!!window.opener")233async def test_window_open_emit_for_immediately_closed_popups(context):234 page = await context.new_page()235 async with page.expect_popup() as popup_info:236 await page.evaluate(237 """() => {238 win = window.open('about:blank')239 win.close()240 }"""241 )242 popup = await popup_info.value243 assert popup244async def test_should_emit_for_immediately_closed_popups(context, server):245 page = await context.new_page()246 await page.goto(server.EMPTY_PAGE)247 async with page.expect_popup() as popup_info:248 await page.evaluate(249 """() => {250 win = window.open(window.location.href)251 win.close()252 }"""253 )254 popup = await popup_info.value255 assert popup256async def test_should_be_able_to_capture_alert(context):257 page = await context.new_page()258 evaluate_promise = asyncio.create_task(259 page.evaluate(260 """() => {261 const win = window.open('')262 win.alert('hello')263 }"""264 )265 )266 popup = await page.wait_for_event("popup")267 dialog = await popup.wait_for_event("dialog")268 assert dialog.message == "hello"269 await dialog.dismiss()270 await evaluate_promise271async def test_should_work_with_empty_url(context):272 page = await context.new_page()273 async with page.expect_popup() as popup_info:274 await page.evaluate("() => window.__popup = window.open('')")275 popup = await popup_info.value276 assert await page.evaluate("!!window.opener") is False277 assert await popup.evaluate("!!window.opener")278async def test_should_work_with_noopener_and_no_url(context):279 page = await context.new_page()280 async with page.expect_popup() as popup_info:281 await page.evaluate(282 '() => window.__popup = window.open(undefined, null, "noopener")'283 )284 popup = await popup_info.value285 # Chromium reports 'about:blank#blocked' here.286 assert popup.url.split("#")[0] == "about:blank"287 assert await page.evaluate("!!window.opener") is False288 assert await popup.evaluate("!!window.opener") is False289async def test_should_work_with_noopener_and_about_blank(context):290 page = await context.new_page()291 async with page.expect_popup() as popup_info:292 await page.evaluate(293 '() => window.__popup = window.open("about:blank", null, "noopener")'294 )295 popup = await popup_info.value296 assert await page.evaluate("!!window.opener") is False297 assert await popup.evaluate("!!window.opener") is False298async def test_should_work_with_noopener_and_url(context, server):299 page = await context.new_page()300 await page.goto(server.EMPTY_PAGE)301 async with page.expect_popup() as popup_info:302 await page.evaluate(303 'url => window.__popup = window.open(url, null, "noopener")',304 server.EMPTY_PAGE,305 )306 popup = await popup_info.value307 assert await page.evaluate("!!window.opener") is False308 assert await popup.evaluate("!!window.opener") is False309async def test_should_work_with_clicking_target__blank(context, server):310 page = await context.new_page()311 await page.goto(server.EMPTY_PAGE)312 await page.set_content(313 '<a target=_blank rel="opener" href="/one-style.html">yo</a>'314 )315 async with page.expect_popup() as popup_info:316 await page.click("a")317 popup = await popup_info.value318 assert await page.evaluate("!!window.opener") is False319 assert await popup.evaluate("!!window.opener")320async def test_should_work_with_fake_clicking_target__blank_and_rel_noopener(321 context, server322):323 page = await context.new_page()324 await page.goto(server.EMPTY_PAGE)325 await page.set_content(326 '<a target=_blank rel=noopener href="/one-style.html">yo</a>'327 )328 async with page.expect_popup() as popup_info:329 await page.eval_on_selector("a", "a => a.click()")330 popup = await popup_info.value331 assert await page.evaluate("!!window.opener") is False332 assert await popup.evaluate("!!window.opener") is False333async def test_should_work_with_clicking_target__blank_and_rel_noopener(334 context, server335):336 page = await context.new_page()337 await page.goto(server.EMPTY_PAGE)338 await page.set_content(339 '<a target=_blank rel=noopener href="/one-style.html">yo</a>'340 )341 async with page.expect_popup() as popup_info:342 await page.click("a")343 popup = await popup_info.value344 assert await page.evaluate("!!window.opener") is False345 assert await popup.evaluate("!!window.opener") is False346async def test_should_not_treat_navigations_as_new_popups(context, server):347 page = await context.new_page()348 await page.goto(server.EMPTY_PAGE)349 await page.set_content(350 '<a target=_blank rel=noopener href="/one-style.html">yo</a>'351 )352 async with page.expect_popup() as popup_info:353 await page.click("a")354 popup = await popup_info.value355 handled_popups = []356 page.on(357 "popup",358 lambda popup: handled_popups.append(True),359 )360 await popup.goto(server.CROSS_PROCESS_PREFIX + "/empty.html")...
main.py
Source:main.py
...56 page.click("text=æ¯æ¥æå¡(ä¸å)")57 time.sleep(1)58 page.frame(name="r_3_3").click("input[name=\"szdbhqk\"]")59 page.frame(name="r_3_3").fill("input[name=\"szdbhqk\"]", "æ ")60 with page.expect_popup() as popup_info:61 page.frame(name="r_3_3").click("input[name=\"jrszdsfwzgfxdq\"]")62 page2 = popup_info.value63 time.sleep(1)64 page2.click("#MyDataGrid td:has-text(\"å¦\")")65 with page.expect_popup() as popup_info:66 page.frame(name="r_3_3").click("input[name=\"skmys\"]")67 page3 = popup_info.value68 time.sleep(1)69 page3.click("#MyDataGrid td:has-text(\"绿ç \")")70 with page.expect_popup() as popup_info:71 page.frame(name="r_3_3").click("input[name=\"brjkqk\"]")72 page4 = popup_info.value73 time.sleep(1)74 page4.click("#MyDataGrid td:has-text(\"å¥åº·\")")75 time.sleep(1)76 with page.expect_popup() as popup_info:77 page.frame(name="r_3_3").click("input[name=\"dtzctw\"]")78 page5 = popup_info.value79 time.sleep(1)80 page5.click("#MyDataGrid td:has-text(\"36.8â\")")81 with page.expect_popup() as popup_info:82 page.frame(name="r_3_3").click("input[name=\"qzhysqk\"]")83 page6 = popup_info.value84 time.sleep(1)85 page6.click("#MyDataGrid td:has-text(\"æ \")")86 with page.expect_popup() as popup_info:87 page.frame(name="r_3_3").click("input[name=\"jrywksfrqk\"]")88 page7 = popup_info.value89 time.sleep(1)90 page7.click("#MyDataGrid td:has-text(\"æ \")")91 with page.expect_popup() as popup_info:92 page.frame(name="r_3_3").click("input[name=\"sfyxlfz\"]")93 page8 = popup_info.value94 time.sleep(1)95 page8.click("#MyDataGrid td:has-text(\"å¦\")")96 with page.expect_popup() as popup_info:97 page.frame(name="r_3_3").click("input[name=\"ywdfhljfxdq\"]")98 page9 = popup_info.value99 time.sleep(1)100 page9.click("#MyDataGrid td:has-text(\"æ \")")101 with page.expect_popup() as popup_info:102 page.frame(name="r_3_3").click("input[name=\"sfjxhsjc\"]")103 page10 = popup_info.value104 time.sleep(1)105 page10.click("#MyDataGrid td:has-text(\"å¦\")")106 with page.expect_popup() as popup_info:107 page.frame(name="r_3_3").click("input[name=\"ywgwljs\"]")108 page11 = popup_info.value109 time.sleep(1)110 page11.click("#MyDataGrid td:has-text(\"æ \")")111 with page.expect_popup() as popup_info:112 page.frame(name="r_3_3").click("input[name=\"ywyjwryjcs\"]")113 page12 = popup_info.value114 time.sleep(1)115 page12.click("#MyDataGrid td:has-text(\"æ \")")116 with page.expect_popup() as popup_info:117 page.frame(name="r_3_3").click("input[name=\"ywyqzysglryjc\"]")118 page13 = popup_info.value119 time.sleep(1)120 page13.click("#MyDataGrid td:has-text(\"æ \")")121 with page.expect_popup() as popup_info:122 page.frame(name="r_3_3").click("input[name=\"glgczt\"]")123 page14 = popup_info.value124 time.sleep(1)125 page14.click("text=éé离è§å¯ç¶æ")126 page.once("dialog", lambda dialog: dialog.dismiss())127 page.frame(name="r_3_3").click("text=å¢å è®°å½")128 context.close()129 browser.close()130 msg()131 132133134with sync_playwright() as playwright:135 run(playwright)
file341.py
Source:file341.py
...6 page = context.new_page()7 # Go to https://www.baidu.com/8 page.goto("https://www.baidu.com/")9 # Click text=è¶
9.69亿人å®ææ°å ç«èå
¨ç¨æ¥ç§10 with page.expect_popup() as popup_info:11 page.click("text=è¶
9.69亿人å®ææ°å ç«èå
¨ç¨æ¥ç§")12 page1 = popup_info.value13 # Click em:has-text("è¶
9.69亿人å®ææ°å ç«èå
¨ç¨æ¥ç§")14 with page1.expect_popup() as popup_info:15 page1.click("em:has-text(\"è¶
9.69亿人å®ææ°å ç«èå
¨ç¨æ¥ç§\")")16 page2 = popup_info.value17 # Click text=ä½è
ææ°æç« 18 page2.click("text=ä½è
ææ°æç« ")19 # Click text=ä¸å®¶å¦è
å
±è¯âä¸å¸¦ä¸è·¯âç论ä¸å®è·µåå±20 with page2.expect_popup() as popup_info:21 page2.click("text=ä¸å®¶å¦è
å
±è¯âä¸å¸¦ä¸è·¯âç论ä¸å®è·µåå±")22 page3 = popup_info.value23 # Close page24 page3.close()25 # Close page26 page2.close()27 # Close page28 page1.close()29 # Close page30 page.close()31 # ---------------------32 context.close()33 browser.close()34with sync_playwright() as playwright:...
test.py
Source:test.py
...6 page = context.new_page()7 # Go to https://www.baidu.com/8 page.goto("https://www.baidu.com/")9 # Click text=å¦ä¹ å
ä¸å
¨ä¼ç²¾ç¥ è¿äºè¡¨è¿°è¦ç²¾è¯»10 with page.expect_popup() as popup_info:11 page.click("text=å¦ä¹ å
ä¸å
¨ä¼ç²¾ç¥ è¿äºè¡¨è¿°è¦ç²¾è¯»")12 page1 = popup_info.value13 # Click text=å¦ä¹ å
ä¸å
¨ä¼ç²¾ç¥,è¿äºè¡¨è¿°è¦ç²¾è¯»-æ°åç½14 # with page1.expect_navigation(url="http://www.xinhuanet.com/politics/2021-11/19/c_1128080146.htm"):15 with page1.expect_navigation():16 with page1.expect_popup() as popup_info:17 page1.click("text=å¦ä¹ å
ä¸å
¨ä¼ç²¾ç¥,è¿äºè¡¨è¿°è¦ç²¾è¯»-æ°åç½")18 page2 = popup_info.value19 # Click :nth-match(:text("è´¢ç»"), 4)20 with page2.expect_popup() as popup_info:21 page2.click(":nth-match(:text(\"è´¢ç»\"), 4)")22 page3 = popup_info.value23 # Click text=央广ç½24 with page3.expect_popup() as popup_info:25 page3.click("text=央广ç½")26 page4 = popup_info.value27 # ---------------------28 context.close()29 browser.close()30with sync_playwright() as playwright:...
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.
Get 100 minutes of automation test minutes FREE!!