Selenium2+python Automation 28-table Positioning "reprint"

Source: Internet
Author: User
Tags xpath

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>
&LT;TH&GT;QQ Group </th>
No. </th> &LT;TH&GT;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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.