当前位置:   article > 正文

Airtest全新升级兼容Selenium 4.0,快来看看更新的内容吧_airtest 可以用xpath吗

airtest 可以用xpath吗

在上周更新推文中提到,我们Airtest-Selenium更新到了1.0.6版本,新增支持Selenium4.0的语法,那么我们来看一下Airtest-Selenium更新后有什么新的内容吧~

selenium 4.0有什么新功能

selenium4.0最主要的还是定位元素方法的更新,与旧版本的selenium代码写法存在一些差异,变得更简洁明了。

1. 定位单个元素方法的更新

首先我们来看一下定位元素方法的更新,AirtestIDE同时兼容新旧两种写法,将find_element_by_xpath()的方式更新为find_element(),目前使用AirtestIDE的Selenium窗口录制脚本输出的仍然为旧写法。但是我们是可以兼容运行新写法的~

更新前写法(Selenium3):

  1. driver.find_element_by_class_name("className")
  2. driver.find_element_by_css_selector(".className")
  3. driver.find_element_by_id("elementId")
  4. driver.find_element_by_link_text("linkText")
  5. driver.find_element_by_name("elementName")
  6. driver.find_element_by_partial_link_text("partialText")
  7. driver.find_element_by_tag_name("elementTagName")
  8. driver.find_element_by_xpath("xpath")
  9. 更新后写法(Selenium4):
  1. #注意使用新写法的时候要引用selenium的By
  2. from selenium.webdriver.common.by import By
  3. driver.find_element(By.CLASS_NAME,"xx")
  4. driver.find_element(By.CSS_SELECTOR,"xx")
  5. driver.find_element(By.ID,"xx")
  6. driver.find_element(By.LINK_TEXT,"xx")
  7. driver.find_element(By.NAME,"xx")
  8. driver.find_element(By.PARTIAL_LINK_TEXT,"xx")
  9. driver.find_element(By.TAG_NAME,"xx")
  10. driver.find_element(By.XPATH,"xx")

2. 定位多个元素方法的更新

与上述定位元素一样,定位多个元素方法的更新将find_elements_by_xpath()的方式换成了find_elements(),下面是一些写法的变化:

更新前写法(Selenium3):

  1. driver.find_elements_by_class_name("className")
  2. driver.find_elements_by_css_selector(".className")
  3. driver.find_elements_by_id("elementId")
  4. driver.find_elements_by_link_text("linkText")
  5. driver.find_elements_by_name("elementName")
  6. driver.find_elements_by_partial_link_text("partialText")
  7. driver.find_elements_by_tag_name("elementTagName")
  8. driver.find_elements_by_xpath("xpath")

更新后写法(Selenium4):

  1. #注意使用新写法的时候要引用selenium的By
  2. from selenium.webdriver.common.by import By
  3. driver.find_elements(By.CLASS_NAME,"xx")
  4. driver.find_elements(By.CSS_SELECTOR,"xx")
  5. driver.find_elements(By.ID,"xx")
  6. driver.find_elements(By.LINK_TEXT,"xx")
  7. driver.find_elements(By.NAME,"xx")
  8. driver.find_elements(By.PARTIAL_LINK_TEXT,"xx")
  9. driver.find_elements(By.TAG_NAME,"xx")
  10. driver.find_elements(By.XPATH,"xx")

3. Selenium 4新增了相对定位

selenium更新到4.0以上的版本后,新增了一个对元素相对定位的支持,能根据某些原点元素作为参考去定位该元素附近的其他元素,目前可用的相对定位有:

  • above 元素的上方
  • below 元素的下方
  • toLeftOf 元素的左方
  • toRightOf 元素的右方
  • near 元素的附近
  1. #假如有九宫格button元素分别排布着1-9,如计算器排布方式
  2. text5 = driver.find_element(By.NAME, "5") #以数字5为原点元素的基准
  3. #在数字5的上面是数字8
  4. text8 = driver.find_element(locate_with(By.TAG_NAME, "button").above(text5))
  5. #在数字5的下面是数字2
  6. text2 = driver.find_element(locate_with(By.TAG_NAME, "button").below(text5))
  7. #在数字5的左面是数字4
  8. text4 = driver.find_element(locate_with(By.TAG\_NAME, "button").to_left_of(text5))
  9. #在数字5的右面是数字6
  10. text6 = driver.find_element(locate_with(By.TAG_NAME, "button").to_right_of(text5))
  11. #默认寻找数字5附近含有该TAG_NAME且离数字5最近的元素
  12. Near = driver.find_element(locate_with(By.TAG_NAME, "button").near(text5))

在 AirtestIDE 上跑 selenium4.0 的新方法

1. 以定位单个元素方法的更新为例

在这里插入图片描述

可以看到,我们这边可以混合使用两种写法的,都可以正常识别并进行正常的操作的,具体参考代码如下:

  1. # -*- encoding=utf8 -*-
  2. __author__ = "AirtestProject"
  3. from airtest.core.api import *
  4. from selenium import webdriver
  5. from selenium.webdriver.common.keys import Keys
  6. from airtest_selenium.proxy import WebChrome
  7. from selenium.webdriver.common.by import By
  8. driver = WebChrome()
  9. driver.implicitly_wait(20)
  10. #打开百度网站
  11. driver.get("https://www.baidu.com")
  12. sleep(2.0)
  13. #在搜索框中搜索2024两会
  14. searchbox = driver.find_element(By.ID,"kw")
  15. searchbox.send_keys("2024两会")
  16. searchbox.submit()
  17. driver.switch_to_new_tab()
  18. #点击进入其中一个直播回放画面
  19. driver.find_element(By.XPATH,"//*[@id=\"2\"]/div/div/div/div[3]/div/div/div[4]/a/button/span").click()
  20. driver.switch_to_new_tab()
  21. sleep(3.0)
  22. #识别视频的简述内容
  23. el=driver.find_element(By.CLASS_NAME,"title-desc").text
  24. print(el)

