python-appium520-2初步使用

1.录制自动化脚本

场景:启动雪球,点击我的,登陆雪球,选择手机及其他登陆,输入手机号

2.Appium客户端

客户端介绍:https://github.com/appium/appium/blob/master/docs/en/about-appium/appium-clients.md

3.使用python进行测试

3.1安装python客户端

pip install Appium-Python-Client

3.2python在线编辑器,建议使用pycharm

pip install jupyter

3.3本地开服务器

使用Go语言,不会,需要学习
./gohttpserver --upload
ifconfig en0

3.4打印session,用session开启Inspector

from appium import webdriver

caps = {}
caps["platformName"] = "Android"
caps["deviceName"] = "domo"
caps["appPackage"] = "com.xueqiu.android"
caps["appActivity"] = ".view.WelcomeActivityAlias"
caps["automationName"] = "UiAutomator2"
caps["newCommandTimeout"] = 600

driver = webdriver.Remote("http://localhost:4723/wd/hub",caps)
driver.implicitly_wait(10)
print(driver.session_id)

3.5测试代码

#python代码
# This sample code uses the Appium python client
# pip install Appium-Python-Client
# Then you can paste this into a file and simply run with Python

from appium import webdriver

caps = {}
caps["platformName"] = "android"
caps["deviceName"] = "domo"
caps["appPackage"] = "com.xueqiu.android"
caps["appActivity"] = ".view.WelcomeActivityAlias"
caps["automationName"]="Uiautomator2" #可加可不加,默认Uiautomator
caps["newCommandTimeout"]=600 #session超时时间,默认60秒。如果一段时间内没有接收到请求,session会消失 

driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)

el1 = driver.find_element_by_id("com.xueqiu.android:id/user_profile_icon")
el1.click()
el2 = driver.find_element_by_id("com.xueqiu.android:id/tv_login")
el2.click()
el3 = driver.find_element_by_id("com.xueqiu.android:id/tv_login_by_phone_or_others")
el3.click()
el4 = driver.find_element_by_id("com.xueqiu.android:id/register_phone_number")
el4.send_keys("123456789")

driver.quit()

FAQ

1.使用python执行脚本,抱如下错误

selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Error executing adbExec. Original error: 'Command '/Users/chenshanju/Library/Android/sdk/platform-tools/adb -P 5037 -s 406e8f3 install /Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-v1.18.0.apk' exited with code 1'; Stderr: 'adb: failed to install /Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-v1.18.0.apk: Failure [INSTALL_FAILED_USER_RESTRICTED: Install canceled by user]'; Code: '1'
小米手机,需要在开发者模式里允许USB调试

2.selenium.common.exceptions.InvalidSelectorException: Message: Locator Strategy 'css selector' is not supported for this session

不要使用Appium Desktop,使用Appium Server解决

3.selenium.common.exceptions.NoSuchElementException: Message: An element could not be located on the page using the given search parameters.

添加延时即可

from appium import webdriver
from time import sleep
caps = {}
caps["platformName"] = "android"
caps["deviceName"] = "domo"
caps["appPackage"] = "com.xueqiu.android"
caps["appActivity"] = ".view.WelcomeActivityAlias"
caps["newCommandTimeout"] = 600
caps["automationName"] = "UiAutomator2"

driver = webdriver.Remote("http://localhost:4723/wd/hub", caps)
driver.implicitly_wait(20)
sleep(20)
print(driver.session_id)
def test_for_element(a):
    try:
        driver.find_element_by_id(a).click()
    except Exception as e:
        print(e)
    finally:
        print(driver.session_id)
        print(a)
        sleep(3)
test_for_element("com.xueqiu.android:id/agree")
test_for_element("com.xueqiu.android:id/user_profile_icon")
test_for_element("com.xueqiu.android:id/tv_login")
test_for_element("com.xueqiu.android:id/tv_login_by_phone_or_others")
driver.find_element_by_id("com.xueqiu.android:id/register_phone_number").send_keys("123456789")
sleep(3)
print(driver.session_id)
driver.quit()
posted on 2018-10-04 10:14  singleSpace  阅读(496)  评论(0编辑  收藏  举报