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()
  • 相关阅读:
    基于AJAX的长轮询(longpolling)方式实现简单的聊天室程序
    WPF笔记(1.4 布局)——Hello,WPF!
    WPF笔记(1.10 绘图)——Hello,WPF!
    The description of GameHall and my program experience for interview
    SQL大量数据操作 切身经验总结
    WPF笔记(1.6 数据绑定)——Hello,WPF!
    WPF笔记(1.8 资源与映射)——Hello,WPF!
    WPF笔记(2.3 StackPanel)——Layout
    WPF笔记(2.2 DockPanel)——Layout
    Description of AJAX
  • 原文地址:https://www.cnblogs.com/sunnybowen/p/10486892.html
Copyright © 2011-2022 走看看