Skip to content

Commit 646af02

Browse files
authored
fix: query_selector strict mode (#870)
1 parent cb54099 commit 646af02

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

playwright/_impl/_element_handle.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ async def select_option(
150150
dict(
151151
timeout=timeout,
152152
noWaitAfter=noWaitAfter,
153+
force=force,
153154
**convert_select_option_values(value, index, label, element)
154155
)
155156
)

playwright/_impl/_frame.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ async def query_selector(
259259
self, selector: str, strict: bool = None
260260
) -> Optional[ElementHandle]:
261261
return from_nullable_channel(
262-
await self._channel.send("querySelector", dict(selector=selector))
262+
await self._channel.send("querySelector", locals_to_params(locals()))
263263
)
264264

265265
async def query_selector_all(self, selector: str) -> List[ElementHandle]:
@@ -321,7 +321,15 @@ async def dispatch_event(
321321
) -> None:
322322
await self._channel.send(
323323
"dispatchEvent",
324-
dict(selector=selector, type=type, eventInit=serialize_argument(eventInit)),
324+
locals_to_params(
325+
dict(
326+
selector=selector,
327+
type=type,
328+
eventInit=serialize_argument(eventInit),
329+
strict=strict,
330+
timeout=timeout,
331+
),
332+
),
325333
)
326334

327335
async def eval_on_selector(
@@ -334,10 +342,13 @@ async def eval_on_selector(
334342
return parse_result(
335343
await self._channel.send(
336344
"evalOnSelector",
337-
dict(
338-
selector=selector,
339-
expression=expression,
340-
arg=serialize_argument(arg),
345+
locals_to_params(
346+
dict(
347+
selector=selector,
348+
expression=expression,
349+
arg=serialize_argument(arg),
350+
strict=strict,
351+
)
341352
),
342353
)
343354
)
@@ -549,6 +560,8 @@ async def select_option(
549560
selector=selector,
550561
timeout=timeout,
551562
noWaitAfter=noWaitAfter,
563+
strict=strict,
564+
force=force,
552565
**convert_select_option_values(value, index, label, element),
553566
)
554567
)

tests/async/test_browsercontext.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,4 +740,6 @@ async def test_strict_selectors_on_context(browser: Browser, server: Server):
740740
)
741741
with pytest.raises(Error):
742742
await page.text_content("button")
743+
with pytest.raises(Error):
744+
await page.query_selector("button")
743745
await context.close()

tests/async/test_frames.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414

1515
import asyncio
1616

17-
from playwright.async_api import Error
17+
import pytest
18+
19+
from playwright.async_api import Error, Page
20+
from tests.server import Server
1821

1922

2023
async def test_evaluate_handle(page, server):
@@ -253,3 +256,17 @@ async def test_should_report_different_frame_instance_when_frame_re_attaches(
253256
frame2 = await frame2_info.value
254257
assert frame2.is_detached() is False
255258
assert frame1 != frame2
259+
260+
261+
async def test_strict_mode(page: Page, server: Server):
262+
await page.goto(server.EMPTY_PAGE)
263+
await page.set_content(
264+
"""
265+
<button>Hello</button>
266+
<button>Hello</button>
267+
"""
268+
)
269+
with pytest.raises(Error):
270+
await page.text_content("button", strict=True)
271+
with pytest.raises(Error):
272+
await page.query_selector("button", strict=True)

0 commit comments

Comments
 (0)