Objective
Table tables are often encountered in Web pages, especially in the background operations page. This article explains in detail how table tables are positioned.
I. Understanding table
1. First look at what the table looks like, for example, this mesh table is table
2. The source code is as follows: (Save with txt text, suffix to HTML)
<! DOCTYPE html>
<meta charset= "UTF-8" > <!--for HTML5
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>table Test Templates </title>
<body>
<table border= "1" id= "myTable" >
<tr>
<TH>QQ Group </th>
No. </th> <TH>QQ
<th> Group master </th>
</tr>
<tr>
<td>selenium Automation </td>
<td>232607095</td>
<td>YOYO</td>
</tr>
<tr>
<td>appium Automation </td>
<td>512200893</td>
<td>YOYO</td>
</tr>
</table>
</body>
Second, table features
1.table Page view source code generally have these obvious tags: table, tr, TH, TD
2.<table> mark a table
3.<tr> marks a row in the middle of the table
4.</th> Defining table header cells
5.</td> Define a cell label, a set of <td> tags will create a cell,<td> label must be placed in the <tr> tag
Third, XPath positioning table
1. For example: I want to locate the "selenium Automation" element in the table, which can be positioned with XPath:
.//*[@id = ' myTable ']/tbody/tr[2]/td[1]
2. The format of the positioning is fixed, just change the number of the TR and TD is OK. As in the second row, the first column tr[2]td[1].
Not familiar with XPath syntax can see this article Selenium2+python Automated 7-xpath Positioning
Iv. Printing the contents of a table
1. Navigate to the text values in the table and print out the script as follows
Five, 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
Selenium2+python Automation 28-table Positioning "reprint"