zoukankan      html  css  js  c++  java
  • python+selenium元素定位01——显式、隐式等待

    前言:

    selenium中导入本地html时,路径引用:
    
    import os
    from selenium import webdriver
    
    current = os.getcwd()
    chrome_driver_path =os.path.join(current,'../webdriver/chromedriver')
    page_path = os.path.join(current,'../pages/wait.html')    #本地html文件
    driver = webdriver.Chrome(executable_path=chrome_driver_path)
    driver.get('file://'+page_path)

    一、等待操作

          1.1显式等待

             特点:

             1)全局设置 对find_element、find_elements生效

    2)每隔500ms在界面进行一次检查,如果检查到了就不报错
    3)下面每个find_element、find_elements都会检查1~20秒
     举例:
     driver.implicitly_wait(20)
     driver.find_element(By.XPATH,'//button[@id="b"]').click()
     driver.find_element(By.XPATH,'//button[@id="b"]').click()    

          1.2隐式等待

             特点:1)比较难写 2)只针对一个元素生效

     举例:
     先导入模块:from selenium.webdriver.support.wait import WebDriverWait
     再操作:
    element = WebDriverWait(driver,20).until(lambda x:x.find_element(By.XPATH,'//button[@id="b"]'))
    print(element.get_attribute('class'))
    
     #其中匿名函数使用说明:add=lambda x,y:x+y
    print(add(34))
             

    二、鼠标键盘事件

          2.1鼠标操作(需添加ActionChains类)

              context_click():右击

              double_click():双击

              drag_and_drop():拖动

              move_to_element():鼠标移动到某个元素上

              click_and_hold():按下鼠标左键在一个元素上

              举例:

             鼠标右击操作:

              from selenium.webdriver.common.action_chains import   ActionChains

              mouse.context_click(元素对象).perform()

              模拟鼠标点击:
              ActionChains(driver).click(元素对象).release(元素对象).perform()

          2.2键盘操作

  • 相关阅读:
    SQL SERVER 2000 安装提示"一般性网络错误" Hello
    转:关于C#程序路径的问题 Hello
    VS2003提示错误:"无法在web服务器上启用调试,您不具备调试此应用程序的权限" Hello
    转贴:轻松实现坐标转换不同地理位置系统转换入门 Hello
    XP系统优化 Hello
    Explorer.exe出错无法打开我的电脑! Hello
    TreeView控件的使用 Hello
    系统提示:‘状态:驱动程序已启用但尚未开始使用’ Hello
    深入理解 go slice
    go 语言 time 时区问题 疑问
  • 原文地址:https://www.cnblogs.com/miaoxiaochao/p/12635775.html
Copyright © 2011-2022 走看看