Preface:
Recently, many friends have encountered the file uploading function in The process of Web automation using Selenium, and found it difficult to use Selenium. So xiaobian here to sort out an article for you, just need a few lines of simple code, you can help you solve the file upload problem.
File upload window analysis
The main reason why you don’t know what to do about file upload is that Selenium is unable to locate relevant elements in the file upload pop-up window. In Chrome, for example, when you click on a web page to upload a file, the following pop-up window will pop up, which is obviously not a browser window, but a Windows pop-up window. However, Selenium only works in the browser, and cannot operate such system pop-ups. So this time we need to use the operating system window tools to achieve.
Introduction to pyWinAuto
Popovers for file uploads can be done in Python through the PyWinAuto module, a python library for PC automation. All Windows on the PC side can use it to operate. I won’t talk too much about PyWinAuto. I’ve written about it before, and I’ve made videos about it. Here is how to use it to achieve file upload. When uploading files, we need this window to operate in three steps:
Operation steps:
1. Fill in the file path
2. Fill in the file name
3. Click open
Before operation, we need to locate these controls, 1, path input box, 2, file input box, 3, open button on the specific details of control positioning operation, we can go to learn the use of PyWinAuto specific positioning code is as follows:
import pywinauto
Create an object that operates on a desktop window using PyWinAutoc
app = pywinauto.Desktop()
# Select the file upload window
dlg = app["Open"]
# Check the file address input box
dlg["Toolbar3"]
# Check the file name input box
dlg["File name (&n):Edit"]
Select the open button
dlg["Open (& O)"]
Copy the code
File upload (image upload OCR recognition) small case
Improved point in the code: Change all of the sleep parts to is_displayed or Selenium wait to reduce the wait time.
from selenium import webdriver
import time
import os
import pywinauto
from pywinauto.keyboard import send_keys
driver = webdriver.Chrome(r"chromedriver.exe")
driver.get("https://pearocr.com/#/")
time.sleep(10)
elems = driver.find_elements_by_xpath("//div[contains(@class,'ImageListToolbar')]//div[@class='BtnBound']")
elems[0].click()
# Use PyWinAuto to select files
app = pywinauto.Desktop()
# Select file upload window
dlg = app["Open"]
# Select the file address input box and click Activate
dlg["Toolbar3"].click()
Enter the path to upload the file
send_keys(R "C:\Users\ XXX \Desktop\)
Type enter to open the path
send_keys("{VK_RETURN}")
# Select the file name input box and enter the file name
dlg["File name (&n):Edit"].type_keys("12312.png")
# Click open
dlg["Open (& O)"].click()
time.sleep(3)
elems[-1].click()
els = driver.find_elements_by_xpath("//div[contains(@class,'q-list--dense')]")
els[1].click()
time.sleep(3)
el2 = driver.find_element_by_xpath("//div[contains(@class,'q-layout--containerized')]")
spans = el2.find_elements_by_xpath("//button[contains(@class,'q-btn--dense')]")
p = spans[2]
p.click()
time.sleep(2)
driver.close()
with open(r"C:\Users\a21036\Downloads\PearOCR.txt"."r",encoding="utf8") as f:
datas = f.readlines()[1: -2]
print([data.strip("\n") for data in datas])
os.remove(r"C:\Users\xxxx\Downloads\PearOCR.txt")
Copy the code