feat: Improved WebSelect Handling: Added Combobox Support, Enhanced Element Detection, and Smarter Option Matching (#679)

## ℹ️ Description

Added Webselect-Function for Input/Dropdown Combobox
PR for issue/missing feature #677

# Fixes / Enhancements

Finding Special Attributes Elements can fail because they are currently
only selected using the name="..." attributes of the HTML elements. If
it fails, ALSO fallback-handle selecting special attribute HTML elements
by ID instead / additionally. (For example the "brands" Input/Combobox
for Mens Shoes...

When trying to select a Value in a <select>, it does not only rely on
the actual Option value (xxx in the example <options
value="xxx">yyy</...>) but instead also on the displayed HTML value
(i.e. yyy in above example). This improves UX because the User doesnt
have to check the actual "value" of the Option but instead can check the
displayed Value from the Browsers Display directly.


Testcases for Webselect_Combobox were not added due to missing knowledge
about Async Mocking properly.


## 📋 Changes Summary

 Fixes & Enhancements
- New WebSelect Functionality
- Improved Element Detection for Special Attributes
- Enhanced <select> Option Matching Logic

This improves UX and test robustness — users no longer need to know the
exact underlying value, as matching also works with the visible label
shown in the browser.

🧩 Result

These updates make dropdown and combobox interactions more intuitive,
resilient, and user-friendly across diverse HTML structures.


### ⚙️ Type of Change
Select the type(s) of change(s) included in this pull request:
- [x] 🐞 Bug fix (non-breaking change which fixes an issue)
- [x]  New feature (adds new functionality without breaking existing
usage)
- [ ] 💥 Breaking change (changes that might break existing user setups,
scripts, or configurations)


##  Checklist
Before requesting a review, confirm the following:
- [x] I have reviewed my changes to ensure they meet the project's
standards.
- [ ] I have tested my changes and ensured that all tests pass (`pdm run
test`).
- [x] I have formatted the code (`pdm run format`).
- [x] I have verified that linting passes (`pdm run lint`).
- [x] I have updated documentation where necessary.

By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Bug Fixes**
* Field lookup now falls back to locating by ID when name lookup times
out.
* Option selection uses a two-pass match (value then displayed text);
JS-path failures now surface as timeouts.
  * Error and log messages localized and clarified.

* **New Features**
* Support for combobox-style inputs: type into the input, open dropdown,
and select by visible text (handles special characters).

* **Tests**
* Added tests for combobox selection, missing dropdowns, no-match
errors, value-path selection, and special-character handling.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Jens <1742418+1cu@users.noreply.github.com>
Co-authored-by: Claude <claude@anthropic.com>
This commit is contained in:
Bjoern147
2025-12-05 21:03:31 +01:00
committed by GitHub
parent 220c01f257
commit 5f68c09899
4 changed files with 239 additions and 22 deletions

View File

@@ -191,6 +191,119 @@ class TestWebScrapingErrorHandling:
await web_scraper.web_input(By.ID, "test-id", "test text")
@pytest.mark.asyncio
async def test_web_select_combobox_missing_dropdown_options(self, web_scraper:WebScrapingMixin) -> None:
"""Test combobox selection when aria-controls attribute is missing."""
input_field = AsyncMock(spec = Element)
input_field.attrs = {}
input_field.clear_input = AsyncMock()
input_field.send_keys = AsyncMock()
web_scraper.web_find = AsyncMock(return_value = input_field) # type: ignore[method-assign]
web_scraper.web_sleep = AsyncMock() # type: ignore[method-assign]
with pytest.raises(TimeoutError, match = "Combobox missing aria-controls attribute"):
await web_scraper.web_select_combobox(By.ID, "combo-id", "Option", timeout = 0.1)
input_field.clear_input.assert_awaited_once()
input_field.send_keys.assert_awaited_once_with("Option")
assert web_scraper.web_sleep.await_count == 1 # Only one sleep before checking aria-controls
@pytest.mark.asyncio
async def test_web_select_combobox_selects_matching_option(self, web_scraper:WebScrapingMixin) -> None:
"""Test combobox selection matches a visible <li> option."""
input_field = AsyncMock(spec = Element)
input_field.attrs = {"aria-controls": "dropdown-id"}
input_field.clear_input = AsyncMock()
input_field.send_keys = AsyncMock()
dropdown_elem = AsyncMock(spec = Element)
dropdown_elem.apply = AsyncMock(return_value = True)
web_scraper.web_find = AsyncMock(side_effect = [input_field, dropdown_elem]) # type: ignore[method-assign]
web_scraper.web_sleep = AsyncMock() # type: ignore[method-assign]
result = await web_scraper.web_select_combobox(By.ID, "combo-id", "Visible Label")
assert result is dropdown_elem
input_field.clear_input.assert_awaited_once()
input_field.send_keys.assert_awaited_once_with("Visible Label")
dropdown_elem.apply.assert_awaited_once()
assert web_scraper.web_sleep.await_count == 2
@pytest.mark.asyncio
async def test_web_select_combobox_no_matching_option_raises(self, web_scraper:WebScrapingMixin) -> None:
"""Test combobox selection raises when no <li> matches the entered text."""
input_field = AsyncMock(spec = Element)
input_field.attrs = {"aria-controls": "dropdown-id"}
input_field.clear_input = AsyncMock()
input_field.send_keys = AsyncMock()
dropdown_elem = AsyncMock(spec = Element)
dropdown_elem.apply = AsyncMock(return_value = False)
web_scraper.web_find = AsyncMock(side_effect = [input_field, dropdown_elem]) # type: ignore[method-assign]
web_scraper.web_sleep = AsyncMock() # type: ignore[method-assign]
with pytest.raises(TimeoutError, match = "No matching option found in combobox"):
await web_scraper.web_select_combobox(By.ID, "combo-id", "Missing Label")
dropdown_elem.apply.assert_awaited_once()
assert web_scraper.web_sleep.await_count == 1 # One sleep after typing, error before second sleep
@pytest.mark.asyncio
async def test_web_select_combobox_special_characters(self, web_scraper:WebScrapingMixin) -> None:
"""Test combobox selection with special characters (quotes, newlines, etc)."""
input_field = AsyncMock(spec = Element)
input_field.attrs = {"aria-controls": "dropdown-id"}
input_field.clear_input = AsyncMock()
input_field.send_keys = AsyncMock()
dropdown_elem = AsyncMock(spec = Element)
dropdown_elem.apply = AsyncMock(return_value = True)
web_scraper.web_find = AsyncMock(side_effect = [input_field, dropdown_elem]) # type: ignore[method-assign]
web_scraper.web_sleep = AsyncMock() # type: ignore[method-assign]
# Test with quotes, backslashes, and newlines
special_value = 'Value with "quotes" and \\ backslash'
result = await web_scraper.web_select_combobox(By.ID, "combo-id", special_value)
assert result is dropdown_elem
input_field.send_keys.assert_awaited_once_with(special_value)
# Verify that the JavaScript received properly escaped value
call_args = dropdown_elem.apply.call_args[0][0]
assert '"quotes"' in call_args or r'\"quotes\"' in call_args # JSON escaping should handle quotes
@pytest.mark.asyncio
async def test_web_select_by_value(self, web_scraper:WebScrapingMixin) -> None:
"""Test web_select successfully matches by option value."""
select_elem = AsyncMock(spec = Element)
select_elem.apply = AsyncMock()
web_scraper.web_check = AsyncMock(return_value = True) # type: ignore[method-assign]
web_scraper.web_await = AsyncMock(return_value = True) # type: ignore[method-assign]
web_scraper.web_find = AsyncMock(return_value = select_elem) # type: ignore[method-assign]
web_scraper.web_sleep = AsyncMock() # type: ignore[method-assign]
result = await web_scraper.web_select(By.ID, "select-id", "option-value")
assert result is select_elem
select_elem.apply.assert_awaited_once()
web_scraper.web_sleep.assert_awaited_once()
@pytest.mark.asyncio
async def test_web_select_raises_on_missing_option(self, web_scraper:WebScrapingMixin) -> None:
"""Test web_select raises TimeoutError when option not found."""
select_elem = AsyncMock(spec = Element)
# Simulate JS throwing an error when option not found
select_elem.apply = AsyncMock(side_effect = Exception("Option not found by value or displayed text: missing"))
web_scraper.web_check = AsyncMock(return_value = True) # type: ignore[method-assign]
web_scraper.web_await = AsyncMock(return_value = True) # type: ignore[method-assign]
web_scraper.web_find = AsyncMock(return_value = select_elem) # type: ignore[method-assign]
with pytest.raises(TimeoutError, match = "Option not found by value or displayed text"):
await web_scraper.web_select(By.ID, "select-id", "missing-option")
async def test_web_input_success_returns_element(self, web_scraper:WebScrapingMixin, mock_page:TrulyAwaitableMockPage) -> None:
"""Successful web_input should send keys, wait, and return the element."""
mock_element = AsyncMock(spec = Element)