-
安裝pip
- 輸入
sudo easy_install pip
命令 - 輸入用戶密碼驾中,回車就開(kāi)始安裝pip了
- 輸入
-
確認(rèn)安裝pip
- 輸入
pip -V
- 正確顯示
pip 9.0.1 from /Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg (python 2.7)
- 輸入
-
安裝appium-python-client
- 輸入
sudo pip install Appium-Python-Client
安裝 -
本地安裝
- 或者去github上下載ZIP
- 解壓后,進(jìn)入目錄
- 輸入
python setup.py install
安裝
- 輸入
-
安裝Selenium
- 輸入
sudo pip install selenium -i http://pypi.douban.com/simple
安裝 -
本地安裝
- 或者去下載安裝包
- 解壓后鹰椒,進(jìn)入目錄
- 輸入
python setup.py install
安裝
- 輸入
-
Selenium降級(jí)安裝
- 進(jìn)入
/Library/Python/2.7/site-packages
目錄 - 把里面selenium開(kāi)頭的文件全部刪除就可以了
-
輸入
sudo pip --default-timeout=300 install selenium==2.53.5
--default-timeout=300 為超時(shí)時(shí)間
selenium==2.53.5 說(shuō)明下載的版本號(hào)為2.53.5
- 進(jìn)入
-
確認(rèn)安裝Selenium
- 輸入
pip show selenium
-
顯示
Name: selenium Version: 2.53.5 Summary: Python bindings for Selenium Home-page: https://github.com/SeleniumHQ/selenium/ Author: UNKNOWN Author-email: UNKNOWN License: UNKNOWN Location: /Library/Python/2.7/site-packages Requires:
- 輸入
-
安裝Appium客戶端
- 去下載安裝包
-
配置參數(shù)
-
編寫(xiě)Python測(cè)試文件
- 最簡(jiǎn)單Demo:?jiǎn)?dòng)App
#coding=utf-8
import time
from appium import webdriver
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1.1'
desired_caps['deviceName'] = 'HUAWEI SCL-AL00'
desired_caps['appPackage'] = 'com.duolabao.customer'
desired_caps['appActivity'] = 'com.duolabao.customer.activity.BootActivity'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
-
運(yùn)行Demo
- 連接手機(jī)
- Launch客戶端Appium
- 進(jìn)入
demo.py
文件所在目錄 - 輸入命令
python demo.py
大功告成
大功告成
大功告成
錯(cuò)誤指南
- No module named appium
在腳本中會(huì)有:from appium import webdriver
第一次運(yùn)行時(shí)可能會(huì)遇到這樣的error:No module named appium
之所以會(huì)報(bào)這樣的error是因?yàn)闆](méi)有裝client
client下載地址 :https://github.com/appium/python-client
下載下來(lái)后肥橙,python setup.py install
- [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/test-easy-install-1433.pth'
running install
Checking .pth file support in /Library/python/2.7/site-packages/
error: can't create or remove files in install directory
The following error occurred while trying to add or remove files in the
installation directory:
[Errno 13] Permission denied: '/Library/Python/2.7/site-packages/test-easy-install-1433.pth'
The installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:
/Library/Python/2.7/site-packages/
Perhaps your account does not have write access to this directory? If the
installation directory is a system-owned directory, you may need to sign in
as the administrator or "root" account. If you do not have administrative
access to this machine, you may wish to choose a different installation
directory, preferably one that is listed in your PYTHONPATH environment
variable.
For information on other options, you may wish to consult the
documentation at:
https://pythonhosted.org/setuptools/easy_install.html
Please make the appropriate changes for your system and try again.
--->>>解決:sudo python setup.py install
- 其他問(wèn)題
問(wèn)題一:
問(wèn)題org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
解決辦法:
(1)啟動(dòng)appium
(2)運(yùn)行cmd 輸入 adb devices -l 查看UDID 如圖:
(3)再在cmd中輸入 appium -a 127.0.0.1 -p4723 -Uf4a4d8bb (-a表示ip,-p表示端口,-U表示設(shè)備的udid 可以通過(guò)appium -h查看更多命令)
(4)如果如下圖所示 就表示 appium服務(wù)啟動(dòng)成功了,注意這個(gè)窗口不要關(guān)閉 因?yàn)檫@是appium的服務(wù) 關(guān)了就關(guān)了服務(wù)帐萎,后面過(guò)程無(wú)法執(zhí)行比伏,而且這個(gè)窗口也是 日志輸出的窗口用于排錯(cuò)胜卤。
問(wèn)題二:
問(wèn)題:org.openqa.selenium.SessionNotCreatedException: A new session could not be created.
(Original error: Device ZX1C622BU3 was not in the list of connected devices)
(WARNING: The server did not provide any stacktrace information)
解決辦法:可能手機(jī)驅(qū)動(dòng)未安裝成功,雖然在adb devices中可以正常查看到devices
問(wèn)題三:
問(wèn)題:org.openqa.selenium.SessionNotCreatedException: A new session could not be created. (Original error: Waited 20 secs for selendroid server and it never showed up)
解決辦法:
查看包的簽名的和Activity
例如:APP從I5版本后啟動(dòng)界面的capabilities.setCapability("appActivity", ".CordovaApp");
從I5版本后Acitivity變成了MainActivity
capabilities.setCapability("appActivity",".MainActivity");// 被測(cè)app的Activity類
問(wèn)題四:
org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. (WARNING: The server did not provide any stacktrace information)
解決方法:
查看設(shè)備上是否安裝Selendroid,或者升級(jí)Selendroid
IOS問(wèn)題五:
org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. (WARNING: The server did not provide any stacktrace information)
解決辦法:IOS_webkit_debug_proxy –c –[Udid] -d
相關(guān)資料
pip安裝
appium-python-client安裝
簡(jiǎn)單Demo
webdriver.Remote報(bào)錯(cuò)
selenium下載地址
selenium降級(jí)
selenium的坑
selenium安裝
常用函數(shù)(一)
新建實(shí)例driver = webdriver.Chrome()
1.通過(guò)標(biāo)簽屬性Id查找元素
方法:find_element_by_id(element_id)
實(shí)例:driver.find_element_by_id("iptUsername")
2.通過(guò)標(biāo)簽屬性name查找元素
方法:find_element_by_name(element_name)
實(shí)例:driver.find_element_by_id("inputPwname")
3.通過(guò)標(biāo)簽Xpath路徑查找元素
方法:find_element_by_xpath(xpath)
實(shí)例:driver.find_element_by_xpath("http://div[@id='menuContainer']/div/div/ul/li[4]/a")
4.通過(guò)標(biāo)簽名tagname查找元素
方法:find_element_by_tag_name(tag_name)
實(shí)例:driver.find_element_by_tag_name("input")
注意:通過(guò)tag_name查找時(shí)使用find_element_by_tag_name查找到的是第一個(gè)標(biāo)簽的tag_name
5.通過(guò)標(biāo)簽中的元素文本鏈接查找元素
方法:find_element_by_link_text(link_text)
實(shí)例:driver.find_element_by_link_text('登 錄')
6.通過(guò)標(biāo)簽的class屬性查找元素
方法:find_elements_by_class_name(class_name)
實(shí)例:driver.find_elements_by_class_name("x-panel-body")
7.通過(guò)css樣式查找元素
方法:find_element_by_css_selector()
實(shí)例:driver.find_element_by_css_selector("input.btn") 溫馨提示:find_elements_by_id()查詢到的是一個(gè)集合赁项,如果id名字重復(fù)的時(shí)候可以采取find_elements_by_id()葛躏,其他的查詢方式同理;
8.瀏覽器中加載url
方法:get(url)
實(shí)例:driver.get("http//:www.baidu.com")
9.向前
方法:forward()
實(shí)例:driver.forward()
10.返回當(dāng)前會(huì)話中的cookies
方法:get_cookies()
實(shí)例:driver.get_cookies()
11.根據(jù)cookie name 查找
方法:driver.get_cookie(cookie_name)
實(shí)例:driver.get_cookie("NET_SessionId")
12.截取當(dāng)前頁(yè)面
方法:
get_screenshot_as_file(filename)
實(shí)例:driver.
get_screenshot_as_file("D:\\Program Files\\Python27\\NM.bmp")
13.獲取當(dāng)前窗口的坐標(biāo)
方法:get_window_position()
實(shí)例:driver.get_window_position()
14.獲取當(dāng)前窗口的長(zhǎng)和寬
方法:get_window_size()
實(shí)例:driver.get_window_size()
常用函數(shù)(二)
新建實(shí)例driver = webdriver.Chrome()
1.獲取當(dāng)前頁(yè)面的Url函數(shù)
方法:current_url
實(shí)例:
driver.current_url
2.獲取元素坐標(biāo)
方法:location
解釋:首先查找到你要獲取元素的悠菜,然后調(diào)用location方法
實(shí)例:
driver.find_element_by_xpath("http://*[@id='tablechart']/tbody/tr[14]/td[9]").location
3.表單的提交
方法:submit
解釋:查找到表單(from)直接調(diào)用submit即可
實(shí)例:
driver.find_element_by_id("form1").submit()
4.獲取CSS的屬性值
方法:value_of_css_property(css_name)
實(shí)例:
driver.find_element_by_css_selector("input.btn").value_of_css_property("input.btn")
5.獲取元素的屬性值
方法:get_attribute(element_name)
實(shí)例:
driver.find_element_by_id("sellaiyuan").get_attribute("sellaiyuan")
6.判斷元素是否被選中
方法:is_selected()
實(shí)例:
driver.find_element_by_id("form1").is_selected()
7.返回元素的大小
方法:size
實(shí)例:
driver.find_element_by_id("iptPassword").size
返回值:{'width': 250, 'height': 30}
8.判斷元素是否顯示
方法:is_displayed()
實(shí)例:
driver.find_element_by_id("iptPassword").is_displayed()
9.判斷元素是否被使用
方法:is_enabled()
實(shí)例:
driver.find_element_by_id("iptPassword").is_enabled()
10.獲取元素的文本值
方法:text
實(shí)例:driver.find_element_by_id("iptUsername").text
11.元素賦值
方法:send_keys(*values)
實(shí)例:
driver.find_element_by_id("iptUsername").send_keys('admin')
注意如果是函數(shù)需要增加轉(zhuǎn)義符u,eg.
driver.find_element_by_id("iptUsername").send_keys(u'青春')
12.返回元素的tagName
方法:tag_name
實(shí)例:
driver.find_element_by_id("iptUsername").tag_name
13.刪除瀏覽器所以的cookies
方法:delete_all_cookies()
實(shí)例:
driver.delete_all_cookies()
14.刪除指定的cookie
方法:delete_cookie(name)
實(shí)例:deriver.delete_cookie("my_cookie_name")
15.關(guān)閉瀏覽器
方法:close()
實(shí)例:driver.close()
16.關(guān)閉瀏覽器并且推出驅(qū)動(dòng)程序
方法:quit()
實(shí)例:driver.quit()
17.返回上一頁(yè)
方法:back()
實(shí)例:driver.back()
18.設(shè)置等待超時(shí)
方法:implicitly_wait(wait_time)
實(shí)例:driver.implicitly_wait(30)
19.瀏覽器窗口最大化
方法:maximize_window()
實(shí)例:driver.maximize_window()
20.查看瀏覽器的名字
方法:name
實(shí)例:drvier.name