标签:browser star wow 图片 传递参数 script win drive timeout
对于Web页面的上传功能实现一般有一下俩种方式:
下面我们实际操作一下来讲解AutoIt上传文件的过程:
1、安装AutoIt(下载网址:https://www.autoitscript.com/site/autoit/downloads/)
2、打开AutoIt Windows Info工具(我的电脑是64位的)
3、用鼠标左键点击弹出窗口的Finder Tool,此时鼠标将变成一个小风扇形状的图标
4、按住Finder Tool不松,将其拖动到需要识别的控件上
窗口的title为“文件上传”,标题的class为“#32770”
文件名输入框的class为“Edit”,Instance为“1”,所以classnameNN为“ComboBox1”
打开按钮的class为“Button”,Instance为“1”,所以classnameNN为“Button1”
5、根据AutoIt Windows Info所识别到的控件信息打开SciTE Script Editor编辑器,编写AutoIt脚本。
;ControlFocus("title","text",controlID) ComboBox1=Combobox instance 1
ControlFocus("文件上传", "" , "Edit1")
;Wait 10 Seconds for the Upload window to appear
WinWait("[CLASS:#32770]", "", 10)
;Set the file name text on the Edit field
ControlSetText("文件上传", "", "Edit1", "alert.html")
Sleep(2000)
;Click on the open button
ControlClick("文件上传", "", "Button1")
6、保存文件后,打开Compile Script to.exe工具,将其生成为.exe可执行文件
1 package JavaTest; 2 3 import java.io.IOException; 4 import java.util.NoSuchElementException; 5 import java.util.concurrent.TimeUnit; 6 import org.openqa.selenium.By; 7 import org.openqa.selenium.WebDriver; 8 import org.openqa.selenium.firefox.FirefoxDriver; 9 10 public class Test { 11 public static void main(String[] arg) throws InterruptedException, IOException 12 { 13 WebDriver driver = new FirefoxDriver(); 14 15 // 设置隐示等待时长:10秒; 16 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 17 driver.get("http://www.jq22.com/yanshi5310"); 18 19 try { 20 driver.switchTo().frame("iframe"); 21 driver.findElement(By.xpath("//*[@id=‘upload_form‘]/div[1]/div[2]")).click(); //打开上传窗口 22 Runtime.getRuntime().exec("C:\\Users\\xxx\\Desktop\\upfile.exe"); // 调用upfile.exe上传程序 23 } 24 catch(NoSuchElementException e) 25 { 26 System.out.println(e.getMessage()); 27 } 28 finally 29 { 30 driver.close(); 31 } 32 } 33 }
1 from selenium import webdriver 2 from selenium.webdriver.common.by import By 3 import os 4 5 # 启动Firefox浏览器 6 driver = webdriver.Firefox() 7 8 # 隐式等待10S,打开网址(可直接通过frame的id和name定位) 9 driver.implicitly_wait(10) 10 driver.get("http://www.jq22.com/yanshi5310") 11 12 try: 13 driver.switch_to.frame("iframe") 14 driver.find_element(By.XPATH, "//*[@id=‘upload_form‘]/div[1]/div[2]").click() # 打开上传窗口 15 os.system("C:\\Users\\xxx\\Desktop\\upfile.exe") # 调用upfile.exe上传程序 16 except Exception as e: 17 print(e.args[0]) 18 finally: 19 driver.close()
1 class Baidu 2 require ‘rubygems‘ 3 require ‘selenium-webdriver‘ 4 5 # 打开firefox并输入网址 6 driver = Selenium::WebDriver.for :firefox 7 8 # 设置隐式等待时间10S 9 driver.manage.timeouts.implicit_wait = 10 10 driver.navigate.to "http://www.jq22.com/yanshi5310" 11 12 begin 13 driver.switch_to.frame(‘iframe‘) 14 driver.find_element(:xpath => "//*[@id=‘upload_form‘]/div[1]/div[2]").click # 打开上传窗口 15 exec("C:\\Users\\xxx\\Desktop\\upfile.exe") 16 rescue => e 17 puts e.message # 显示报错信息 18 ensure 19 driver.close 20 end 21 end
话不多说,<( ̄︶ ̄)↗[GO!]
FireFox about:config详细介绍:https://www.cnblogs.com/abcd19880817/p/7210711.html
FirefoxProfile fp = new FirefoxProfile(); // 为0是下载到浏览器默认下载路径,1是“我的下载”;2是自定义 fp.setPreference("browser.download.folderList", 2); // 是否显示开始 fp.setPreference("browser.download.manager.showWhenStarting", false); // 指定所下载文件的目录 fp.setPreference("browser.download.dir", "d:\\"); // 下载文件类型 fp.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");
fp = webdriver.FirefoxProfile() fp.set_preference("browser.download.folderList", 2) fp.set_preference("browser.download.manager.showWhenStarting", False) # 指定所下载文件的目录。os.getcwd()函数不需要传递参数,用于返回当前的目录 fp.set_preference("browser.download.dir",os.getcwd()) fp.set_preference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream")
profile = Selenium::WebDriver::Firefox::Profile.new profile[‘browser.download.folderList‘] = 2 profile[‘browser.download.manager.showWhenStarting‘] = false driver = Selenium::WebDriver.for :firefox, :profile => profile
标签:browser star wow 图片 传递参数 script win drive timeout
原文地址:https://www.cnblogs.com/CSgarcia/p/9413951.html