forked from qxf2/selenium-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10_Table_Text.py
54 lines (45 loc) · 1.67 KB
/
10_Table_Text.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Learn to parse the text within each cell of a table
DISCLAIMER: This code is aimed at Selenium BEGINNERS
For more advanced tutorials and to learn how Qxf2 writes GUI automation, please visit our:
a) Our GUI automation guides: http://qxf2.com/gui-automation-diy
b) Other GitHub repos: https://github.com/qxf2
AUTHOR: Avinash Shetty
Contact: [email protected]
SCOPE:
1) Launch Firefox driver
2) Navigate to Qxf2 Tutorial page
3) Get all the fields from the table
4) Close the browser
"""
import time
from selenium import webdriver
# Create an instance of Firefox WebDriver
driver = webdriver.Firefox()
# Maximize the browser window
driver.maximize_window()
# Navigate to Qxf2 Tutorial page
driver.get("http://qxf2.com/selenium-tutorial-main")
# KEY POINT: Logic to get the text in each cell of the table
# Find the Example table element in the page
table = driver.find_element_by_xpath("//table[@name='Example Table']")
# Use find_elements_by_xpath method to get the rows in the table
rows = table.find_elements_by_xpath("//tbody/descendant::tr")
# Create a list to store the text
result_data = []
# Go to each row and get the no of columns and the navigate to column
# Then get the text from each column
for i in xrange(0,len(rows)):
# Find no of columns by getting the td elements in each row
cols = rows[i].find_elements_by_tag_name('td')
cols_data = []
for j in xrange(0,len(cols)):
# Get the text of each field
cols_data.append(cols[j].text.encode('utf-8'))
result_data.append(cols_data)
# Print the result list
print result_data
# Pause the script for 3 sec
time.sleep(3)
# Close the browser
driver.close()