Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Element.focus() command #257

Closed
ElSnoMan opened this issue May 3, 2022 · 1 comment
Closed

Add Element.focus() command #257

ElSnoMan opened this issue May 3, 2022 · 1 comment
Labels
beginner Small - requires some line changes and a test or two enhancement New feature or request

Comments

@ElSnoMan
Copy link
Owner

ElSnoMan commented May 3, 2022

Problem

Currently, if you want to focus an element in Pylenium, you either have to perform some random action on the element or use the Actions class from Selenium. Both work, but it's probably better to have a convenient method to achieve this.

# Current solutions which don't feel so great

## Random action
py.get("#button").hover()

---or---

## Actions class

element = py.get("#button")
actions = Actions(py.webdriver)
actions.move_to_element(element).perform()

Solutions

I recommend adding a new .focus() method to our Element class instead!

Solution 1: Use Selenium's Action class

# Something like this under the Element class

def focus(self) -> "Element":
    actions = Actions(self.py.webdriver)
    actions.move_to_element(self.webelement).perform()
    return self

Solution 2: Use javascript to do the focus

# Something like this under the Element class

def focus(self) -> "Element":
    javascript = "js that does the focus"
    self.py.execute_script(javascript, self.webelement)
    return self

With a solution in place, focusing on an element feels much more explicit and makes sense 🎉

def test_focus(py: Pylenium):
    py.visit("https://google.com")
    py.get("[name='q']").focus()
    ...
@ElSnoMan ElSnoMan added enhancement New feature or request beginner Small - requires some line changes and a test or two labels May 3, 2022
@ElSnoMan ElSnoMan changed the title Add Element.focus() Add Element.focus() command May 3, 2022
@ElSnoMan ElSnoMan closed this as completed Jul 6, 2022
@ElSnoMan
Copy link
Owner Author

ElSnoMan commented Jul 6, 2022

Released in v1.16.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beginner Small - requires some line changes and a test or two enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant