zoukankan      html  css  js  c++  java
  • WebElement类的方法

    WebElement类的方法

    1、清空(clear())

    #!/usr/bin/python3
    from selenium import webdriver
    import time
    
    chrome = webdriver.Chrome()
    chrome.get('https://www.baidu.com')
    chrome.find_element_by_id('kw').send_keys('python') #在百度输入框输入’python‘
    time.sleep(3)
    chrome.find_element_by_id('kw').clear()     #清空输入框的内容
    time.sleep(3)
    

    2、获取元素的属性值(get_attribute())

    #!/usr/bin/python3
    from selenium import webdriver
    import time
    
    chrome = webdriver.Chrome()
    chrome.get('https://www.baidu.com')
    input_box = chrome.find_element_by_id('kw')  #定位到输入框
    print("百度输入框的class属性值是:%s"%input_box.get_attribute('class'))
    
    time.sleep(3)
    

    结果:
    百度输入框的class属性值是:s_ipt

    #!/usr/bin/python3
    from selenium import webdriver
    import time
    
    chrome = webdriver.Chrome()
    chrome.get('https://www.baidu.com')
    input_box = chrome.find_element_by_id('kw')  #定位到输入框
    input_box.send_keys('selenium')              #在输入框输入‘selenium’
    print("百度输入框内输入的值是:%s"%input_box.get_attribute('value'))
    
    time.sleep(3)
    

    结果:
    百度输入框内输入的值是:selenium

    3、检查元素是否可见(is_displayed())

    #!/usr/bin/python3
    from selenium import webdriver
    import time
    
    chrome = webdriver.Chrome()
    chrome.get('https://www.baidu.com')
    input_box = chrome.find_element_by_id('kw')
    print('百度输入框是否可见:%s'%input_box.is_displayed())
    

    结果:
    百度输入框是否可见:True

    4、检查元素是否可编辑(is_enabled())

    #!/usr/bin/python3
    from selenium import webdriver
    import time
    
    chrome = webdriver.Chrome()
    chrome.get('https://www.baidu.com')
    input_box = chrome.find_element_by_id('kw')
    print('百度输入框是否可见:%s'%input_box.is_enabled())
    

    结果:
    百度输入框是否可见:True

    5、检查元素是否已被选中(is_selected())

    #!/usr/bin/python3
    from selenium import webdriver
    import time
    
    chrome = webdriver.Chrome()
    chrome.get('https://www.baidu.com')
    chrome.find_element_by_link_text('登录').click()    #在百度首页点击登录
    
    time.sleep(1)  #等待1S(不等待的话可能页面未加载完导致后面元素定位不到)
    chrome.find_element_by_id('TANGRAM__PSP_11__footerULoginBtn').click()  #用户名登录
    check_box = chrome.find_element_by_id('TANGRAM__PSP_11__memberPass')  #定位到下次自动登录复选框
    check_box.click()   #取消选定状态
    
    print('是否选中:%s'%check_box.is_selected())
    time.sleep(3)
    

    结果:
    是否选中:False

    6、提交(submit())

    #!/usr/bin/python3
    from selenium import webdriver
    import time
    
    chrome = webdriver.Chrome()
    chrome.get('https://www.baidu.com')
    chrome.find_element_by_id('kw').send_keys('软件测试')
    chrome.find_element_by_id('su').submit()
    
    time.sleep(3)
    
  • 相关阅读:
    Oracle存储过程基础
    LoadRunner常用知识点-----LoadRunner日志输出
    Action(8):Error-26608:HTTP Status-Code=504(Gateway Time-out)
    Action(8):Error -27728:Step download timeout(120 seconds)has expired when downloading
    windows7无声音,提示未插入扬声器或耳机的解决
    解决loadrunner 脚本和replaylog中的中文乱码问题
    LoadRunner11-遇到问题及解决办法(汇总)
    java内存溢出分析工具
    Loadrunner乱码问题
    LoadRunner常用函数
  • 原文地址:https://www.cnblogs.com/jingxindeyi/p/13053161.html
Copyright © 2011-2022 走看看