实用selenium+python实现web自动化测试第八节
本節重點:
調用js方法
·?execute_script(script,?*args)
在當前窗口/框架?同步執行javaScript
腳本:JavaScript的執行。
*參數:適用任何JavaScript腳本。
使用:
driver.execute_script(‘document.title’)
?
?
使快播登陸用戶名輸入框標紅顯示:
#coding=utf-8
from?selenium?import?webdriver
import?time
?
driver = webdriver.Firefox()
driver.get("http://passport.kuaibo.com/login/?referrer=http%3A%2F%2Fvod.kuaibo.com%2F%3Ft%3Dhome")
?
#給用戶名的輸入框標紅
js="var q=document.getElementById(\"user_name\");q.style.border=\"1px solid red\";"
#調用js
driver.execute_script(js)
time.sleep(3)
?
driver.find_element_by_id("user_name").send_keys("username")
driver.find_element_by_id("user_pwd").send_keys("password")
driver.find_element_by_id("dl_an_submit").click()
time.sleep(3)
?
driver.quit()
js解釋:
q=document.getElementById(\"user_name\")
元素q的id?為user_name
q.style.border=\"1px?solid?red\
元素q的樣式,邊框為1個像素紅色
?
隱藏元素
js.html
<html>
????<head>
??????<meta?http-equiv="content-type"?content="text/html;charset=utf-8"?/>
??????<title>js</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">
????????$(document).ready(function(){
??????????$('#tooltip').tooltip({"placement": "right"});
????????});
??????</script>
????</head>
?
????<body>
??????<h3>js</h3>
??????<div?class="row-fluid">
????????<div?class="span6 well">????????
??????????<a?id="tooltip"?href="#"?data-toggle="tooltip"?title=" selenium-webdriver(python)">hover to see tooltip</a>
??????????<a?class="btn">Button</a>
????????</div>??????
??????</div>????????
????</body>
????<script?src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
??</html>
?
(保持html文件與執行腳本在同一目錄下)
執行js一般有兩種場景:
·?一種是在頁面上直接執行JS
·?另一種是在某個已經定位的元素上執行JS
?
#coding=utf-8
from?selenium?import?webdriver
import?time,os
?
driver = webdriver.Firefox()
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()
?
js解釋:
arguments對象,它是調用對象的一個特殊屬性,用來引用Arguments對象。Arugments對象就像數組。
fadeOut()?方法使用淡出效果來隱藏被選元素,假如該元素是隱藏的。
?
PS:可以看到js?可以做selenium?做不到的事情,但是在什么樣的自動化的時候才能(或必須)要js幫忙,我還沒遇到過。不過js可以selenium完成更強大的功能,這是不容置疑的。
另外,之前沒有學過JS?,所以js代碼很陌生。如果有時間的話也建議各位同學補充這方面的知識。UI自動化離不開前端技術。
?
本節重點:
·?上傳文件
文件上傳操作也比較常見功能之一,上傳功能沒有用到新有方法或函數,關鍵是思路。
上傳過程一般要打開一個本地窗口,從窗口選擇本地文件添加。所以,一般會卡在如何操作本地窗口添加上傳文件。
其實,在selenium??webdriver?沒我們想的那么復雜;只要定位上傳按鈕,通send_keys添加本地文件路徑就可以了。絕對路徑和相對路徑都可以,關鍵是上傳的文件存在。下面通地例子演示。
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>
?
upload.py
#coding=utf-8
from?selenium?import?webdriver
import?os,time
?
driver = webdriver.Firefox()
?
#腳本要與upload_file.html同一目錄
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()
?
?
其它有些應用不好找,所以就自己創建頁面,這樣雖然麻煩,但腳本代碼突出重點。
這里找一139郵箱的實例,有帳號的同學可以測試一下~!
(登陸基礎版的139郵箱,網盤模塊上傳文件。)
139upload.py
?
#coding=utf-8
from?selenium?import?webdriver
import?os,time
?
driver = webdriver.Firefox()
?
driver.get("http://m.mail.10086.cn")
driver.implicitly_wait(30)
?
#登陸
driver.find_element_by_id("ur").send_keys("手機號")
driver.find_element_by_id("pw").send_keys("密碼")
driver.find_element_by_class_name("loading_btn").click()
time.sleep(3)
?
#進入139網盤模塊
driver.find_element_by_xpath("/html/body/div[3]/a[9]/span[2]").click()
time.sleep(3)
?
#上傳文件
driver.find_element_by_id("id_file").send_keys('D:\\selenium_use_case\upload_file.txt')
time.sleep(5)
?
driver.quit()
轉載于:https://www.cnblogs.com/longyu4356/p/9342368.html
總結
以上是生活随笔為你收集整理的实用selenium+python实现web自动化测试第八节的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 淘宝API系列,商品详情数据的获取(数据
- 下一篇: php封装协议查看zip,支持的协议和封