Example illustration of selenium + python getting table data

  • 2020-12-09 00:56:26
  • OfStack

Method 1:


<code class="language-python">""" 
 According to the table the id Properties and table In a 1 The element locates its in table The position of  
table Including the header, position coordinates are from 1 Begin to calculate  
tableId : table the id attribute  
queryContent : The content that needs to be located  
""" 
def get_table_content(tableId,queryContent): 
  arr = [] 
  arr1 = []  
  table_loc = (By.ID,tableId) 
  #  According to the row query table data, the extracted data is 1 Entire line, separated by space 1 Columns of data  
  table_tr_list = driver.find_element(*table_loc).find_elements(By.TAG_NAME, "tr") 
  for tr in table_tr_list: 
    arr1 = (tr.text).split(" ") # Divide into Spaces ( The number is the same as the number of columns )1 D list  
    # print(tr.text) 
    # print(arr1) 
    arr.append(arr1)  # Compose the tabular data 2 The list of d  
 
  # To iterate over table Data, determine the location of the query data  
  for i in range(len(arr)): 
    for j in range(len(arr[i])): 
      if queryContent== arr[i][j]: 
        print("%r Coordinate for (%r,%r)" %(queryContent,i+1,j+1)) 
 
get_table_content("myTable"," The first 2 Line first 2 column ")</code> 

Method 2:


"""
 According to the table the id Properties and table In a 1 The element locates its in table The position of 
table Including the header, position coordinates are from 1 Begin to calculate 
tableId : table the id attribute 
queryContent : The content that needs to be located 
"""
def get_table_content(tableId,queryContent):
 
  #  According to the row query table data, the extracted data is 1 Entire line, separated by space 1 Columns of data 
  table_tr_list = driver.find_element(By.ID, tableId).find_elements(By.TAG_NAME, "tr")
  table_list = [] # store table data 
  for tr in table_tr_list:  # Traverse every 1 a tr
    # each 1 a tr Data basis of td Query out, return the result is list object 
    table_td_list = tr.find_elements(By.TAG_NAME, "td")
    row_list = []
    print(table_td_list)
    for td in table_td_list:  # Traverse every 1 a td
      row_list.append(td.text)  # Take the data out of the table and put it in the rows and columns table 
    table_list.append(row_list)
 
  #  To iterate over table Data, determine the location of the query data 
  for i in range(len(table_list)):
    for j in range(len(table_list[i])):
      if queryContent== table_list[i][j]:
        print("%r Coordinate for (%r,%r)" %(queryContent,i+1,j+1))
 
get_table_content("myTable"," The first 2 Line first 2 column ")

Method 3:


"""
 According to the CSS The selector and table In a 1 The element locates its in table The position of 
table Including the header, position coordinates are from 1 Begin to calculate 
cssSelector : table the CSS Selector attribute 
queryContent : The content that needs to be located 
"""
def get_table_content(cssSelector,queryContent):
  locator = cssSelector + ">tbody>tr"
  table_list = []
  table_tr_list = driver.find_elements(By.CSS_SELECTOR, locator)[1:] # Remove the header 
  for tr in table_tr_list:
    row_list = []
    table_td_list = tr.find_elements(By.TAG_NAME,"td")
    for td in table_td_list:
      row_list.append(td.text)
    table_list.append(row_list)
 
  for i in range(len(table_list)):
    for j in range(len(table_list[i])):
      if queryContent== table_list[i][j]:
        print("%r Coordinate for (%r,%r)" %(queryContent,i+1,j+1))
 
get_table_content("#myTable"," The first 2 Line first 2 column ")

Related articles: