Using Smart Parameters
Smart Parameters allow you to interact with web pages using natural language. This powerful feature bridges the gap between technical precision and human-readable commands, making your automation scripts more maintainable and intuitive.
What You'll Learn
- What Smart Parameters are (technical selectors vs. natural language)
- How Smart Parameters interpret your input
- Examples of using both technical selectors and natural language
- How Smart Parameters apply to URLs and key presses
- The benefits of using Smart Parameters
- Best practices for effective use
What Are Smart Parameters?
Smart Parameters are context-aware values that intelligently determine what you're trying to target on a webpage. There are two ways to use Smart Parameters:
- Technical Selectors: Traditional CSS, XPath, or data-attribute selectors
- Natural Language Descriptions: Human-readable descriptions of elements
How Smart Parameters Work
When you provide a Smart Parameter, the system:
- Analyzes whether you've provided a technical selector or natural language
- For technical selectors: Uses it directly to find the element
- For natural language: Translates your description into an appropriate selector
- Validates the element exists in the current page context
- Performs the requested action on the matched element
Smart Parameters in Action
Using Technical Selectors
Technical selectors provide precise control when you know the exact element structure:
# Using CSS Selector
browser.click(selector="#submit-button")
# Using XPath
browser.type(selector="//input[@name='username']", text="testuser")
Using Natural Language
Natural language makes your code more readable and resilient to UI changes:
# Finding by visible text
browser.click("Sign In")
# Finding by element description
browser.type("Email field", "user@example.com")
# Finding by positional description
browser.click("Submit button in the login form")
URLs and Keys
In addition to selectors, Smart Parameters can also handle things like URLs and keyboard keys.
For instance, I can navigate to a URL using a natural language description:
browser.navigate("google")
Which is equivalent to using a URL:
browser.navigate("https://www.google.com")
Similarly, when using the press function, you can specify keys loosely:
browser.press("return")
Is the same as saying:
browser.press("Enter")
Smart Parameter Commands
Smart Parameters work with all interaction commands:
| Command | Example with Technical Selector | Example with Natural Language |
|---|---|---|
| click | browser.click("#btn", wait="auto") | browser.click("Sign up", wait="auto") |
| type_text | browser.type_text(".input", "hello", delay=0.1) | browser.type_text("Username field", "testuser", delay=0.1) |
| press | browser.press("Enter", wait="auto") | browser.press("return key", wait="auto") |
| drag_and_drop | browser.drag_and_drop("#source", "#target") | browser.drag_and_drop("the blue block", "the thing that says 'droppable'") |
| navigate | browser.navigate("https://www.example.com") | browser.navigate("Example website") |
Benefits of Smart Parameters
- Readability: Natural language makes code self-documenting
- Maintainability: Less brittle than hard-coded selectors/parameters
- Flexibility: Choose the right approach for each scenario
- Productivity: Faster script creation with natural descriptions
- Resilience: Handles UI changes gracefully
- Accessibility: Easier for non-technical team members to understand
- Consistency: Promotes uniformity across automation scripts
Best Practices
-
Be specific with natural language descriptions:
# Too vague
browser.click("button") # May find multiple buttons
# Better
browser.click("Add to cart button") -
Consider element context:
# Target elements within specific containers
browser.type("Email field in the newsletter form", "news@example.com")
Conclusion
Smart Parameters are a game-changer for web automation, offering a powerful yet user-friendly way to interact with web pages. By combining technical precision with natural language descriptions, you can create robust, readable scripts that adapt to changing UIs with ease.