Appium基础篇11-元素操作之点击和输入

本篇开始介绍元素操作,先来看看元素点击和元素输入行为。前面我们已经知道了元素点击的方法是click(),我们直接在前面代码基础上,用send_keys()方法给输入框输入文字。结果,我这边报错了。

1. 编写send_keys()脚本,运行报错。

[python]  view plain  copy
  1. import os  
  2. import time  
  3. from appium import webdriver  
  4.   
  5. apk_path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))  # 获取当前项目的根路径  
  6.   
  7. desired_caps ={ 'platformName''Android',  
  8.                 'platformVersion''6.0.1',  
  9.                 'deviceName''KIW-AL10',  
  10.                 'noReset'True,  
  11.                 'appPackage''com.baidu.searchbox',  
  12.                 'appActivity''com.baidu.searchbox.SplashActivity',  
  13.                 'unicodeKeyboard'True,  
  14.                 'resetKeyboard'True  
  15.                 }  
  16.   
  17. driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)#启动app  
  18. time.sleep(3#app启动后等待3秒,方便元素加载完成  
  19. # 根据元素xpath来定位  
  20. # 点击“输入框”  
  21. driver.find_element_by_id("com.baidu.searchbox:id/baidu_searchbox").click()  
  22. # 输入字段  
  23. searchInputBox = driver.find_element_by_id('com.baidu.searchbox:id/SearchTextInput')  
  24. searchInputBox.send_keys("Appium")  
       这个脚本,我和前面对比,修改了下参数的写法,直接写到字典中去,这样可能看起来代码更简洁。在最后一行,使用了send_keys()方法来给输入框元素输入字段,点击和输入方法和selenium是一模一样的。运行一下,结果报错:we wanted {"required":["value"]} and you sent ["text","value","id","sessionId"]。检查了半天,发现代码没有问题,网上搜索一下,说是我当前selenium版本太高了,需要降级。

降级和重新安装

打开cmd分别输入以下命令:

1. python -m pip uninstall selenium

2. python -m pip install selenium==3.3.1

        我原来selenium最新版本是3.4.3,重新安装3.3.1之后,运行脚本,测试通过,发现能够在百度输入框输入“Appium”字段。从这个问题来看,我们是不是忽略了一个问题,appium运行环境有一个前提条件是不是需要安装selenium?这个问题,我来测试下。继续cmd采用上面命令来卸载selenium3.3.1版本,运行一下,果然报错:ModuleNotFoundError: No module named 'selenium',查看appium源码,确实使用selenium,用了from selenium import webdriver。所以,这里记住,Appium安装环境,还需要安装selenium。如果做过selenium的同学,可能不会里面遇到这个错误提示,但是新手学appium和没有用过selenium的人,运行元素定位的脚本就会报这个错误。看来,先学Selenium,然后学Appium是很有优势的。

猜你喜欢

转载自blog.csdn.net/qq_24857309/article/details/79664398