Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Manipulating Table In A Website
#1
How would I manipulate and get information from a table in a website?

These cells have buttons but not using IDs, although ACC would work, I can only find the first one on the first cell. How do I do the other cells?

Thanks
Denise
#2
Would like to see the page.
#3
I guess you want to get text of cells in the table. Can be used several ways.

1. Select the table manually, get selected text (getsel), and parse it (findrx or tok). But I don't know is it reliable. Cells are separated by two spaces, but cell contents also may contain two spaces... Also, text parsing is not easy.

2. Find the table using htm, and get its html (HTML). Then parse (findrx). It would be reliable but even more difficult. Also, does not work in firefox.

3. Find first cell as accessible object (acc) and get other cells using accessible object navigation. This is reliable and not very difficult. You only have to read about accessible object navigation in QM help. Works on IE and FF, however something is different.

Macro for IE

Code:
Copy      Help
;This macro works in IE.

ClearOutput
;Find first cell (containing useful information) in the table.
;Since its contents is undefined, capture some object above
;the table and navigate to the cell. In the example is used
;navigation string "parent next7 first". You can discover the
;string, as well as other navigation strings, by looking at
;the object tree in the 'Find accessible object' dialog and
;experimenting with the Navigate field.

Acc a=acc("Order #" "TEXT" win(" Internet Explorer" "IEFrame") "Internet Explorer_Server" "" 0x1801 0x40 0x20000040 "parent next7 first")
;Now walk through the table using accessible object navigation
int i r
;For each row
rep ;;I don't know how many rows there are, so use rep and later break on error
,r+1; out "--- row %i ---" r
,;For each cell
,for i 0 5 ;;I know the number of columns. It is 5 (Order #, Customer, Date, Total, Sales Rep)
,,str s=a.Name
,,out s
,,if(i!=4) a.Navigate("parent next first") ;;get next cell
,;get first cell in next row
,a.Navigate("parent next6 first")
,err break ;;if error, there are no more rows
,

Macro for FF

Code:
Copy      Help
;This macro works in FF.

ClearOutput
;Find first cell (containing useful information) in the table.
;Since its contents is undefined, capture some object above
;the table and navigate to the cell. In the example is used
;navigation string "parent next7 first". You can discover the
;string, as well as other navigation strings, by looking at
;the object tree in the 'Find accessible object' dialog and
;experimenting with the Navigate field.

Acc a=acc("Order #" "TEXT" win("Mozilla Firefox" "MozillaUIWindowClass") "MozillaContentWindowClass" "" 0x1801 0x40 0x20000040 "parent next7 first")
;Now walk through the table using accessible object navigation
int i r
;For each row
rep ;;I don't know how many rows there are, so use rep and later break on error
,r+1; out "--- row %i ---" r
,;For each cell
,for i 0 5 ;;I know the number of columns. It is 5 (Order #, Customer, Date, Total, Sales Rep)
,,str s=a.Name
,,out s
,,if(i!=4) a.Navigate("parent next first") ;;get next cell
,;get first cell in next row
,a.Navigate("parent next3 first")
,err break ;;if error, there are no more rows
,

The same with array

Code:
Copy      Help
;This macro gets table cells to 2-dim array.
;Works in IE and FF.


int ff
;ff=1 ;;enable this for firefox

ClearOutput

;Create empty 2-dim array.
ARRAY(str) ar.create(5 0)
;Get first cell.
Acc a
if(ff) a=acc("Order #" "TEXT" win("Mozilla Firefox" "MozillaUIWindowClass") "MozillaContentWindowClass" "" 0x1801 0x40 0x20000040 "parent next7 first")
else a=acc("Order #" "TEXT" win(" Internet Explorer" "IEFrame") "Internet Explorer_Server" "" 0x1801 0x40 0x20000040 "parent next7 first")
;Walk through the table.
int c r
for r 0 1000000 ;;for each row
,ar.redim(-1) ;;add empty elements for 1 row
,
,for c 0 5 ;;for each cell in the row
,,ar[c r]=a.Name
,,if(c!=4) a.Navigate("parent next first")
,,
,a.Navigate(iif(ff "parent next3 first" "parent next6 first"))
,err break

;Show results.
for r 0 ar.len(2)
,out "--- row %i ---" r+1
,for c 0 ar.len(1)
,,str s=ar[c r]
,,out s
,,

These macros work in the page you emailed me (see the attached image). The output is

Code:
Copy      Help
--- row 1 ---
2660
c, k  
9/12/2007 4:45:00 PM
USD $23.80

--- row 2 ---
2659
M, N
9/12/2007 4:17:00 PM
USD $14.00

--- row 3 ---
2658
l, a
9/12/2007 3:43:00 PM
USD $42.00

--- row 4 ---
2657
p, s
9/12/2007 2:53:00 PM
USD $28.00


Attached Files Image(s)
       
#4
I also created an easy to use function that gets web table cells.

Member function Acc.WebTableToArray. Create it using menu File -> New -> New Member Function.
Code:
Copy      Help
function ARRAY(str)&an [ARRAY(str)&av] [flags] [$separator] ;;flags: 1 trim spaces in names

;Gets text of cells in a table in a web page.
;Most content in web pages is in tables, even if you don't see
;borders and grid. For example you can get list of links.


;This function cannot be used with tables whose cells contain other
;tables, because it gets only first level accessible objects in cells.


;This Acc variable must represent accessible object that is a child of the first cell you need. If you cannot capture the object using the Find Accessible Object dialog (for example its text is not known at run time), you can capture some other object and use post-navigation string.
;an - array that will be populated with accessible object names of this and subsequent cells. Can be 0 if not needed.
;av - array that will be populated with accessible object values of this and subsequent cells. Can be useful for example to get text of input boxes or URLs of links. Can be 0 if not needed.
;separator - string to be used to separate text of multiple objects in a cell (if the cell contains more than 1 object). Default: " ".


;EXAMPLE
;ARRAY(str) an
;Acc a=acc("Order #" "TEXT" win(" Internet Explorer" "IEFrame") "Internet Explorer_Server" "" 0x1801 0x40 0x20000040 "parent next6 first")
;a.WebTableToArray(an)
;;display results
;out "------ results ------"
;int i
;for(i 0 an.len) out an[i]
;out "------ results with row and column numbers, assuming there are 7 columns ------"
;int r c nc=7
;for r 0 an.len/nc

,;out "--- row %i ---" r+1
,;for c 0 nc
,,;out "column %i: %s" c+1 an[r*nc+c]


if(&an) an.redim
if(&av) av.redim
if(!len(separator)) separator=" "

Acc a2 a3
Navigate("parent" a2)
rep
,if(a2.Role=ROLE_SYSTEM_CELL)
,,a2.Navigate("first" a3)
,,if(&an)
,,,str& sn=an[an.redim(-1)]
,,,sn=a3.Name; if(flags&1) sn.trim
,,if(&av)
,,,str& sv=av[av.redim(-1)]
,,,sv=a3.Value
,,rep
,,,a3.Navigate("next"); err break
,,,if(&an)
,,,,str s2=a3.Name; if(flags&1) s2.trim
,,,,sn.from(sn separator s2)
,,,if(&av)
,,,,sv.from(sv separator a3.Value)
,a2.Navigate("next"); err break
#5
Oh wow, I will give this a try and let you know.

Might take me awhile :lol:


Forum Jump:


Users browsing this thread: 1 Guest(s)