玩转UI自动化测试4

1.selenium 执行JS操作

 <1>.处理readonly属性,只读属性,如果需要编辑,则将readonly=false去掉

   driver.execute_script("document.getElementById('text').readOnly=false")

   以上代码,修改文本框属性为可以输入

   <2>. 修改按钮,由不可点击变成可点击

   driver.execute_script("document.getElementById('button').disabled=''")

   <3>. 补充,文本框清空再输入操作

   text = driver.find_element_by_id("text")

   text.clear()

   text.send_keys("哈哈")

2. selenium 等待

 <1>.强制等待 time.sleep

     driver.find_element_by_class_name("wait").click()

   time.sleep(5)

   print(driver.find_element_by_class_name("red").text)

 <2>.全局等待

   只针对于找页面的元素,如果页面需要等待一段时间,还是要用sleep

     driver.implicitly_wait(5)  最长等待5秒,每0.5秒轮询一次,找到了就继续往后操作,超过5秒找不到就报错

    driver.implicitly_wait(5)
    driver.find_element_by_class_name("wait").click()
    print(driver.find_element_by_class_name("red").text)
  使用try except 捕获异常,处理错误
    driver.implicitly_wait(30)
    try:
   driver.find_element_by_id("广告弹窗").click()
    except:
      print("元素未找到")

<3>.隐式等待
  等待自定义的条件出现,有时候需要自己写代码扩展
  from selenium.webdriver.support.wait import WebDriverWait
import selenium.webdriver.support.expected_conditions as ec
  #等待10秒,等到alert出现
  #点击自动化学习小站的下单按钮
  driver.find_element_by_class_name("order_confirm").click()
  #等待10秒,等到alert出现
  WebDriverWait(driver, 10).until(ec.alert_is_present())
  alert = driver.switch_to.alert
  #打印alert弹窗的文本内容
  print(alert.text)
  #关闭弹窗
  alert.accept()
  #总结:大部分情况下加了全局等待,sleep都可以去掉,全局等待只针对于页面的元素,找到了就马上执行下一步的操作
  #如果需要让页面稳定一点再进行下一步的操作,那么还是需要用sleep

3. selenium 多元素定位

# 多元素定位, 返回的是element对象,返回的是列表,以下代码,包含price都会展示出来
# 找不到返回空列表,不会报错,判断列表长度就知道有没有找到driver.find_elements_by_class_name("price")
for i in driver.find_elements_by_class_name("price"):
i.click()
print(i.text)

4. selenium之xpath定位

 xml表达式练习,xml与htnl结构类似  https://www.w3school.com.cn/example/xmle/books.xml

  复杂的元素定位,需要使用xpath,基本上所有元素都能定位的到

   # xpath表达式,能找到的元素多一些

   # css 选择器,执行效率比xpath快

   # xpath表达式第一个:/表示路径,根节点

  # 查找根节点下bookstore下面的book下面的title
# /bookstore/book/title
 # xpath表达式第二个:// 递归路径 ,可能会找到多个,一般结合谓语一起使用
 # []谓语 一般跟//递归路径一起使用,表示什么条件的元素
 # /bookstore/book[1]  ---数字--索引 找bookstore下的第一本书, xpath索引从1开始
 # /bookstore/book[@cover] 是属性就加@符号,找cover属性存在的
 # /bookstore/book[@category='web'] 是属性就加@符号,找(category)分类是web的书
 # /bookstore/book[@category='web' and @cover] 是属性就加@符号,找(category)分类是web,且cover属性存在的书
 # /bookstore/book[@category='web' or @category='children'] 是属性就加@符号,找(category)分类是web, 或者分类是children的书
 # /bookstore/book[not(@category='web')] 找(category)分类不是web的书
 # //title[text()="Learning XML"] 找title中text文本值为Learning XML
 # /bookstore/book/title[text()='Learning XML']/../ 找title中text文本值为Learning XML的book
 # /bookstore/book[title="Learning XML"] 上面的简便写法

 # 还可以写包含 contains
 # 分类是web 且作者包含James的书的价格
 # //book[@category='web' and contains(author,'James')]/price
 # //book[@category='web' and not(contains(author,'James'))]/price
 # //book/* book下所有,*表示所有

 # count的使用
 # 3.选取 单个作者 且 定价大于30的书的分类签
 # //book[count(author)=1 and price>30]/@categroy

5. 获取cookie

  根据cookie的name去获取cookie

  driver.get_cookie()

  获取所有的cookie

  driver.get_cookies()

6.UI自动化如何减少脚本维护量

 <1>.尽量减少页面上的操作,能用get尽量用get跳转,不要点击

 <2>.元素定位有id用id,有name 用name,xpath尽量按逻辑写,不要按照索引写

 <3>.最终的结果验证不要写的过于具体,写包含错误也可以

 
 
   
     
posted @ 2022-06-05 16:37  maxwell11  阅读(36)  评论(0编辑  收藏  举报