2. 以元素相对定位方法的更新为例

注意:如果使用AirtestIDE原生环境跑测的同学们可能会发现出现了这个报错信息:No module named ‘selenium.webdriver.support.relative_locator’,这个是AirtestIDE环境下的兼容性问题,目前我们已经在排期兼容了,后续有新的兼容信息会通知大家!

在这里插入图片描述
虽然AirtestIDE的原生环境会有一定兼容性的报错,但是可以通过更换python路径为本地的python环境就可以使用新的相对定位的方法啦~(PS:前提是本地的python环境下的selenium以及Airtest-Selenium的版本皆为目前最新版本)

图片

具体更换AirtestIDE的环境为本地python环境的详细方法,可以点击查看我们的教程文档:https://airtest.doc.io.netease.com/IDEdocs/3.4run_script/0_run_script/#4
下面我们利用一个小小的例子,来看一下相对定位的实现情况吧。

图片

可以看到可以通过一个已知的原点元素以及目标元素的所在方位可以通过相对定位去直接寻找,在日常编写脚本的时候可以更方便快捷。

参考代码如下:

  1. # -*- encoding=utf8 -*-
  2. __author__ = "AirtestProject"
  3. from airtest.core.api import *
  4. from selenium import webdriver
  5. from selenium.webdriver.common.keys import Keys
  6. from airtest_selenium.proxy import WebChrome
  7. driver = WebChrome()
  8. driver.implicitly_wait(20)
  9. from selenium.webdriver.common.by import By
  10. from selenium.webdriver.support.relative_locator import locate_with
  11. #打开计算机网站
  12. driver.get("http://www.0756jia.com/")
  13. sleep(2.0)
  14. #以数字5为相对定位的原点
  15. text5 = driver.find_element(By.ID,"simple5")
  16. #在数字5上方是数字8
  17. text8 = driver.find_element(locate_with(By.TAG_NAME, "A").above(text5))
  18. text8.click()
  19. #在数字5下方是数字2
  20. text2 = driver.find_element(locate_with(By.TAG_NAME, "A").below(text5))
  21. text2.click()
  22. #在数字5左方是数字4
  23. text4 = driver.find_element(locate_with(By.TAG_NAME, "A").to_left_of(text5))
  24. text4.click()
  25. #在数字5右方是数字6
  26. text6 = driver.find_element(locate_with(By.TAG_NAME, "A").to_right_of(text5))
  27. text6.click()
  28. #在数字5旁边是数字8
  29. Near = driver.find_element(locate_with(By.TAG_NAME, "A").near(text5))
  30. Near.click()

小结与注意事项

4.1 AirtestIDE更新了Airtest-Selenium,支持了Selenium 4.0的新定位语法

AirtestIDE更新了Airtest-Selenium到1.0.6版本,支持了Selenium4.0的新定位语法,包括了单个以及多个元素的定位语法,将find_element_by_xpath()的方式换成了find_element()。

4.2 在本地python环境中,更新airtest-selenium、selenium,可以支持4.0新功能

虽然在AirtestIDE的原生环境中,暂时不支持进行该相对定位方式,但是可以在本地的python环境中,将Airtest-Selenium、Selenium4到更新到最新版本后,可使用新增的元素的相对定位方法。

在更新Airtest-Selenium的时候,别忘了将自己环境下的selenium更到4.0以上的版本

  1. #更新airtest-selenium
  2. pip install -U airtest-selenium
  3. #更新selenium
  4. pip install -U selenium

4.3 chrome与chromedriver对应问题

大家想用新版本的chrome浏览器进行自动化测试很久了,我们兼容了目前新版本的chrome浏览器,但是要注意的是要将AirtestIDE环境下以及本地环境下的chromedriver更换成与自己chrome版本对应的才可以噢!

旧版chromedriver下载地址:https://chromedriver.storage.googleapis.com/index.html
新版chromedriver下载地址:https://googlechromelabs.github.io/chrome-for-testing/

4.4小结

目前Airtest-Selenium的1.0.6版本兼容了selenium4.0的语法,我们可以更简单快捷的完成我们想要的自动化测试效果,可以更好的联动浏览器去完成更多的内容。同时我们也欢迎大家给我们投稿你们想要实现的selenium实操例子,也非常欢迎热心同学给我们投稿自己实现的脚本例子~

如同学们在使用新版的Airtest-Selenium时遇到了一些问题无法解决,可以通过此网站向我们的开发者快速提单:https://airtest.netease.com/issue_create 。

可以在标题中加入“Airtest-Selenium1.0.6”之类的字眼。方便我们快速筛选和排查。

 

总结:

感谢每一个认真阅读我文章的人!!!

作为一位过来人也是希望大家少走一些弯路,如果你不想再体验一次学习时找不到资料,没人解答问题,坚持几天便放弃的感受的话,在这里我给大家分享一些自动化测试的学习资源,希望能给你前进的路上带来帮助。

  1. 文档获取方式:

  2. 加入我的软件测试交流群:680748947免费获取~(同行大佬一起学术交流,每晚都有大佬直播分享技术知识点)

这份文档,对于想从事【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!

以上均可以分享,只需要你搜索vx公众号:程序员雨果,即可免费领取

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/675310
推荐阅读
相关标签
  

闽ICP备14008679号