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键盘操作

  • 相关阅读:
    oracle—数据泵及常用参数
    NTP服务及时间同步
    kudu安装
    ogg12c 配置
    ogg12-ERROR OGG-01031 file D:OGGdirdated000000 is not in any allowed output directories
    ogg12c_静默安装
    git的基本使用
    redis数据库
    linux之网络
    flask框架基础
  • 原文地址:https://www.cnblogs.com/miaoxiaochao/p/12635775.html
Copyright © 2011-2022 走看看