Home > Software design >  Python Selenium - I created Nested List to put table data but showing wrong items order
Python Selenium - I created Nested List to put table data but showing wrong items order

Time:08-24

Python Selenium - I created Nested List to put table data but showing wrong item order. The items on the nested list must be in the correct order like this [[lotto game, combinations, draw date, jackpot, winners], [...], [...], [...]]. When I tried to print the nested list value it gave wrong items order. I tried to change the code many times, more than a week has passed but I still can't do it. What's wrong with this code? Any suggestion?

# Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53)
# Selenium 3.141.0
# PyCharm Community Edition 4.0.5

from selenium import webdriver
from selenium.webdriver.support.ui import Select
import time

firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference('permissions.default.stylesheet', 2)
firefox_profile.set_preference('permissions.default.image', 2)

driver = webdriver.Firefox(executable_path='C:\\Users\\Mikey\\Downloads\\geckodriver-v0.30.0-win64\\geckodriver', firefox_profile=firefox_profile)
driver.get("https://www.pcso.gov.ph/SearchLottoResult.aspx")

time.sleep(15)

select = Select(driver.find_element_by_id('cphContainer_cpContent_ddlStartMonth'))
select.select_by_value('April')

select = Select(driver.find_element_by_id('cphContainer_cpContent_ddlSelectGame'))
select.select_by_visible_text('Lotto 6/42')

driver.find_element_by_id('cphContainer_cpContent_btnSearch').click()

time.sleep(7)

table = driver.find_element_by_id('cphContainer_cpContent_GridView1')
table_row = []
list_elem = []
counter = 0
for row in table.find_elements_by_tag_name('tr'):
    for cell in row.find_elements_by_tag_name('td'):
        counter  = 1
        if counter <= 5:
            list_elem.append(cell.text)
        elif counter >= 6:
            table_row.append(list_elem)
            list_elem = []
            counter = 0

print(table_row)
print('length:', len(table_row), '\n')

assert "Philippine Charity Sweepstakes Office" in driver.title
print(driver.title)

driver.close()

CodePudding user response:

I think this is what you need:

driver.get("https://www.pcso.gov.ph/SearchLottoResult.aspx")

time.sleep(5)

select = Select(driver.find_element(By.ID, 'cphContainer_cpContent_ddlStartMonth'))
select.select_by_value('April')

select = Select(driver.find_element(By.ID, 'cphContainer_cpContent_ddlSelectGame'))
select.select_by_visible_text('Lotto 6/42')

driver.find_element(By.ID,'cphContainer_cpContent_btnSearch').click()

time.sleep(4)

table = driver.find_element(By.ID,'cphContainer_cpContent_GridView1')
table_row = []
list_elem = []
counter = 0
for row in table.find_elements(By.TAG_NAME,'tr'):
    for cell in row.find_elements(By.TAG_NAME, 'td'):
        list_elem.append(cell.text)
        if len(list_elem) == 5:
            table_row.append(list_elem)
            list_elem=[]

print(table_row)
print('length:', len(table_row), '\n')

assert "Philippine Charity Sweepstakes Office" in driver.title
print(driver.title)
driver.close()

here is the output:

