Drag and drop local files with Selenium (Python)

Introduction

When uploading a file with Python Selenium, you can select the file with send_keys (/ path / to / file) for the input element with type =" file " [^ selenium-send-keys]. In most cases, the input element doesn't seem to be there, but it's hidden in CSS, so in most cases this is the way to go (modify the CSS so that the webdriver can access the input element. It is necessary.)

However, some pages do not use the input element for file selection, and you may need to select files by dragging and dropping. In such a case, the following method is effective.

Method

Use the code from florentbr / # wd-drop-file.py --GitHub Gist.

from selenium import webdriver
from selenium.webdriver.remote.webelement import WebElement
import os.path

with open("wd-drop-file.min.js", "r") as f:
    JS_DROP_FILES = f.read()

def drop_files(element, files, offsetX=0, offsetY=0):
    driver = element.parent
    isLocal = not driver._is_remote or '127.0.0.1' in driver.command_executor._url
    paths = []
    
    # ensure files are present, and upload to the remote server if session is remote
    for file in (files if isinstance(files, list) else [files]) :
        if not os.path.isfile(file) :
            raise FileNotFoundError(file)
        paths.append(file if isLocal else element._upload(file))
    
    value = '\n'.join(paths)
    elm_input = driver.execute_script(JS_DROP_FILES, element, offsetX, offsetY)
    elm_input._execute('sendKeysToElement', {'value': [value], 'text': value})

WebElement.drop_files = drop_files

#Below is an example of use
driver = webdriver.Chrome()
driver.get("https://react-dropzone.js.org/")
dropzone = driver.find_element_by_css_selector("[data-preview='Basic example'] [style]")
dropzone.drop_files("C:\\temp\\image1.png ")

If you look at Gist, you can see everything, but the code looks like this. See Gist for wd-drop-file.min.js. The original entered JS_DROP_FILES directly, but this code reads from an external file.

Recommended Posts

Drag and drop local files with Selenium (Python)
Scraping with Python, Selenium and Chromedriver
[GUI with Python] PyQt5-Drag and drop-
Reading and writing JSON files with Python
Practice web scraping with Python and Selenium
Try running Google Chrome with Python and Selenium
ScreenShot with Selenium (Python)
Download and import files with Splunk external python
Scraping with Selenium [Python]
Reading and writing fits files with Python (memo)
Local server with python
Install selenium on Mac and try it with python
Automatic follow on Twitter with python and selenium! (RPA)
Read and write files with Slackbot ~ Bot development with Python ~
Automate Chrome with Python and Selenium on your Chromebook
Automatically translate DeepL into English with Python and Selenium
Programming with Python and Tkinter
Encryption and decryption with Python
Scraping with selenium in Python
Python and hardware-Using RS232C with Python-
Scraping with Selenium + Python Part 1
Python: Working with Firefox with selenium
Sorting image files with Python (2)
Scraping with Selenium in Python
Sort huge files with python
Sorting image files with Python (3)
Sorting image files with Python
Integrate PDF files with Python
Reading .txt files with Python
Scraping with Selenium + Python Part 2
python with pyenv and venv
Challenge Python3 and Selenium Webdriver
Works with Python and R
Communicate with FX-5204PS with Python and PyUSB
Robot running with Arduino and python
Install Python 2.7.9 and Python 3.4.x with pip.
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
Recursively unzip zip files with python
Scraping with Selenium in Python (Basic)
Manipulate files and folders in Python
[Python] POST wav files with requests [POST]
Read and use Python files from Python
Decrypt files encrypted with OpenSSL with Python 3
JSON encoding and decoding with python
Hadoop introduction and MapReduce with Python
Reading and writing NetCDF with Python
Save and retrieve files with Pepper
Handle Excel CSV files with Python
I played with PyQt5 and Python3
Read files in parallel with Python
Export and output files in Python
Reading and writing CSV with Python
Selenium and python to open google
Multiple integrals with Python and Sympy
Coexistence of Python2 and 3 with CircleCI (1.0)
Easy modeling with Blender and Python
Sugoroku game and addition game with python
FM modulation and demodulation with Python