zoukankan      html  css  js  c++  java
  • 常用方法

    1. 启动浏览器

    driver = webdriver.Firefox()

    2. 通过get方法访问目标url

    driver.get("http://www.baidu.com")

    3. 刷新当前网页

    self.driver.refresh()

    4. 当前窗口最大化

    self.driver.maximize_window()

    5. 获取页面的html源代码

    pageSource = self.driver.page_source

    6. 调用driver的title 属性 获取页面的title属性

    title = self.driver.title

    7. 获取当前页面的url地址

    currentPageUrl1 = self.driver.current_url

    8. 获取当前窗口句柄

    now_handle = self.driver.current_window_handle

    9. 断言当前页面的title属性值是否是“百度一下,你就知道”

    self.assertEqual(title,"百度一下,你就知道","页面title属性值错误")

    10. 断言标题是"搜狗搜索引擎"

    assert self.driver.title.find("搜狗搜索引擎") >= 0,"assert error"

    11. 判断页面元素是否可操作

    print(nowsElement.is_enabled)

    12. 获取页面元素,找到输入框元素

    searchBox = self.driver.find_element_by_id("query")

    13. 获取搜索输入框页面元素的name属性值

    print(searchBox.get_attribute("name"))

    14. 清楚输入框中的内容

    input = self.driver.find_element_by_id("kw")

    imput.clear()

    15. 单击

    Button = self.driver.find_element_by_id("button")

    Button.click()

    16. 双击某个元素

    from selenium.webdriver import ActionChains

    input = self.driver.find_element_by_id("su")

    action_chains = ActionChains(self.driver)

    action_chains.double_click(input).perform()

    17. 单选下拉列表 遍历多个选项并显示所有选项的文本和选项值

        # 使用name属性找到页面上的name属性为"fruit"的下拉列表元素,也可以根据属性值选择或根据文本选择 

         select = self.driver.find_element_by_xpath("/html/body/select")

          all_options = select.find_elements_by_tag_name("option")

          for option in all_options:

                 print("选项显示的文本:",option.text)

                 print("选项值为:",option.get_attribute("value”))

                 option.click()

                 time.sleep(3)

    18. 拖动元素

    initialPosition = self.driver.find_element_by_id("draggable")

            targetPosition = self.driver.find_element_by_id("draggable2")
            dragElement = self.driver.find_element_by_id("draggable3")
            from selenium.webdriver import ActionChains
            '''
            创建一个新的ActionChains,将webdriver 实例对象driver 作为参数值传入
            然后通过WebDriver实例执行用户动作
            '''
            action_chains = ActionChains(self.driver)
            # 将页面上第一个能被拖拽的元素拖拽到第二个元素的位置
            action_chains.drag_and_drop(initialPosition,targetPosition).perform()
            # 架构页面上第三个能拖拽的元素向右下拖动10个像素,共拖动5次
            for i in range(5):
                action_chains.drag_and_drop_by_offset(dragElement,10,10).perform()

                time.sleep(2)

    19. 组合按键

        from selenium.webdriver import ActionChains

         ActionChains(self.driver).key_down(keys.control).send_keys('a').key_up(keys.control).perform

    20. 长按鼠标

        from selenium.webdriver import ActionChains

        # 在ID属性值为'div1'的元素上执行按下鼠标左键,并保持

        ActionChains(self.driver).click_and_hold(div).perform()

        # 在id 属性值为"div1"的元素上释放一直按下的鼠标左键

        ActionChains(self.driver).release(div).perform()

    21. 鼠标悬停

        from selenium.webdriver import ActionChains

        # 将鼠标悬浮到第一个链接元素上

        ActionChains(self.friver).move_to_element(link1).perform()
    ————————————————
    版权声明:本文为CSDN博主「test_豆」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/doudou617/article/details/81020726

  • 相关阅读:
    Python接口测试实战5(下)
    Python接口测试实战4(上)
    Python接口测试实战3(下)- unittest测试框架
    Python接口测试实战3(上)- Python操作数据库
    Python接口测试实战2
    Python接口测试实战1(下)- 接口测试工具的使用
    Python接口测试实战1(上)- 接口测试理论
    python+request+Excel做接口自动化测试
    Android软件测试Monkey测试工具
    Python循环语句
  • 原文地址:https://www.cnblogs.com/hd-test/p/11737296.html
Copyright © 2011-2022 走看看