Simulate clicking with the mouse at a specified position. The final position to be clicked is computed based on the selector provided and the offset specified and it must be within the visible area of the screen. Implementation note: This method bypasses the normal event pr
(
self,
widget: Widget | type[Widget] | str | None = None,
offset: tuple[int, int] = (0, 0),
shift: bool = False,
meta: bool = False,
control: bool = False,
times: int = 1,
button: int = 1,
)
| 190 | raise error from None |
| 191 | |
| 192 | async def click( |
| 193 | self, |
| 194 | widget: Widget | type[Widget] | str | None = None, |
| 195 | offset: tuple[int, int] = (0, 0), |
| 196 | shift: bool = False, |
| 197 | meta: bool = False, |
| 198 | control: bool = False, |
| 199 | times: int = 1, |
| 200 | button: int = 1, |
| 201 | ) -> bool: |
| 202 | """Simulate clicking with the mouse at a specified position. |
| 203 | |
| 204 | The final position to be clicked is computed based on the selector provided and |
| 205 | the offset specified and it must be within the visible area of the screen. |
| 206 | |
| 207 | Implementation note: This method bypasses the normal event processing in `App.on_event`. |
| 208 | |
| 209 | Example: |
| 210 | The code below runs an app and clicks its only button right in the middle: |
| 211 | ```py |
| 212 | async with SingleButtonApp().run_test() as pilot: |
| 213 | await pilot.click(Button, offset=(8, 1)) |
| 214 | ``` |
| 215 | |
| 216 | Args: |
| 217 | widget: A widget or selector used as an origin |
| 218 | for the click offset. If this is not specified, the offset is interpreted |
| 219 | relative to the screen. You can use this parameter to try to click on a |
| 220 | specific widget. However, if the widget is currently hidden or obscured by |
| 221 | another widget, the click may not land on the widget you specified. |
| 222 | offset: The offset to click. The offset is relative to the widget / selector provided |
| 223 | or to the screen, if no selector is provided. |
| 224 | shift: Click with the shift key held down. |
| 225 | meta: Click with the meta key held down. |
| 226 | control: Click with the control key held down. |
| 227 | times: The number of times to click. 2 will double-click, 3 will triple-click, etc. |
| 228 | button: The mouse button to click. |
| 229 | |
| 230 | Raises: |
| 231 | OutOfBounds: If the position to be clicked is outside of the (visible) screen. |
| 232 | |
| 233 | Returns: |
| 234 | `True` if no selector was specified or if the selected widget was under the mouse |
| 235 | when the click was initiated. `False` is the selected widget was not under the pointer. |
| 236 | """ |
| 237 | try: |
| 238 | return await self._post_mouse_events( |
| 239 | [MouseDown, MouseUp, Click], |
| 240 | widget=widget, |
| 241 | offset=offset, |
| 242 | button=button, |
| 243 | shift=shift, |
| 244 | meta=meta, |
| 245 | control=control, |
| 246 | times=times, |
| 247 | ) |
| 248 | except OutOfBounds as error: |
| 249 | raise error from None |