selenium+python automation 77-autoit file upload

foreword

Regarding the upload of non-input files, after clicking the upload button, the pop-up windows control has jumped out of the three realms and does not belong to the jurisdiction of selenium (selenium is not omnipotent, it can only operate elements on the web). The autoit tool is professional in handling windows' control windows, so this needs to be solved with the help of AutoIt.

1. Environmental preparation

1. You can download and install from the autoit official website  http://www.autoitscript.com/site/

2. After downloading to the local, install it foolishly. After installation, find this Autoit v3 in the application

3. Introduction of several menu functions in AutoIt:

  • SciTE Script Editor editor, write AutoIt scripts here
  • AutoIt Windows Info element locator for identifying Windows control information
  • Run Script executes AutoIt script
  • Compile Script to.exe will AutoIt generate .exe executable file

4.Autoit  online documentation , Chinese version http://www.autoitx.com/Doc/

Second, the script implementation

1. First prepare the environment of the web page, take the uploaded image of the blog garden as an example: create a new blog > click image upload > upload local image > pop up the selection image interface, don't move it here

2. Open the SciTE Script Editor and start writing the script. The code is very simple with only four lines

WinActivate("文件上传");
ControlSetText("文件上传", "", "Edit1", "D:\1.png" ); Sleep(2000); ControlClick("文件上传", "", "Button1");

3. Execute after editing, tools>go; or press f5 to execute, after execution, you can see that the image is uploaded successfully.

4. Several commonly used syntax of autoit

  • WinActivate("title") focuses on the specified active window
  • ControlFocus ( "title", "window text", controlID) sets the input focus to a control in the specified window;
  • WinWait ( "title" , "window text" , timeout) pauses the execution of the script until the specified window exists (appears);
  • ControlSetText ( "title", "window text", controlID, "new text" ) modifies the text of the specified control;
  • Sleep (delay) causes the script to pause for the specified time, in milliseconds;
  • ControlClick("title", "window text", control ID, button, click count) sends a mouse click command to the specified control;

Four, element positioning

1. Find Tool View element properties, hold down the icon under Find Tool with the mouse, and then drag it to the element you want to locate

2. View title, title is the Title field identified by AutoIt Window Info

3. Check controlID, controlID is the splicing of Class and Instance identified by AutoIt Window Info

For example, the Class property of the "Open" button here is Button and the Instance property is 1, then the controlID property is Button1

5. Export as exe file

1. After the third step is executed successfully, first save the script to the local

2. Find and open the Compile Script to.exe tool in the application, and convert the .au3 file just exported into a .exe file

3. In order to verify that the generated .exe file is valid, first execute it once in cmd

Drag the generated .exe file directly into cmd and press Enter to execute it

Six, python execution

1. The action of uploading a file has been turned into a .exe file, and then use python to execute the .exe file to achieve file upload

python calls dos, use this method os.system("command to be executed")

# coding:utf-8
from selenium import webdriver import time import os # 加载配置文件实现免登录 profileDir = r'C:\Users\Gloria\AppData\Roaming\Mozilla\Firefox\Profiles\1x41j9of.default' # 你自己电脑的配置文件路径,不要抄我的 profile = webdriver.FirefoxProfile(profileDir) driver = webdriver.Firefox(profile) driver.implicitly_wait(30) driver.get("http://www.cnblogs.com/yoyoketang/") driver.find_element_by_link_text("新随笔").click() time.sleep(3) # 点开编辑器图片 driver.find_element_by_css_selector("img.mceIcon").click() time.sleep(3) # 定位所有iframe,取第二个 iframe = driver.find_elements_by_tag_name('iframe')[1] # 切换到iframe上 driver.switch_to_frame(iframe) # 点开文件上传按钮 driver.find_element_by_name('file').click() # 执行autoit上传文件 os.system("C:\Users\Gloria\Desktop\sendjpg.exe") # 你自己本地的这个.exe文件绝对路径

Seven, autoit command line parameters

1. The exe file packaged above has written the path of the uploaded file to death. Only the fixed image can be transferred each time. We hope to transfer different images during the actual test, so we need to parameterize the file path.
To parameterize the incoming parameters, you can pass the command line parameters of autoit:

    myProg.exe param1 “This is a string parameter” 99

In a script, command line arguments can be obtained with the following variables:

$CmdLine[0] ; = 3
$CmdLine[1] ; = param1 $CmdLine[2] ; = "This is a string parameter" $CmdLine[3] ; = 99 $CmdLineRaw ; = 'param1 "This is a string parameter" 99'

$CmdLine[0] gets the total number of command line parameters, in the above example $CmdLine[0]=3
$CmdLine[1]~$CmdLine[63] gets the command line parameters from 1st to 63rd, this The method can only get up to 63 parameters, but it is enough under normal circumstances.
$CmdLineRaw gets all the parameters that are not split, which is a long string. In this case, it is not limited to 63 parameters.

WinActivate("文件上传");
ControlSetText("文件上传", "", "Edit1", $CmdLine[1] ); Sleep(2000); ControlClick("文件上传", "", "Button1");

2. The question is, if you want to upload pictures in batches, how to do it? ? Is it so easy after parameterization?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325472838&siteId=291194637