Skip to content

Commit 4d126f3

Browse files
authored
feat: add clear_input_by_deleting method to simulate delete key presses (#136)
1 parent dfb127d commit 4d126f3

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
- Added `speed` in `Tab.scroll_down` and `Tab.scroll_up` methods to control the scroll speed @nathanfallet
1515
- Allow to wait for promise in `Element.apply` method @nathanfallet
16+
- Added `Element.clear_input_by_deleting` to handle inputs with custom delete behavior @nathanfallet
1617

1718
### Changed
1819

zendriver/core/element.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,39 @@ async def clear_input(self):
694694
"""clears an input field"""
695695
return await self.apply('function (element) { element.value = "" } ')
696696

697+
async def clear_input_by_deleting(self):
698+
"""
699+
clears the input of the element by simulating a series of delete key presses.
700+
701+
this method applies a JavaScript function that simulates pressing the delete key
702+
repeatedly until the input is empty. it is useful for clearing input fields or text areas
703+
when :func:`clear_input` does not work (for example, when custom input handling is implemented on the page).
704+
"""
705+
return await self.apply(
706+
"""
707+
async function clearByDeleting(n, d = 50) {
708+
n.focus();
709+
n.setSelectionRange(0, 0);
710+
while (n.value.length > 0) {
711+
n.dispatchEvent(
712+
new KeyboardEvent("keydown", {
713+
key: "Delete",
714+
code: "Delete",
715+
keyCode: 46,
716+
which: 46,
717+
bubbles: !0,
718+
cancelable: !0,
719+
})
720+
);
721+
n.value = n.value.slice(1);
722+
await new Promise((r) => setTimeout(r, d));
723+
}
724+
n.dispatchEvent(new Event("input", { bubbles: !0 }));
725+
}
726+
""",
727+
await_promise=True,
728+
)
729+
697730
async def send_keys(self, text: str, special_characters: bool = False):
698731
"""
699732
send text to an input field, or any other html element.

0 commit comments

Comments
 (0)