一、加載firefox配置html
參考代碼:python
# coding=utf-8
from selenium import webdriver
# 配置文件地址,打開Firefox點右上角設置--幫助--故障排除信息--顯示文件夾web
profile_directory = r'C:\Users\xxx\AppData\Roaming\Mozilla\Firefox\Profiles\1x41j9of.default' #瀏覽器
# 加載配置配置
profile = webdriver.FirefoxProfile(profile_directory)
# 啓動瀏覽器配置
driver = webdriver.Firefox(profile)微信
二、單選框和複選框app
radio和checkbox源碼post
1.上圖的html源碼以下,把下面這段複雜下來,寫到文本里,後綴改爲.html就能夠了。測試
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>單選和複選</title>
</head>
<body>
</form>
<h4>單選:性別</h4>
<form>
<label value="radio">男</label>
<input name="sex" value="male" id="boy" type="radio"><br>
<label value="radio1">女</label>
<input name="sex" value="female" id="girl" type="radio">
</form>
<h4>微信公衆號:從零開始學自動化測試</h4>
<form>
<!-- <label for="c1">checkbox1</label> -->
<input id="c1" type="checkbox">selenium<br>
<!-- <label for="c2">checkbox2</label> -->
<input id="c2" type="checkbox">python<br>
<!-- <label for="c3">checkbox3</label> -->
<input id="c3" type="checkbox">appium<br>
<!-- <form>
<input type="radio" name="sex" value="male" /> Male
<br />
<input type="radio" name="sex" value="female" /> Female
</form> -->
</body>
</html> 網站
參考代碼:
# coding:utf-8
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("file:///C:/Users/Gloria/Desktop/checkbox.html")
# 沒點擊操做前,判斷選項框狀態
s = driver.find_element_by_id("boy").is_selected()
print s
driver.find_element_by_id("boy").click()
# 點擊後,判斷元素是否爲選中狀態
r = driver.find_element_by_id("boy").is_selected()
print r
# 複選框單選
driver.find_element_by_id("c1").click()
# 複選框全選
checkboxs = driver.find_elements_by_xpath(".//*[@type='checkbox']")
for i in checkboxs:
i.click()ui
三、富文本編輯框輸入文本內容
# coding:utf-8
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
profileDir = r'C:\Users\Gloria\AppData\Roaming\Mozilla\Firefox\Profiles\1x41j9of.default'
profile = webdriver.FirefoxProfile(profileDir)
driver = webdriver.Firefox(profile)
bolgurl = "http://www.cnblogs.com/"
yoyobolg = bolgurl + "yoyoketang"
driver.get(yoyobolg)
driver.find_element_by_id("blog_nav_newpost").click()
time.sleep(5)
edittile = u"Selenium2+python自動化23-富文本"
editbody = u"這裏是發帖的正文"
driver.find_element_by_id("Editor_Edit_txbTitle").send_keys(edittile)
driver.switch_to.frame("Editor_Edit_EditorBody_ifr")
driver.find_element_by_id("tinymce").send_keys(Keys.TAB)
driver.find_element_by_id("tinymce").send_keys(editbody)
四、日曆控件
日曆控件是web網站上常常會遇到的一個場景,有些輸入框是能夠直接輸入日期的,有些不能,以咱們常常搶票的12306網站爲例,詳細講解如何解決日曆控件爲readonly屬性的問題。
基本思路:先用js去掉readonly屬性,而後直接輸入日期文本內容
# coding:utf-8
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://kyfw.12306.cn/otn/index/init")
# 去掉元素的readonly屬性
js = 'document.getElementById("train_date").removeAttribute("readonly");'
driver.execute_script(js)
# 用js方法輸入日期
js_value = 'document.getElementById("train_date").value="2016-12-25"'
driver.execute_script(js_value)
# # 清空文本後輸入值
# driver.find_element_by_id("train_date").clear()
# driver.find_element_by_id("train_date").send_keys("2016-12-25")
五、table定位
.源碼以下:(用txt文本保存,後綴改爲html)
<!DOCTYPE html>
<meta charset="UTF-8"> <!-- for HTML5 -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<html>
<head>
<title>Table測試模板</title>
</head>
<body>
<table border="1" id="myTable">
<tr>
<th>QQ羣</th>
<th>QQ號</th>
<th>羣主</th>
</tr>
<tr>
<td>selenium自動化</td>
<td>232607095</td>
<td>YOYO</td>
</tr>
<tr>
<td>appium自動化</td>
<td>512200893</td>
<td>YOYO</td>
</tr>
</table>
</body>
</html>
table特徵
1.table頁面查看源碼通常有這幾個明顯的標籤:table、tr、th、td
2.<table>標示一個表格
3.<tr>標示這個表格中間的一個行
4.</th> 定義表頭單元格
5.</td> 定義單元格標籤,一組<td>標籤將將創建一個單元格,<td>標籤必須放在<tr>標籤內
# coding:utf-8from selenium import webdriverimport timeurl = 'file:///C:/Users/Gloria/Desktop/table.html'driver = webdriver.Firefox()driver.get(url)time.sleep(3)t = driver.find_element_by_xpath(".//*[@id='myTable']/tbody/tr[2]/td[1]")print t.text