zoukankan      html  css  js  c++  java
  • selenium_webdriver_API_定位元素

    1、用selenium webdriver打开浏览器 打开网

    # 调用selenium的webdriver
      from selenium import webdriver
    # 赋值变量 启动浏览器
      b = webdriver.Firefox()
    # 用浏览器打开网页b.get(url),可以用help(b.get)获取帮助 b
      b.get(https://baidu.com)
    # 检查如何打开的是不是百度或你要的网站
      # 检查title
        b.title
      # 关键字验证若返回 True 则正确 False 则错误
        '百度' in b,title
      # 检查 url,若返回是你想要的则正确
        b.current_url
      # url验证 若返回是True则正确 False则错误
        'baidu' in b.current_url

    2、元素定位 八种

    id定位

    from selenium import webdriver
    d = webdriver.Chrome()
    d.get('https://www.baidu.com')
    
    d.find_element_by_id("kw").send_keys("hello")
    

    name 定位

    from selenium import webdriver
    d = webdriver.Chrome()
    d.get('https://www.baidu.com')
    
    d.find_element_by_name("wd").send_keys("hello")/

    class定位

    from selenium import webdriver
    d = webdriver.Chrome()
    d.get('https://www.baidu.com')
    
    d.find_element_by_class_name('s_ipt').send_keys('hello')
    

      

    tag 定位 :

    如果懂HTML知识,我们就知道HTML是通过tag来定义功能的,比如input是输入,table是表格,等等...。每个元素其实就是一个tag,一个tag往往用来定义一类功能,我们查看百度首页的html代码,可以看到有很多div,input,a等tag,所以很难通过tag去区分不同的元素。基本上在我们工作中用不到这种定义方法,仅了解就行。下面代码仅做参考,运行时必定报

    from selenium import webdriver
    d = webdriver.Chrome()
    d.get('https://www.baidu.com')
    
    d.find_element_by_tag_name('input').send_keys('hello')
    

      

    link 定位  

      此种方法是专门用来定位文本链接的,比如百度首页右上角有“新闻”,“hao123”,“地图”等链接

    from selenium import webdriver
    import time          # 调了一个时间模块
    d = webdriver.Chrome()
    d.get('https://www.baidu.com')
    
    d.find_element_by_link_text('新闻').click()
    time.sleep(5)  # 让等待5秒
    d.quit()  
    

      

    partial_link 定位

    有时候一个超链接的文本很长很长,我们如果全部输入,既麻烦,又显得代码很不美观,这时候我们就可以只截取一部分字符串,用这种方法模糊匹配了

    from selenium import webdriver
    import time
    d = webdriver.Chrome()
    d.get('https://www.baidu.com')
    
    d.find_element_by_partial_link_text('闻').click()
    time.sleep(5)
    d.quit()
    

      

    xpath 定位

    前面介绍的几种定位方法都是在理想状态下,有一定使用范围的,那就是:在当前页面中,每个元素都有一个唯一的id或name或class或超链接文本的属性,那么我们就可以通过这个唯一的属性值来定位他们。但是在实际工作中并非有这么美好,有时候我们要定位的元素并没有id,name,class属性,或者多个元素的这些属性值都相同,又或者刷新页面,这些属性值都会变化。那么这个时候我们就只能通过xpath或者CSS来定位了。

    from selenium import webdriver
    import time
    d = webdriver.Chrome()
    d.get('https://www.baidu.com')
    # 定位搜索框,然后输入hello
    d.find_element_by_xpath('//*[@id="kw"]').send_keys('hello')
    time.sleep(5)
    d.quit()
    

      

    //*[@id="kw"]     //:从这个页面的任何一个位置开始,*:开始匹配   [] :里面的属性

    css 定位

    这种方法相对xpath要简洁些,定位速度也要快些,但是学习起来会比较难理解,这里只做下简单的介绍

    from selenium import webdriver
    import time
    d = webdriver.Chrome()
    d.get('https://www.baidu.com')
    # 加断言,若在直接就下一步 不会有什么反应,如果不在 就会报错
    assert '百度' in d.title  
    # 先清除一下搜索框
    d.find_element_by_css_selector('#kw').clear()
    d.find_element_by_css_selector('#kw').send_keys('hello')
    time.sleep(5)
    d.quit()

      

      

    # 如何获取网页里面的文本  
    from selenium import webdriver
    import time
    d = webdriver.Chrome()
    d.get('https://www.baidu.com')
    assert '百度' in d.title  # 加断言,若在直接就下一步 不会有什么反应,如果不在 就会报错
    # 定位搜索框 输入hello world
    d.find_element_by_css_selector('#kw').send_keys('hello world')
    # 定位到百度一下 点击
    d.find_element_by_xpath('//*[@id="su"]').click()
    time.sleep(3)  #记得给一个时间等待 因为搜索引擎不能立刻出来内容
    # 获取搜素的文本
    s = d.find_element_by_xpath('//*[@id="content_left"]').text
    print(s)  #打印出来看一下
    # 断言 看在不在里面
    assert 'hello world' in s
    time.sleep(5)
    d.quit()   
    # 您也可以调用quit或close。quit该退出将退出整个浏览器,而close`将关闭一个标签,但如果只有一个选项卡是开放的,默认情况下,大多数浏览器将完全退出:
    

      

    # Keys 快捷键 引用
    from selenium import webdriver
    import time
    # 要引入这个模块
    from selenium.webdriver.common.keys import Keys
    d = webdriver.Chrome()
    d.get('https://www.baidu.com')
    assert '百度' in d.title  # 加断言,若在直接就下一步 不会有什么反应,如果不在 就会报错
    
    
    
    d.find_element_by_css_selector('#kw').send_keys('hello world')
    time.sleep(3)
    # 点回车键
    d.find_element_by_xpath('//*[@id="su"]').send_keys(Keys.ENTER)
    

      

      

  • 相关阅读:
    Vertica 业务用户指定资源池加载数据
    Vertica 数据库知识汇总篇
    Oracle OCP 1Z0-053 Exam Topics
    Oracle 11gR2 RAC修改监听默认端口
    Oracle 11.2.0.4 RAC安装最新PSU补丁
    Openfiler配置RAC共享存储
    ORACLE 11gR2 DG(Physical Standby)日常维护01
    oracle 存储过程 返回结果集
    exception javax.crypto.BadPaddingException: Given final block not properly padded
    plsql 连接oracle数据库的2种方式
  • 原文地址:https://www.cnblogs.com/niunai/p/10186177.html
Copyright © 2011-2022 走看看