Skip to content

Instantly share code, notes, and snippets.

@florentbr
Last active February 22, 2022 02:28
Show Gist options
  • Save florentbr/d7bee068932bdf6e00af3b9f01af9af6 to your computer and use it in GitHub Desktop.
Save florentbr/d7bee068932bdf6e00af3b9f01af9af6 to your computer and use it in GitHub Desktop.
# disable the file picker
driver.execute_script("""
HTMLInputElement.prototype.click = function () {
if (this.type !== 'file') {
HTMLElement.prototype.click.call(this);
}
else if (!this.parentNode) {
this.style.display = 'none';
this.ownerDocument.documentElement.appendChild(this);
this.addEventListener('change', () => this.remove());
}
}
""")
# perform the click
driver.find_element_by_xpath('//button[normalize-space()="Upload"]')\
.click()
# assign the file to the <input type="file">
driver.find_element_by_css_selector('input[type=file]')\
.send_keys(r'c:/myimage.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment