zoukankan      html  css  js  c++  java
  • Python(81)_selenium定位页面元素

    1、通过id来定位

    #-*-coding:utf-8-*-
    from selenium import webdriver
    f = webdriver.Chrome("D:\Documents\Downloads\chromedriver.exe")
    f.get("http://www.baidu.com")
    f.find_element_by_id('kw').send_keys('selenium')
    f.find_element_by_id('su').click()

    2、通过name来定位

    <input type="text" class="s_ipt" name="wd" id="kw" maxlength="100" autocomplete="off">
    
    #-*-coding:utf-8-*-
    from selenium import webdriver
    f = webdriver.Chrome("D:\Documents\Downloads\chromedriver.exe")
    f.get("http://www.baidu.com")
    f.find_element_by_name('wd').send_keys('selenium')
    f.find_element_by_id('su').click()
    f.quit()

    3、通过class来定位

    #-*-coding:utf-8-*-
    from selenium import webdriver
    f = webdriver.Chrome("D:\Documents\Downloads\chromedriver.exe")
    f.get("http://www.baidu.com")
    f.find_element_by_class_name('s_ipt').send_keys('selenium')
    f.find_element_by_id('su').click()
    f.quit()
    
    要查找的都是要唯一的

    4、通过tag_name来定位

    标签名    input标签  
              一般不唯一
    
    #-*-coding:utf-8-*-
    from selenium import webdriver
    f = webdriver.Chrome("D:\Documents\Downloads\chromedriver.exe")
    f.get("http://www.baidu.com")
    inputs = f.find_elements_by_tag_name('input')
    for i in  inputs:
        if i.get_attribute('autocomplete') == 'off':
            i.send_keys('selium')
    f.find_element_by_id('su').click()
    f.quit()
    
    for循环勾选所有复选框

    5、通过link_text定位

    #-*-coding:utf-8-*-
    from selenium import webdriver
    f = webdriver.Chrome("D:\Documents\Downloads\chromedriver.exe")
    f.get("http://www.baidu.com")
    
    f.find_element_by_link_text("新闻").click()
    f.quit()

    6、通过partial_link_text定位

    #-*-coding:utf-8-*-
    from selenium import webdriver
    f = webdriver.Chrome("D:\Documents\Downloads\chromedriver.exe")
    f.get("http://www.baidu.com")
    
    f.find_element_by_partial_link_text("123").click()
    f.quit()
  • 相关阅读:
    UILabel 分段改变文字颜色和字体
    ios tableView删除行
    ios 控件点击没反应的问题
    ios MJRefresh最新的一些使用
    android下水波纹效果实现
    9patch的用法,简单两句就会用了。
    有关android.support.v7.app.ActionBarActivity错误的问题
    android中fragment的使用及与activity之间的通信
    [转]CSS中继承性属性和非继承性的属性
    js中的attribute详解
  • 原文地址:https://www.cnblogs.com/sunnybowen/p/10486892.html
Copyright © 2011-2022 走看看