Python-Selenium2做Web自动化测试(12)-上传文件、下载文件、调用 JavaScript、控制浏览器滚动条

来源:互联网 发布:知乎自己的匿名回答 编辑:程序博客网 时间:2024/05/29 10:02

一、上传文件
文件上传操作只要定位上传按钮,通 send_keys 添加本地文件路径就可以了。绝对路径和相对路径都可以,关键是上传的文件存在。下面通地例子演示操作过程。

引用网友的Html文件
upload_file.html

<html><head><meta http-equiv="content-type" content="text/html;charset=utf-8" /><title>upload_file</title><script type="text/javascript" async=""src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css"rel="stylesheet" /><script type="text/javascript"></script></head><body><div class="row-fluid"><div class="span6 well"><h3>upload_file</h3><input type="file" name="file" /></div></div></body><script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script></html>

操作上传脚本:

#coding=utf-8from selenium import webdriverimport os,timechromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"os.environ["webdriver.chrome.driver"] = chromedriverdriver=webdriver.Chrome(chromedriver)#打开上传文件页面file_path = 'file:///' + os.path.abspath('upload_file.html')driver.get(file_path)#定位上传按钮,添加本地文件driver.find_element_by_name("file").send_keys('D:\\selenium_use_case\upload_file.txt')time.sleep(2)driver.quit()

二、下载文件
使用 requests 模块来查找内容类型。 Requests 是一个 Python 的 HTTP 客户端库,默认下载的 python 环境包不包含这个类库,需要另外安装。使用方法如下。

import requestsprint requests.head(’http://www.python.org’).headers[’content-type’]#coding=utf-8import osfrom selenium import webdriverchromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"os.environ["webdriver.chrome.driver"] = chromedriverdriver=webdriver.Chrome(chromedriver)driver.set_preference("browser.download.folderList",2)driver.set_preference("browser.download.manager.showWhenStarting",False)driver.set_preference("browser.download.dir", os.getcwd())driver.set_preference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream")browser = webdriver.Chrome(driver)browser.get("http://pypi.python.org/pypi/selenium")browser.find_element_by_partial_link_text("selenium-2").click()

三、调用 JavaScript
当 webdriver 遇到没法完成的操作时,可以考虑借用 JavaScript 来完成,当 webdriver 遇到没法完成的操作时,笔者可以考虑借用 JavaScript 来完成
webdriver 提供了 execute_script() 接口用来调用 js 代码。
执行 js 一般有两种场景:

  • 一种是在页面上直接执行 JS
  • 另一种是在某个已经定位的元素上执行 JS
#coding=utf-8from selenium import webdriverimport time,oschromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"os.environ["webdriver.chrome.driver"] = chromedriverdriver=webdriver.Chrome(chromedriver)file_path = 'file:///' + os.path.abspath('js.html')driver.get(file_path)#######通过 JS 隐藏选中的元素##########第一种方法:#隐藏文字信息driver.execute_script('$("#tooltip").fadeOut();')time.sleep(5)#隐藏按钮:button = driver.find_element_by_class_name('btn')driver.execute_script('$(arguments[0]).fadeOut()',button)time.sleep(5)driver.quit()
  • execute_script(script, *args) 在当前窗口/框架 同步执行 javaScript
  • script:JavaScript 的执行。
  • *args:适用任何 JavaScript 脚本。

四、控制浏览器滚动条
通过前面javascript学习后,我们使用他来进行控制浏览器滚动条操作( 但滚动条并非页面上的元素, 可以借助 JavaScript是来完成操作。)
一般用到操作滚动条的会两个场景:

  • 注册时的法律条文的阅读,判断用户是否阅读完成的标准是:滚动条是否拉到最下方。
  • 要操作的页面元素不在视觉范围,无法进行操作,需要拖动滚动条

用于标识滚动条位置的代码
如果滚动条在最上方的话, scrollTop=0 , 那么要想使用滚动条在最可下方, 可以 scrollTop=100000 ,这样就可以使滚动条在最下方

<body onload= "document.body.scrollTop=0 "><body onload= "document.body.scrollTop=100000 ">
#coding=utf-8from selenium import webdriverimport time,os#访问百度chromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"os.environ["webdriver.chrome.driver"] = chromedriverdriver=webdriver.Chrome(chromedriver)driver.get("http://www.baidu.com")#搜索driver.find_element_by_id("kw").send_keys("selenium")driver.find_element_by_id("su").click()time.sleep(3)#将页面滚动条拖到底部js="var q=document.documentElement.scrollTop=10000"driver.execute_script(js)time.sleep(3)#将滚动条移动到页面的顶部js_="var q=document.documentElement.scrollTop=0"driver.execute_script(js_)time.sleep(3)driver.quit()
0 0
原创粉丝点击