zoukankan      html  css  js  c++  java
  • Selenium 实战到吹牛系列:七

    Selenium 实战到吹牛系列


    PS:遍历 HTML 整个表格(Table)

    例子

    HTML:

    <html>
    <head>
    <title>测试页面</title>
    </head>
    <body>
    	<meta charset="UTF-8">
        <table border="3">
          <thead>
            <tr>
              <th>类型</th>
              <th>周一</th>
              <th>周二</th>
              <th>Option</th></tr>
          </thead>
          <tbody>
            <tr>
              <td>苹果</td>
              <td>1</td>
              <td>2</td>
              <td><input type="checkbox">A选项</input></td>
              </tr>
            <tr>
              <td>水梨</td>
              <td>3</td>
              <td>21</td>
              <td><input type="checkbox">B选项</input></td>
            <tr>
              <td>香蕉</td>
              <td>43</td>
              <td>123</td>
              <td><input type="checkbox">C选项</input></td>
            <tr>
              <td>西红柿</td>
              <td>16</td>
              <td>39</td>
              <td><input type="checkbox">D选项</input></td>
    </body>
    </html>
    

    代码:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Date    : 2019-07-05 16:12:33
    # @Author  : BenLam
    # @Link    : https://www.cnblogs.com/BenLam/
    
    from selenium import webdriver
    driver=webdriver.Firefox()
    driver.get(r'test.html')
    
    #定位并获取表格参数
    tr = driver.find_elements_by_tag_name('tr')
    #for循环输出表里的文字
    tr_text_list = [row.text for row in tr for col in row.find_elements_by_tag_name('td')]
    print(tr_text_list)
    
    #方法二:
    tr = driver.find_elements_by_tag_name('tr')
    
    for row in tr:
        for col in row.find_elements_by_tag_name('td'):
            print(col.text + '	',end='')
        print('
    ')
    
    driver.quit()
    
    

    输出结果:

    >>>['苹果 1 2 A选项', '苹果 1 2 A选项', '苹果 1 2 A选项', '苹果 1 2 A选项', '水梨 3 21 B选项', '水梨 3 21 B选项', '水梨 3
     21 B选项', '水梨 3 21 B选项', '香蕉 43 123 C选项', '香蕉 43 123 C选项', '香蕉 43 123 C选项', '香蕉 43 123 C选项', '西
    红柿 16 39 D选项', '西红柿 16 39 D选项', '西红柿 16 39 D选项', '西红柿 16 39 D选项']
    >>>
    

    例子_B

    PS: 勾选【Option】里边的选项

    代码:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Date    : 2019-07-05 16:12:33
    # @Author  : BenLam
    # @Link    : https://www.cnblogs.com/BenLam/
    
    from selenium import webdriver
    driver=webdriver.Firefox()
    driver.get(r'test.html')
    
    #定位并获取表格参数
    tr = driver.find_elements_by_tag_name('tr')
    #for循环输出表里的文字
    tr_text_list = [row.text for row in tr for col in row.find_elements_by_tag_name('td')]
    print(tr_text_list)
    
    #勾选【A选项】
    Option = driver.find_element_by_xpath(r'//*[@type="checkbox"][1]').click()
    
    driver.quit()
    

    例子_C

    PS: 获取表格中的列
    代码:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Date    : 2019-07-05 16:12:33
    # @Author  : BenLam
    # @Link    : https://www.cnblogs.com/BenLam/
    
    from selenium import webdriver
    driver=webdriver.Firefox()
    driver.get(r'test.html')
    
    #定位并获取表格参数
    tr = driver.find_elements_by_tag_name('tr')
    
    # 获取表中所有的 “thead -> th”
    thead = tr[0].find_elements_by_tag_name('th')
    print(len(thead))
    
    # 获取某个单元格的值
    tr_text = tr[1].find_elements_by_tag_name('td')[0].text
    print(tr_text)
    
    # 循环获取表格中的第一列
    for num in range(1, len(tr)):
        print(tr[num].find_elements_by_tag_name('td')[0].text)
    
    driver.quit()
    

    输出结果:

    >>>print(len(thead))
    4
    >>>print(tr_text)
    '苹果'
    >>> for num in range(1, len(tr)):
    ...     print(tr[num].find_elements_by_tag_name('td')[0].text)
    ...
    '苹果'
    '水梨'
    '香蕉'
    '西红柿'
    
  • 相关阅读:
    LinuxShell脚本攻略--第八章 当个好管家
    LinuxShell脚本攻略--第六章 B计划
    LinuxShell脚本攻略--第三章 以文件之名
    LinuxShell脚本攻略--第二章 命令之乐
    LinuxShell脚本攻略--第一章 小试牛刀
    TCP/IP 与OSI结构图
    网络号和主机号的计算
    IP地址分类及私网IP
    转:Cache相关
    原码 反码 补码 移码
  • 原文地址:https://www.cnblogs.com/BenLam/p/11138779.html
Copyright © 2011-2022 走看看