Selenium2+python automated 28-table positioning

foreword

    Table forms are often encountered in web pages, especially background operation pages are more common. This article explains in detail how to locate the table table.

1. Know the table

    1. First look at what the table looks like, as shown in the figure below, this kind of mesh table is all tables

    2. The source code is as follows: (Save with txt text, change the suffix to html)

<!DOCTYPE html>
<meta charset="UTF-8"> <!-- for HTML5 -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<html>  
        <head>  
            <title>Table测试模板</title>  
              
        </head>  
        <body>  
            <table border="1" id="myTable">  
                <tr>  
                    <th>QQ群</th>  
                    <th>QQ号</th>  
                    <th>群主</th>  
                </tr>  
                <tr>  
                    <td>selenium自动化</td>  
                    <td>232607095</td>  
                    <td>YOYO</td>  
                </tr>  
                <tr>  
                    <td>appium自动化</td>  
                    <td>512200893</td>  
                    <td>YOYO</td>  
                </tr>  
            </table>  
        </body>  
</html> 

Second, table features

    1. When viewing the source code on the table page, there are generally these obvious tags: table, tr, th, td

    2. <table> indicates a table

    3. <tr> marks a row in the middle of this table

    4. </th> Define the header cell

    5. </td> defines the cell tag, a group of <td> tags will create a cell, the <td> tag must be placed inside the <tr> tag

Three, xpath positioning table

    1. For example: I want to locate the "selenium automation" element in the table, here I can use xpath to locate:

.//*[@id='myTable']/tbody/tr[2]/td[1]

    2. The format of the positioning here is fixed, just change the numbers after tr and td. For example, the second row and the first column tr[2]td[1].

If you are not familiar with xpath syntax, you can read this Selenium2+python automation 7-xpath positioning

Fourth, print the table content

    1. Locate the text value in the table and print it out. The script is as follows

5. Reference code:

# coding:utf-8
from selenium import webdriver
import time
url = 'file:///C:/Users/Gloria/Desktop/table.html'
driver = webdriver.Firefox()
driver.get(url)
time.sleep(3)
t = driver.find_element_by_xpath(".//*[@id='myTable']/tbody/tr[2]/td[1]")
print t.text

Supplementary note: Some friends may encounter the situation that the table is on the ifame, and then you need to switch the iframe first.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325455762&siteId=291194637