Repository: microsoft/playwright-python

Issue:
Strict mode does not work with query_selector etc.



Discussion:

Resolving patch:
diff --git a/playwright/_impl/_element_handle.py b/playwright/_impl/_element_handle.py
--- a/playwright/_impl/_element_handle.py
+++ b/playwright/_impl/_element_handle.py
@@ -150,6 +150,7 @@ async def select_option(
             dict(
                 timeout=timeout,
                 noWaitAfter=noWaitAfter,
+                force=force,
                 **convert_select_option_values(value, index, label, element)
             )
         )
diff --git a/playwright/_impl/_frame.py b/playwright/_impl/_frame.py
--- a/playwright/_impl/_frame.py
+++ b/playwright/_impl/_frame.py
@@ -259,7 +259,7 @@ async def query_selector(
         self, selector: str, strict: bool = None
     ) -> Optional[ElementHandle]:
         return from_nullable_channel(
-            await self._channel.send("querySelector", dict(selector=selector))
+            await self._channel.send("querySelector", locals_to_params(locals()))
         )
 
     async def query_selector_all(self, selector: str) -> List[ElementHandle]:
@@ -321,7 +321,15 @@ async def dispatch_event(
     ) -> None:
         await self._channel.send(
             "dispatchEvent",
-            dict(selector=selector, type=type, eventInit=serialize_argument(eventInit)),
+            locals_to_params(
+                dict(
+                    selector=selector,
+                    type=type,
+                    eventInit=serialize_argument(eventInit),
+                    strict=strict,
+                    timeout=timeout,
+                ),
+            ),
         )
 
     async def eval_on_selector(
@@ -334,10 +342,13 @@ async def eval_on_selector(
         return parse_result(
             await self._channel.send(
                 "evalOnSelector",
-                dict(
-                    selector=selector,
-                    expression=expression,
-                    arg=serialize_argument(arg),
+                locals_to_params(
+                    dict(
+                        selector=selector,
+                        expression=expression,
+                        arg=serialize_argument(arg),
+                        strict=strict,
+                    )
                 ),
             )
         )
@@ -549,6 +560,8 @@ async def select_option(
                 selector=selector,
                 timeout=timeout,
                 noWaitAfter=noWaitAfter,
+                strict=strict,
+                force=force,
                 **convert_select_option_values(value, index, label, element),
             )
         )


Test patch:
diff --git a/tests/async/test_browsercontext.py b/tests/async/test_browsercontext.py
--- a/tests/async/test_browsercontext.py
+++ b/tests/async/test_browsercontext.py
@@ -740,4 +740,6 @@ async def test_strict_selectors_on_context(browser: Browser, server: Server):
     )
     with pytest.raises(Error):
         await page.text_content("button")
+    with pytest.raises(Error):
+        await page.query_selector("button")
     await context.close()
diff --git a/tests/async/test_frames.py b/tests/async/test_frames.py
--- a/tests/async/test_frames.py
+++ b/tests/async/test_frames.py
@@ -14,7 +14,10 @@
 
 import asyncio
 
-from playwright.async_api import Error
+import pytest
+
+from playwright.async_api import Error, Page
+from tests.server import Server
 
 
 async def test_evaluate_handle(page, server):
@@ -253,3 +256,17 @@ async def test_should_report_different_frame_instance_when_frame_re_attaches(
     frame2 = await frame2_info.value
     assert frame2.is_detached() is False
     assert frame1 != frame2
+
+
+async def test_strict_mode(page: Page, server: Server):
+    await page.goto(server.EMPTY_PAGE)
+    await page.set_content(
+        """
+        <button>Hello</button>
+        <button>Hello</button>
+    """
+    )
+    with pytest.raises(Error):
+        await page.text_content("button", strict=True)
+    with pytest.raises(Error):
+        await page.query_selector("button", strict=True)