Skip to content

Commit 8ffb552

Browse files
committed
Separating out detection of focusable elements for sendKeys in IE
Allows separate error messages for attempting to send keys to a non-focusable element. Also fixes sending keystrokes to <option> elements by forwarding the detections to the parent <select> element.
1 parent b44592f commit 8ffb552

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

cpp/iedriver/CommandHandlers/SendKeysCommandHandler.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,30 @@ void SendKeysCommandHandler::ExecuteInternal(
8686
HWND window_handle = browser_wrapper->GetContentWindowHandle();
8787
HWND top_level_window_handle = browser_wrapper->GetTopLevelWindowHandle();
8888

89-
ElementHandle element_wrapper;
90-
status_code = this->GetElement(executor, element_id, &element_wrapper);
89+
ElementHandle initial_element;
90+
status_code = this->GetElement(executor, element_id, &initial_element);
9191

9292
if (status_code == WD_SUCCESS) {
93+
ElementHandle element_wrapper = initial_element;
94+
CComPtr<IHTMLOptionElement> option;
95+
HRESULT hr = initial_element->element()->QueryInterface<IHTMLOptionElement>(&option);
96+
if (SUCCEEDED(hr) && option) {
97+
// If this is an <option> element, we want to operate on its parent
98+
// <select> element.
99+
CComPtr<IHTMLElement> parent_node;
100+
hr = initial_element->element()->get_parentElement(&parent_node);
101+
while (SUCCEEDED(hr) && parent_node) {
102+
CComPtr<IHTMLSelectElement> select;
103+
HRESULT select_hr = parent_node->QueryInterface<IHTMLSelectElement>(&select);
104+
if (SUCCEEDED(select_hr) && select) {
105+
IECommandExecutor& mutable_executor = const_cast<IECommandExecutor&>(executor);
106+
mutable_executor.AddManagedElement(parent_node, &element_wrapper);
107+
break;
108+
}
109+
hr = parent_node->get_parentElement(&parent_node);
110+
}
111+
}
112+
93113
CComPtr<IHTMLElement> element(element_wrapper->element());
94114

95115
// Scroll the target element into view before executing the action
@@ -233,6 +253,12 @@ void SendKeysCommandHandler::ExecuteInternal(
233253
return;
234254
}
235255

256+
if (!element_wrapper->IsFocusable()) {
257+
response->SetErrorResponse(ERROR_ELEMENT_NOT_INTERACTABLE,
258+
"Element cannot be interacted with via the keyboard because it is not focusable");
259+
return;
260+
}
261+
236262
if (!this->VerifyPageHasFocus(top_level_window_handle, window_handle)) {
237263
LOG(WARN) << "HTML rendering pane does not have the focus. Keystrokes may go to an unexpected UI element.";
238264
}

0 commit comments

Comments
 (0)