• python+appium【第五章(扩展)-基本元素操作】


    上次教大家识别元素,现在教大家一些手机操作,以及一些扩展内容(示例代码:澎湃新闻)

    获取信息

    • 获取元素的文本信息
      # 与selenium一致
      
      text = driver.find_element(By.XPATH,'//x[@x="x"]').text
      print(text)
      示例:
      text = driver.find_element(By.XPATH, '//android.widget.ImageView[@bounds="[1217,2628][1329,2730]"]')
      print(text)
    • 获取屏幕分辨率
      window = driver.get_window_size()
      print(window)
    • 获取屏幕左上方坐标
      location = driver.location
      print(location)

    元素断言操作

    对于原生应用来讲,元素断言可以通过获取displayed、enabled、selected属性值,然后通过条件语句if来进行判断

    • 查看元素是否可见
      is_displayed(self):此元素是否可见。隐藏元素和被控件挡住无法操作的元素(仅限H5和webview支持)
      
      示例: 
      # 查看元素是否可见 is_displayed()
      phone = driver.find_element(By.XPATH, '//android.widget.EditText[@text="输入11位手机号"]').is_displayed()
      print(phone)
    • 查看元素是否可用
      is_enabled(self):此元素是否可用。元素灰色和无法操作的元素(仅限H5和webview支持)
      
      # 查看元素是否可用 is_enabled
      enabled = driver.find_element(By.XPATH,
                                    '//android.widget.TextView[@resource-id="com.wondertek.paper:id/get_verification_code"]').is_enabled()
      print(enabled)
    • 查看元素是否被选中
      is_selected(self):此元素是否被选中。适用于单选框、复选框等类型的元素(仅限H5和webview支持)
      
      select = driver.find_element(By.XPATH,
                                   '//android.widget.CheckBox[@resource-id="com.wondertek.paper:id/checkbox_agreement"]').is_selected()
      print(select)

    基本按键类操作

    • 锁屏
      # 锁屏3s  ==》 交叉事件
      driver.lock(3)
      print(driver.is_locked()) # 判断是否锁屏,是则返回false
    • 解锁
      # 解锁
      driver.unlock()
      time.sleep(3)
      print(driver.is_locked())
    • 组合使用
      import time
      from appium import webdriver
      from appium.webdriver.webdriver import By
      from appium.webdriver.connectiontype import ConnectionType

      des = {
      "platformName": "Android",
      "platformVersion": "9",
      "deviceName": "mac虚拟机",
      "appPackage": "com.wondertek.paper",
      "appActivity": "cn.thepaper.paper.ui.main.MainActivity",
      "udid": "192.168.56.104:5555",
      "noReset": "True"
      }

      driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des)

      time.sleep(2)


      # 锁屏3s ==》 交叉事件 driver.lock(3) print(driver.is_locked()) # 判断是否锁屏,是啧返回false # 解锁 driver.unlock() time.sleep(3) print(driver.is_locked())
    • 横竖屏设置
      # 横竖屏设置
      driver.orientation = "LANDSCAPE" # 设置横屏
      time.sleep(3)
      driver.orientation = "PORTRAIT" # 设置竖屏
    • 打开通知栏
      # 打开通知栏
      driver.open_notifications()
      
      print(driver.orientation)
    • 网络配置
      network_connection:获取网络状态,返回整型数字0(None)、1(AirplaneMode)、2(Wifionly)、4(Dataonly)、6(Allnetworkon)

      # 网络配置 print(driver.network_connection) # 查看当前网络状态 driver.set_network_connection(
      1) # 飞行模式 0 1 2 4 6 time.sleep(5)

      # set_network_connection(self,connection_type):设置网络状态,使用数字或导入ConnectionType类进行传参设置 driver.set_network_connection(ConnectionType.ALL_NETWORK_ON) # 全开

    • 截屏操作
      driver.save_screenshot('pengpai.png')  # 传入存放路径
    • 按键操作
      按键类操作API讲解:按键类操作用来模拟在手机设备上进行按键操作,常用API如下:
      1、press_keycode(self,keycode,metastate=None,flags=None):模拟按键输入,其中:keycode:发送到设备的键值编码可以通过AndroidKeyCode进行查询对应数值metastate:将被发送的元信息flags:设置的按键事件标记

      import time
      from appium import webdriver from appium.webdriver.webdriver import By from appium.webdriver.connectiontype import ConnectionType des = { "platformName": "Android", "platformVersion": "9", "deviceName": "mac虚拟机", "appPackage": "com.android.settings", "appActivity": "com.android.settings.Settings", "udid": "192.168.56.104:5555", "noReset": True } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', des) time.sleep(2) driver.find_element(By.XPATH, '//android.widget.TextView[@text="在设置中搜索"]').click() time.sleep(1) driver.press_keycode(29, 64, 59) # a 打开左边shift键的开关 按住左边shift键 == A 组合键 time.sleep(1) driver.press_keycode(29, 128, 60) # a 打开右边的shift键开关 按住右边shift键 == A time.sleep(1) driver.press_keycode(29, 1048576) # a 打开大小写开关 time.sleep(1) driver.press_keycode(29, 1) # 1 打开shift键

    后续更新

  • 相关阅读:
    ubuntu更换阿里源
    记一次开源软件的篡改
    linux下搜索指定内容
    随笔_1
    单细胞中的细胞类型划分
    scDNA-seq genomic analysis pipline
    NIH周三讲座视频爬虫
    ggplot2_bubble
    TCGA数据批量下载
    lncRNA芯片重注释
  • 原文地址:https://www.cnblogs.com/yushengaqingzhijiao/p/15305742.html
Copyright © 2020-2023  润新知