# Issues with "Focus" keyword

**URL:** <https://forum.robotframework.org/t/issues-with-focus-keyword/8933>\
**Category:** Browser\
**Created:** [4 June 2025 10:21 UTC](https://forum.robotframework.org/t/issues-with-focus-keyword/8933 "2025-06-04T10:21:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rasjani](https://avatars.discourse-cdn.com/v4/letter/r/858c86/32.png) [@rasjani](https://forum.robotframework.org/u/rasjani)\
**Post date:** [4 June 2025 10:21 UTC](https://forum.robotframework.org/t/issues-with-focus-keyword/8933/1 "2025-06-04T10:21:07Z")

</div>

I’ve started to get issues with `Focus` keyword within last month or so.. Scenario is following:

In testcase:

```robotframework
  Focus And Input Text ${INPUT_FIRSTNAME} clientname True
  Focus And Input Text ${INPUT_LASTNAME} clientsurename True
  Focus And Input Text ${INPUT_EMAIL} clientname.clientsurename@mailinator.com True
  Focus And Input Text ${INPUT_PHONE} 401234567 True

```

Obviously this is my own keyword so the implementation looks like this:

```robotframework
Focus And Input Text
    [Arguments] ${selector} ${text} ${clear}=${False} ${unfocus}=${False}
    Focus ${selector}
    IF ${clear}==${True} Clear Text ${selector}
    Keyboard Input action=type input=${text}
    IF ${unfocus}==${True} Keyboard Key press Tab

```

Issue is, every now and then, setting to focus to given selector _works_ - eg, element is found _but_ when actual text input happens, text is entered into previously focused element - email in my case.

Anyone seen this sort of case happening ? My assumptions are:

1. focus doesn’t seem to work correctly
2. Clear Text kw will shift the focus away from element that was focused in previous row in my kw.
3. Something that i might not understand at this time ..

Any ideas ? And to be clear, none of the kw calls triggers an error since text was inputted into wrong input field, form does not validate and test fails as validation error prevents form submit..

---

<div class="post-metadata">

**Author:** ![falcon030](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/falcon030/32/3420_2.png) [@falcon030](https://forum.robotframework.org/u/falcon030)\
**Post date:** [4 June 2025 10:49 UTC](https://forum.robotframework.org/t/issues-with-focus-keyword/8933/2 "2025-06-04T10:49:56Z")

</div>

Doens’t [Press Keys](https://marketsquare.github.io/robotframework-browser/Browser.html#Press%20Keys) work instead of Keyboard Input?

---

<div class="post-metadata">

**Author:** ![rasjani](https://avatars.discourse-cdn.com/v4/letter/r/858c86/32.png) [@rasjani](https://forum.robotframework.org/u/rasjani)\
**Post date:** [4 June 2025 11:42 UTC](https://forum.robotframework.org/t/issues-with-focus-keyword/8933/3 "2025-06-04T11:42:20Z")

</div>

Since my original kw was written over a year ago i’ve forgotten the reason but i do remember switching out from Press Keys to Keyboard Input. However, that should be irrelevant as the issue is with Focus keyword and focus not staying where it was requested to be placed.

---

<div class="post-metadata">

**Author:** ![aaltat](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/aaltat/32/15_2.png) [@aaltat](https://forum.robotframework.org/u/aaltat)\
**Post date:** [4 June 2025 13:28 UTC](https://forum.robotframework.org/t/issues-with-focus-keyword/8933/4 "2025-06-04T13:28:20Z")

</div>

Focus should do it and most likely it does it too. But I would suspect that something changes the focus, maybe the browser is doing something or something and it sets the focus on the original position. As a simple and dummy try, have you put small sleep and see does it after the execution. To know where to put the sleep is hard to say because I don’t see your application and don’t know how it works.

In any case, if sleep resolves the problem, then it’s a timing issue and just relying on element being present and Playwright doing its checks are not good enough. Perhaps there is a network request or something that should be waited before you input credentials.

---

<div class="post-metadata">

**Author:** ![rasjani](https://avatars.discourse-cdn.com/v4/letter/r/858c86/32.png) [@rasjani](https://forum.robotframework.org/u/rasjani)\
**Post date:** [4 June 2025 14:09 UTC](https://forum.robotframework.org/t/issues-with-focus-keyword/8933/5 "2025-06-04T14:09:10Z")

</div>

I have not ruled out the possibility that the app itself changes the focus – and there has been such feature implemented _but_ that would mean that there should be visible notification in page but screenshot does not have notification..

Waiting for network calls doesnt feel correct because in the cases i’ve seen this happen, all the fields are there and initial 2 fields have already been filled correctly by `Focus And Input Text` - and then the third one has both 3rd and 4th’s input data ..

I did add add a call to Get Elements States to verify that the selector is `focused` after the call to Focus ..

In order to rule out someone stealing the focus or `Clear Text` messing it up, i made additional kw

```python
Ensure Focus
  [Arguments] ${selector}
  Focus ${selector}
  Get Element States ${selector} *= focused

```

Lets see if this gets triggered ..

But as you are now here @aaltat - when i checked the nodejs focus ( [Locator | Playwright](https://playwright.dev/docs/api/class-locator#locator-focus) ) .. It is async function and has possibility to set the timeout. However in browser side, there’s no option to set the timeout. This feels it could be that if default timeout is 0, focus is not checked ?

---

<div class="post-metadata">

**Author:** ![aaltat](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/aaltat/32/15_2.png) [@aaltat](https://forum.robotframework.org/u/aaltat)\
**Post date:** [4 June 2025 17:32 UTC](https://forum.robotframework.org/t/issues-with-focus-keyword/8933/6 "2025-06-04T17:32:20Z")

</div>

Default is whatever Playwright uses [robotframework-browser/node/playwright-wrapper/interaction.ts at main · MarketSquare/robotframework-browser · GitHub](https://github.com/MarketSquare/robotframework-browser/blob/main/node/playwright-wrapper/interaction.ts#L163). But adding such option is easy, want to do a PR?
