Skip to content

Commit 1254ba2

Browse files
committed
Refactoring IE element's parent document detection to be the focused doc
1 parent 29f3d39 commit 1254ba2

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

cpp/iedriver/Element.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,18 @@ bool Element::IsAttachedToDom() {
10931093
return false;
10941094
}
10951095

1096+
bool Element::IsDocumentFocused(IHTMLDocument2* focused_doc) {
1097+
CComPtr<IDispatch> parent_doc_dispatch;
1098+
this->element_->get_document(&parent_doc_dispatch);
1099+
1100+
if (parent_doc_dispatch.IsEqualObject(focused_doc)) {
1101+
return true;
1102+
} else {
1103+
LOG(WARN) << "Found managed element's document is not currently focused";
1104+
}
1105+
return false;
1106+
}
1107+
10961108
bool Element::HasFirstChildTextNodeOfMultipleChildren() {
10971109
CComPtr<IHTMLDOMNode> element_node;
10981110
HRESULT hr = this->element_.QueryInterface<IHTMLDOMNode>(&element_node);

cpp/iedriver/Element.h

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class Element {
6262
bool IsInteractable(void);
6363
bool IsEditable(void);
6464
bool IsAttachedToDom(void);
65+
bool IsDocumentFocused(IHTMLDocument2* focused_doc);
6566

6667
std::string element_id(void) const { return this->element_id_; }
6768
IHTMLElement* element(void) { return this->element_; }

cpp/iedriver/IECommandHandler.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,7 @@ int IECommandHandler::GetElement(const IECommandExecutor& executor,
6262
CComPtr<IHTMLDocument2> focused_doc;
6363
current_browser->GetDocument(&focused_doc);
6464

65-
CComPtr<IDispatch> parent_doc_dispatch;
66-
candidate_wrapper->element()->get_document(&parent_doc_dispatch);
67-
68-
if (focused_doc.IsEqualObject(parent_doc_dispatch)) {
65+
if (candidate_wrapper->IsDocumentFocused(focused_doc)) {
6966
*element_wrapper = candidate_wrapper;
7067
return WD_SUCCESS;
7168
} else {

cpp/iedriver/Script.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -683,9 +683,12 @@ int Script::AddArgument(HWND element_repository_handle, const Json::Value& arg)
683683
ElementHandle wrapped_element(new Element(info.element, info.element_id));
684684
bool is_element_valid = wrapped_element->IsAttachedToDom();
685685
if (is_element_valid) {
686-
CComPtr<IDispatch> parent_doc_dispatch;
687-
wrapped_element->element()->get_document(&parent_doc_dispatch);
688-
is_element_valid = this->script_engine_host_.IsEqualObject(parent_doc_dispatch);
686+
is_element_valid = wrapped_element->IsDocumentFocused(this->script_engine_host_);
687+
} else {
688+
status_code = static_cast<int>(::SendMessage(element_repository_handle,
689+
WD_REMOVE_MANAGED_ELEMENT,
690+
NULL,
691+
reinterpret_cast<LPARAM>(&info)));
689692
}
690693

691694
if (is_element_valid) {

0 commit comments

Comments
 (0)