selenium
selenium copied to clipboard
[java] feat: Add method to select options containing the provided text
User description
Thanks for contributing to Selenium! A PR well described will help maintainers to quickly review and merge it
Before submitting your PR, please check our contributing guidelines. Avoid large PRs, help reviewers by making them as simple and short as possible.
Description
Add method to select options containing the provided text.
Motivation and Context
The existing selectByVisibleText method only allowed selecting options that exactly match the provided text. However, when using Selenium for crawling, situations arose where selecting options that contain the provided text became necessary.
As a Korean developer, I often encounter cases where suffixes or particles are appended to words, making exact matches insufficient.
For example, if the option text is "1년납" and the provided text is "1년", there was no way to match these semantically similar terms. Therefore, I added a new method to handle such cases by selecting options that contain the provided text.
Types of changes
- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
Checklist
- [x] I have read the contributing document.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have added tests to cover my changes.
- [ ] All new and existing tests passed.
PR Type
Enhancement, Tests
Description
- Introduced a new method
selectByContainsVisibleTextin theISelectinterface and its implementation. - The method allows selecting options that contain the provided text, supporting both exact and partial matches.
- Added comprehensive tests to verify the new method's functionality and exception handling.
Changes walkthrough 📝
| Relevant files | |||||
|---|---|---|---|---|---|
| Enhancement |
| ||||
| Tests |
|
💡 PR-Agent usage: Comment
/helpon the PR to get a list of all available PR-Agent tools and their descriptions
PR Reviewer Guide 🔍
| ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪ |
| 🧪 PR contains tests |
| 🔒 No security concerns identified |
| ⚡ Key issues to review Performance Concern Possible Bug |
PR Code Suggestions ✨
| Category | Suggestion | Score |
| Error handling |
Add error handling for StaleElementReferenceException when interacting with WebElementsThe method is not handling potential StaleElementReferenceException that might occur java/src/org/openqa/selenium/support/ui/Select.java [202-210]
Suggestion importance[1-10]: 9Why: Adding error handling for potential StaleElementReferenceException is a significant improvement, increasing the robustness of the code when dealing with dynamic web elements. | 9 |
| Enhancement |
✅ Simplify the search text handling by using the trimmed text directlySuggestion Impact:The commit simplified the search text handling by using the trimmed text directly in the method selectByContainsVisibleText and deSelectByContainsVisibleText.code diff:
The method is using a complex approach to handle spaces in the search text. Consider java/src/org/openqa/selenium/support/ui/Select.java [191-199]
Suggestion importance[1-10]: 8Why: Simplifying the logic by using trimmed text directly improves code readability and efficiency, addressing a minor enhancement in the code. | 8 |
| Performance |
Replace XPath selector with CSS selector for better performance in exact match searchConsider using a more efficient approach for the initial exact match search. Instead java/src/org/openqa/selenium/support/ui/Select.java [178-180]
Suggestion importance[1-10]: 7Why: The suggestion to replace XPath with a CSS selector can improve performance, as CSS selectors are generally faster. However, the proposed CSS selector syntax is incorrect for matching text content, which reduces the score. | 7 |
| Possible issue |
Add a check to return after selecting the first match for single-select dropdownsThe method is currently selecting all matching options, which might not be the java/src/org/openqa/selenium/support/ui/Select.java [182-187]
Suggestion importance[1-10]: 5Why: The suggestion correctly identifies a potential issue with single-select dropdowns, but the proposed solution is redundant as the existing code already handles this case with the | 5 |
Thank you for the review. I will request you again after adding the function to deselect with the same concept.
Merged based on approval and CI passed. Thank you, @syber911911 !