edward
edward

Reputation: 43

Empty result when reading in from web table using selenium and python

inspect source

string = [ ]

bot = myBotManifesto('USERNAME','PASSWORD')

truck_num = bot.driver_web_browser.find_element_by_css_selector
('tr#ctl00_ContentPlaceHolder1_PopupControlTractores_TractorGrid_DXDataRow0>td:nth-child(6)')

string.append(truck_num.text)

print(string)

THE RESULT IS an empty list. Im not sure why. That is my main problem . Once i have that down , id like to read in the same css_selector but for the next tr in the web table.

any help or tips would be greatly appreciated.


[ ' ' ]

Upvotes: 0

Views: 183

Answers (2)

edward
edward

Reputation: 43

easier way to do this that works. however i believe in my case the information is protected. Im going to ask for advice on a different question. But this code works for me .

def click_single_tractor(self): #dynamically clicks tractor
    sleep(2)
    basecss = '#ctl00_ContentPlaceHolder1_PopupControlTractores_TractorGrid_DXMainTable > tbody > tr'
    cssbase = '#ctl00_ContentPlaceHolder1_PopupControlTractores_TractorGrid_DXHeadersRow > td'

    table_rows = self.driver_web_browser.find_elements_by_css_selector(basecss)
    table_data = self.driver_web_browser.find_elements_by_css_selector(cssbase)

    for index, tr in enumerate(table_rows,1):
        iteratingSelObj = self.driver_web_browser.find_elements_by_css_selector("{}:nth-child({})".format( basecss, str(index)))
        print('table row:',int(index))

        for indx, td in enumerate(table_data,1):
            iterate_td = self.driver_web_browser.find_element_by_css_selector("{}:nth-child({})".format( cssbase, str(indx)))
            print('table data:',int(indx))
            if indx == 6:
                #get value of element
                # add to string ? to get value
                self.mystr.append(td.text)

    print(self.mystr)

result:

table row: 1

table data: 1

table data: 2

table data: 3

table data: 4

table data: 5

table data: 6

table data: 7

table data: 8

table data: 9

table data: 10

table row: 2

table data: 1

table data: 2

table data: 3

table data: 4

table data: 5

table data: 6

table data: 7

table data: 8

table data: 9

table data: 10

table row: 3

table data: 1

table data: 2

table data: 3

table data: 4

table data: 5

table data: 6

table data: 7

table data: 8

table data: 9

table data: 10

table row: 4

(all the way up to 7 rows because thats how many there was and 10 table datas each row )

['Num. Económico', 'Num. Económico', 'Num. Económico', 'Num. Económico', 'Num. Económico', 'Num. Económico', 'Num. Económico']

Upvotes: 0

Razvan
Razvan

Reputation: 387

The problem is with the locator, that is not a stable one. You will need to define it in another way to make it unique and to don't depend on front-end code which can change each time you refresh the page or the table loads new data. If you can share your html code, we can try to help you with the locator.

Upvotes: 1

Related Questions