Skip to content

Commit 2fb4ddf

Browse files
committed
Throw exception clicking on file upload elements in IE
The WebDriver W3C Specification states that if a user attempts to click a file upload element (<input type="file">), an error should be returned. This is consistent with the intended behavior of WebDriver from the start, which is that to upload a file, one should use sendKeys, not click() on the input element. This now codifies that behavior with returning an error. Note that if users have erroneously been using click() with file upload elements, then using some external library (Robot Framework, Sikuli, etc.) to handle the resulting file selection dialog, their code will now break, in that the click() method will throw an exception, and the element will not be clicked. Those users should update their code accordingly.
1 parent 7a0e54a commit 2fb4ddf

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

cpp/iedriver/CommandHandlers/ClickElementCommandHandler.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "ClickElementCommandHandler.h"
2020
#include "errorcodes.h"
21+
#include "logging.h"
2122
#include "../Browser.h"
2223
#include "../Element.h"
2324
#include "../Generated/atoms.h"
@@ -55,6 +56,10 @@ void ClickElementCommandHandler::ExecuteInternal(const IECommandExecutor& execut
5556
ElementHandle element_wrapper;
5657
status_code = this->GetElement(executor, element_id, &element_wrapper);
5758
if (status_code == WD_SUCCESS) {
59+
if (this->IsFileUploadElement(element_wrapper)) {
60+
response->SetErrorResponse(ERROR_INVALID_ARGUMENT, "Cannot call click on an <input type='file'> element. Use sendKeys to upload files.");
61+
return;
62+
}
5863
if (executor.input_manager()->enable_native_events()) {
5964
if (this->IsOptionElement(element_wrapper)) {
6065
std::string option_click_error = "";
@@ -238,4 +243,22 @@ int ClickElementCommandHandler::ExecuteAtom(
238243
return status_code;
239244
}
240245

246+
bool ClickElementCommandHandler::IsFileUploadElement(ElementHandle element) {
247+
CComPtr<IHTMLInputFileElement> file;
248+
element->element()->QueryInterface<IHTMLInputFileElement>(&file);
249+
CComPtr<IHTMLInputElement> input;
250+
element->element()->QueryInterface<IHTMLInputElement>(&input);
251+
CComBSTR element_type;
252+
if (input) {
253+
input->get_type(&element_type);
254+
HRESULT hr = element_type.ToLower();
255+
if (FAILED(hr)) {
256+
LOGHR(WARN, hr) << "Failed converting type attribute of <input> element to lowercase using ToLower() method of BSTR";
257+
}
258+
}
259+
bool is_file_element = (file != NULL) ||
260+
(input != NULL && element_type == L"file");
261+
return is_file_element;
262+
}
263+
241264
} // namespace webdriver

cpp/iedriver/CommandHandlers/ClickElementCommandHandler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class ClickElementCommandHandler : public IECommandHandler {
3636
Response* response);
3737

3838
private:
39+
bool IsFileUploadElement(ElementHandle element_wrapper);
3940
bool IsOptionElement(ElementHandle element_wrapper);
4041
std::wstring GetSyntheticClickAtom();
4142
std::wstring GetClickAtom();

0 commit comments

Comments
 (0)