selenium+python:脚本学习笔记(三)

来源:互联网 发布:怎么关闭网络防火墙 编辑:程序博客网 时间:2024/06/05 08:17
 


  • 文件下载


http://blog.csdn.net/huilan_same/article/details/52789954?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutiao.io


#实例化一个火狐配置文件
fp = webdriver.FirefoxProfile()


#设置各项参数,参数可以通过在浏览器地址栏中输入about:config查看。


#设置成0代表下载到浏览器默认下载路径;设置成2则可以保存到指定目录
fp.set_preference("browser.download.folderList",2)


#是否显示开始,(个人实验,不管设成True还是False,都不显示开始,直接下载)
fp.set_preference("browser.download.manager.showWhenStarting",False)


#下载到指定目录
fp.set_preference("browser.download.dir","c:\\test")


#不询问下载路径;后面的参数为要下载页面的Content-type的值
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream")


#启动一个火狐浏览器进程,以刚才的浏览器参数
dr = webdriver.Firefox(firefox_profile=fp)


 


browser.download.manager.showWhenStarting:在开始下载时是否显示下载管理器


browser.helperApps.neverAsk.saveToDisk:对所给出文件类型不再弹出框进行询问


 


  • 等待



locator = (By.XPATH, ".//*[@id='navigation']/li[4]/ul/li[2]/a")


ec=WebDriverWait(driver,20, 0.5).until(EC.presence_of_element_located(locator)) 



self.driver.implicitly_wait(30)


都不好用时,可能考虑元素是一开始就存在在html中 而不是页面加载后才出现的


 


  • 滚动条


js="document.documentElement.scrollTop=" + "500;"#向下移动


js1="document.documentElement.scrollLeft=" + "500;"#向右移动


js2="window.scrollTo(100,500);"#同时操作2个坐标,水平移动到100像素,向下移动到500像素


js3="window.scrollBy(100,100);"#相对移动


js4="document.getElementById(\"MainContent_txtMeasureConfirmTimeEnd\").scrollTop=500;"#以元素为基准,移动


driver.execute_script(js)


 


  • 上传


通过input标签实现的上传页面,可以通过send_keys操作实现上传


#driver.find_element_by_id("MainContent_FileUpload1").send_keys('D:\\upload_file.txt')


 


autoIt


;ControlFocus("title","text",controlID) Edit1=Edit 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", "D:\upload_file.txt")


  Sleep(2000)


; Click on the Open button


  ControlClick("文件上传", "","Button1");


 


os.system("D:\\upload.exe")


 


  • close()与quit()


这是close()的说明:


Closes the current window.
关闭当前窗口。


这是quit()的说明:


Quits the driver and closes every associated window.
退出驱动并关闭所有关联的窗口。

 



原创粉丝点击