• 006 selenium css/jquery/返回对象


    '''
    时间:2018/12/04
    功能:css/jquery/返回对象
    目录:
        一: xpath
            1 id
            2 class
            3 其他
            4 路径
            5 子节点
            6 条件 
        二: jquery
            1 浏览器调试
            2 代码登录
        三: 返回对象
            1 打印输出
        四: 登录判断
            1 方法一
            2 方法二
        五: 思考
    '''

    一: xpath

      1 id

      2 class

      3 其他

      4 路径

      5 子节点

      6 条件

    二: jquery
      1 浏览器调试

      2 代码登录

    from selenium import webdriver
    import time
    
    driver = webdriver.Firefox()
    driver.get("http://127.0.0.1/zentao/user-login-L3plbnRhby8=.html")
    
    time.sleep(1)
    
    jq = '''
        $("#account").val("admin");
        $("[name = 'password']").val("123456");
        $("#keepLoginon").click();
        $("#submit").click();
        '''
    driver.execute_script(jq)

    三: 返回对象
      1 打印输出

    #coding = utf-8
    from selenium import webdriver
    
    driver = webdriver.Firefox()
    driver.get("http://www.baidu.com")
    
    # 获取标题 - 页面
    print("title: %s" %driver.title)
    
    # 获取尺寸 - 输入框
    size = driver.find_element_by_id("kw").size
    print("size: %s" %size)
    
    # 获取信息 - 底部信息
    text = driver.find_element_by_id("cp").text
    print("text: %s" %text)
    
    # 获取信息 - 标签信息
    tag = driver.find_element_by_id("kw").tag_name
    print("tag: %s" %tag)
    
    # 获取属性
    attribute = driver.find_element_by_id("kw").get_attribute("type")
    print("attribute_type: %s" %attribute)
    attribute = driver.find_element_by_id("kw").get_attribute("id")
    print("attribute_id: %s" %attribute)
    attribute = driver.find_element_by_id("kw").get_attribute("class")
    print("attribute_class: %s" %attribute)
    attribute = driver.find_element_by_id("kw").get_attribute("name")
    print("attribute_name: %s" %attribute)
    
    # 是否可见
    result = driver.find_element_by_id("kw").is_displayed()
    print("is_displayed: %s" %result)
    
    # 获取信息 - 浏览器名称
    print("name: %s" %driver.name)
    
    # 退出程序
    driver.quit()
    title: 百度一下,你就知道
    size: {'height': 22, 'width': 500}
    text: ©2018 Baidu 使用百度前必读 意见反馈 京ICP证030173号  京公网安备11000002000001号 
    tag: input
    attribute_type: text
    attribute_id: kw
    attribute_class: s_ipt
    attribute_name: wd
    is_displayed: True
    name: firefox

    四: 登录判断

      1 方法一

    from selenium import webdriver
    import time
    
    driver = webdriver.Firefox()
    driver.get("http://127.0.0.1/zentao/user-login-L3plbnRhby8=.html")
    
    time.sleep(1)
    jq = '''
        $("#account").val("admin");
        $("[name = 'password']").val("123456");
        $("#keepLoginon").click();
        $("#submit").click();
        '''
    driver.execute_script(jq)
    
    time.sleep(1)
    text = driver.find_element_by_xpath(".//*[@id='topnav']/a[1]").text
    if(u"退出" == text):
        print("登录成功")
    else:
        print("登录失败")
    
    is_displayed = driver.find_element_by_xpath(".//*[@id='topnav']/a[1]").is_displayed()
    print(is_displayed)
    if(True == is_displayed):
        print("登录成功")
    else:
        print("登录失败")
    登录成功
    True
    登录成功

      2 方法二

    from selenium import webdriver
    import time
    
    driver = webdriver.Firefox()
    driver.get("http://127.0.0.1/zentao/user-login-L3plbnRhby8=.html")
    
    time.sleep(1)
    jq = '''
        $("#account").val("admin");
        $("[name = 'password']").val("1234567");
        $("#keepLoginon").click();
        $("#submit").click();
        '''
    driver.execute_script(jq)
    
    try:
        time.sleep(1)
        is_displayed = driver.find_element_by_xpath(".//*[@id='topnav']/a[1]").is_displayed()
        print("登录成功")
    except:
        print("登录失败")
    登录成功

    五: 思考

    1 jquery语法和css语法类型。
    2 jquery可以解决,selenium有时点击无效的问题。
  • 相关阅读:
    UI自动化测试(五)TestNG简介与安装步骤
    selenium webdriver 右键另存为下载文件(结合robot and autoIt)
    SpringBoot系列之日志框架介绍及其原理简介
    SpringBoot系列之profles配置多环境(篇二)
    SpringBoot系列之Spring容器添加组件方式
    MySQL基础之自连接用法简介
    MySQL基础之Natural Join用法
    MySQL基础之STRAIGHT JOIN用法简介
    SpringBoot系列之配置文件加载位置
    SpringBoot系列之外部配置用法简介
  • 原文地址:https://www.cnblogs.com/huafan/p/10066753.html
Copyright © 2020-2023  润新知