[['Lotto 6/42', '29-35-12-36-07-03', '4/21/2022', '30,084,914.40', '0'], ['Lotto 6/42', '21-37-28-22-39-02', '4/23/2022', '34,194,443.60', '0'], ['Lotto 6/42', '18-15-17-24-33-10', '4/26/2022', '38,170,552.20', '0'], ['Lotto 6/42', '39-24-33-06-42-25', '4/28/2022', '42,434,650.00', '0'], ['Lotto 6/42', '17-25-21-33-39-04', '4/30/2022', '47,018,808.20', '0'], ['Lotto 6/42', '07-22-19-20-40-10', '5/3/2022', '51,412,148.00', '1'], ['Lotto 6/42', '19-07-04-41-23-09', '5/5/2022', '5,940,000.00', '0'], ['Lotto 6/42', '14-24-08-16-22-37', '5/7/2022', '6,051,682.00', '0'], ['Lotto 6/42', '07-13-18-02-28-37', '5/10/2022', '8,509,629.80', '0'], ['Lotto 6/42', '15-20-33-37-10-02', '5/12/2022', '11,692,275.80', '0'], ['Lotto 6/42', '19-25-18-33-10-31', '5/14/2022', '15,020,774.00', '0'], ['Lotto 6/42', '13-08-15-33-37-22', '5/17/2022', '18,149,335.60', '0'], ['Lotto 6/42', '24-40-18-03-08-34', '5/19/2022', '22,290,301.60', '0'], ['Lotto 6/42', '37-34-32-35-08-36', '5/21/2022', '26,320,485.80', '0'], ['Lotto 6/42', '13-29-26-11-12-34', '5/24/2022', '30,265,196.20', '0'], ['Lotto 6/42', '34-41-04-29-19-18', '5/26/2022', '34,985,545.40', '0'], ['Lotto 6/42', '09-17-23-11-13-30', '5/28/2022', '39,311,022.60', '0'], ['Lotto 6/42', '25-20-29-07-21-02', '5/31/2022', '43,912,343.80', '3'], ['Lotto 6/42', '28-36-14-37-13-03', '6/2/2022', '5,940,000.00', '0'], ['Lotto 6/42', '24-35-04-07-22-02', '6/4/2022', '6,768,840.80', '0'], ['Lotto 6/42', '22-09-38-36-42-13', '6/7/2022', '9,696,500.00', '0'], ['Lotto 6/42', '39-33-18-01-24-36', '6/9/2022', '12,998,501.00', '1'], ['Lotto 6/42', '02-20-34-40-27-22', '6/11/2022', '5,940,000.00', '0'], ['Lotto 6/42', '42-38-21-06-01-41', '6/14/2022', '6,051,521.20', '0'], ['Lotto 6/42', '33-31-13-41-25-18', '6/16/2022', '9,423,997.80', '1'], ['Lotto 6/42', '08-15-11-23-36-33', '6/18/2022', '5,940,000.00', '1'], ['Lotto 6/42', '07-18-12-17-06-14', '6/21/2022', '5,940,000.00', '0'], ['Lotto 6/42', '24-05-13-39-06-40', '6/23/2022', '6,457,370.20', '0'], ['Lotto 6/42', '27-24-40-14-39-04', '6/25/2022', '10,518,111.40', '0'], ['Lotto 6/42', '18-31-26-08-23-29', '6/28/2022', '13,886,702.20', '0'], ['Lotto 6/42', '31-38-13-20-14-18', '6/30/2022', '18,353,331.00', '0'], ['Lotto 6/42', '17-10-15-09-35-01', '7/2/2022', '23,545,622.40', '0'], ['Lotto 6/42', '06-18-17-11-31-29', '7/5/2022', '28,572,306.60', '0'], ['Lotto 6/42', '09-10-42-08-13-41', '7/7/2022', '34,838,186.80', '2'], ['Lotto 6/42', '27-18-26-13-02-03', '7/9/2022', '5,940,000.00', '1'], ['Lotto 6/42', '09-33-35-12-31-08', '7/12/2022', '5,940,000.00', '0'], ['Lotto 6/42', '13-08-42-03-02-15', '7/14/2022', '6,848,076.80', '0'], ['Lotto 6/42', '41-32-31-04-08-38', '7/16/2022', '10,733,280.80', '0'], ['Lotto 6/42', '18-09-16-12-36-14', '7/19/2022', '14,283,746.80', '0'], ['Lotto 6/42', '40-19-16-07-10-21', '7/21/2022', '18,367,924.00', '0'], ['Lotto 6/42', '17-11-23-21-05-25', '7/23/2022', '23,061,318.00', '2'], ['Lotto 6/42', '24-30-32-37-02-05', '7/26/2022', '5,940,000.00', '0'], ['Lotto 6/42', '37-22-25-08-26-15', '7/28/2022', '6,627,802.60', '0'], ['Lotto 6/42', '14-33-32-11-12-15', '7/30/2022', '10,233,856.00', '0'], ['Lotto 6/42', '01-14-26-29-27-18', '8/2/2022', '13,796,166.60', '0'], ['Lotto 6/42', '29-32-12-23-01-21', '8/4/2022', '18,013,724.80', '0'], ['Lotto 6/42', '04-31-37-20-05-39', '8/6/2022', '22,414,970.60', '0'], ['Lotto 6/42', '42-19-15-40-03-33', '8/9/2022', '26,895,199.20', '0'], ['Lotto 6/42', '42-18-26-35-02-19', '8/11/2022', '32,075,959.80', '1'], ['Lotto 6/42', '26-12-20-24-39-41', '8/13/2022', '5,940,000.00', '1'], ['Lotto 6/42', '13-19-12-05-08-25', '8/16/2022', '5,940,000.00', '0'], ['Lotto 6/42', '25-33-12-07-05-18', '8/18/2022', '6,567,885.60', '0'], ['Lotto 6/42', '35-01-34-18-28-31', '8/20/2022', '10,196,551.40', '0']]
length: 53 

Philippine Charity Sweepstakes Office

Process finished with exit code 0

Please verify

  • Related