Skip to content

NEW: Add get_text and get_attribute methods to Element class#590

Open
taejukim wants to merge 1 commit intoyashaka:masterfrom
taejukim:add-get-feature
Open

NEW: Add get_text and get_attribute methods to Element class#590
taejukim wants to merge 1 commit intoyashaka:masterfrom
taejukim:add-get-feature

Conversation

@taejukim
Copy link
Copy Markdown

Description

This PR adds two new convenience methods to the Element class in selene/web/_elements.py:

  • get_text() - Returns the text content of the element
  • get_attribute(name) - Returns the value of the specified attribute of the element

Added Methods

def get_text(self) -> str:
    """Gets the text content of the element."""
    from selene.core import query
    return self.get(query.text)

def get_attribute(self, name: str) -> Optional[str]:
    """Gets the value of the specified attribute of the element."""
    from selene.core import query
    return self.get(query.attribute(name))

Usage Examples

# Before
text = browser.element('#my-element').get(query.text)
href = browser.element('a').get(query.attribute('href'))

# After
text = browser.element('#my-element').get_text()
href = browser.element('a').get_attribute('href')

Benefits

  • More intuitive API: Simpler and more readable syntax
  • Backward compatible: No breaking changes
  • Familiar: Similar to Selenium WebElement methods
  • Consistent: Leverages existing query functionality

Implementation

Both methods use existing query.text and query.attribute functionality, so no additional tests are required.

Commit Message

[#ISSUE_NUMBER] NEW: Add get_text and get_attribute methods to Element class

Add convenience methods get_text() and get_attribute(name) to Element class
for more intuitive API compared to element.get(query.text/attribute).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant