cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to capture text using SAP Gui Scripting?

Former Member
0 Likes
11,445

sap-capture-1.png

sap-capture-2.png

sap-capture-3.png

I am using Python to automate an SAP Logon process by modifying recorded script.
But I have run into a problem. I do not know how to extract the text data I find on SAP.


One process that I am doing is to extract information from Inbox line-by-line.
How can I capture the text using scripting? (Info record, vendor, material, etc)


I tried to record most steps but I have no idea how to capture the .text field.
The record does not pinpoint to that section.

 session.findById("wnd[0]").resizeWorkingPane(184, 30, 0) session.findById("wnd[0]/tbar[1]/btn[36]").press() session.findById("wnd[0]/usr/cntlSINWP_CONTAINER/shellcont/shell/shellcont[0]/shell").selectedNode = " 2" # Selects Inbox

session.findById("wnd[0]/usr/cntlSINWP_CONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[0]/shell").selectedRows = "0" # Selects First Row
        session.findById("wnd[0]/usr/cntlSINWP_CONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[0]/shell").selectionChanged()
        print(session.findById("wnd[0]/usr/cntlSINWP_CONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[0]/shell").text) 
# This thing prints "SAPGUI.GridViewCtrl.1"......
        session.findById("wnd[0]/usr/cntlSINWP_CONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[0]/shell").pressToolbarButton("DISP") 
      
        session.findById("wnd[0]/usr/tabsSO33_TAB1/tabpTAB1").select()
        print(session.findById("wnd[0]/usr/tabsSO33_TAB1/tabpTAB1").text)

# This thing prints "Doc. contents" ......I need to get (Info record, vendor, material, etc)


If you look at SAP_Capture_3.png photo, I have identified the shell. But I cannot get the text data of it...

print(session.findByID("wnd[0]/usr/cntlSINWP_CONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[1]/shell").text)

Above code only gives me "SAP.HTMLControl.1"...........I want the contents.

Help me please.

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member

Dear stefan.schnell,

Thank you for your reply.
I have taken an alternative path by downloading and reading the data.

I will try the html method you suggested in future.
Thank you sir.

Stefan-Schnell
Active Contributor

Hello Dong,

the HTML of this kind of documents looks like this:

<html>
  <head>
  </head>
  <frameset>
    <frame src="template.htm">
  </frameset>
</html>

So you can't get a text via body.innerHtml, because there is none.

Here the code to get the complete HTML source:

HTMLSource = HTMLDoc.GetElementsByTagName("HTML")
print(HTMLSource[0].outerHtml)

The location.href of the frame looks like this:

saphtmlp://htmlviewer.sap.com/051MlPZb7kgUvZbDLR683m/HTML000001.HTM

I don't know how to handle this kind of protocol to get the content of the frame.

As alternative I tried to get the text with screen scraping and OCR and that works.

Best regards
Stefan

Stefan-Schnell
Active Contributor

rainmankim

Hello Dong,
welcome in the SAP Community.

As far as I understand you correct you want to extract the context of an HTML control. Please try this:

    browser = session.findById("wnd[0]/usr/cntlHTML/shellcont/shell").BrowserHandle
    HTMLDoc = browser.Document
    print(HTMLDoc.body.innerText)

Use the property BrowserHandle to get an Internet Explorer object and here the Document property to get the loaded HTML document. Last but not least the innerText property to get the text of the HTML control.

Best regards
Stefan

Former Member
0 Likes
def saplogin():          # This function will Login to SAP and perform T-code


    try:
        ##### Step A: Opening SAP
        path = r"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe"
        subprocess.Popen(path) # This opens SAP
        time.sleep(4)  # deliberate time delay for application to open


        SapGuiAuto = win32com.client.GetObject('SAPGUI')
        if not type(SapGuiAuto) == win32com.client.CDispatch:
            return


        application = SapGuiAuto.GetScriptingEngine
        if not type(application) == win32com.client.CDispatch:
            SapGuiAuto = None
            return
        connection = application.OpenConnection("XXXXXXXXXXXXXXXXXXX", True)


        if not type(connection) == win32com.client.CDispatch:
            application = None
            SapGuiAuto = None
            return


        session = connection.Children(0)
        if not type(session) == win32com.client.CDispatch:
            connection = None
            application = None
            SapGuiAuto = None
            return
        
        ##### Step B: Logging into your account
        session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "XXXXXXXXXX"  # USER ID
        session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "XXXXXXXXXX" # PASSWORD
        session.findById("wnd[0]").sendVKey(0)
        session.findById("wnd[0]").sendVKey(0) # Additional enter key to get past compliance msg
        
        
        ##### Step C: Going into SAP Business Workplace
        session.findById("wnd[0]").sendVKey(36)  # Ctrl + F12
        
        ##### Step D: Selecting Inbox
        session.findById("wnd[0]/usr/cntlSINWP_CONTAINER/shellcont/shell/shellcont[0]/shell").selectedNode = "          2"
        
        
        ##### Step E: Selecting Relevant Rows        
        session.findById("wnd[0]/usr/cntlSINWP_CONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[0]/shell").selectedRows = "0"
        session.findById("wnd[0]/usr/cntlSINWP_CONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[0]/shell").selectionChanged()
        print("test 1")
        print(session.findById("wnd[0]/usr/cntlSINWP_CONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[0]/shell").text)
        print("test 2")
        print(session.findById("wnd[0]/usr/cntlSINWP_CONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[0]").text)
        print("test 3 I need this data")
        print(session.findByID("wnd[0]/usr/cntlSINWP_CONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[1]/shell").text)
        browser1 = session.findByID("wnd[0]/usr/cntlSINWP_CONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[1]/shell").BrowserHandle
        HTMLDoc1 = browser1.Document
        print("test 4 I really need this data")
        print(HTMLDoc1.body.innerText)
        
        ##### Step F: Trying to get data by clicking "Display" button
        session.findById("wnd[0]/usr/cntlSINWP_CONTAINER/shellcont/shell/shellcont[1]/shell/shellcont[0]/shell").pressToolbarButton("DISP")       
        session.findById("wnd[0]/usr/tabsSO33_TAB1/tabpTAB1").select()
        print("test 5")
        print(session.findById("wnd[0]/usr/tabsSO33_TAB1/tabpTAB1").text)
        print("test 6")
        print(session.findById("wnd[0]/usr/tabsSO33_TAB1").text)
        time.sleep(2)  # deliberate time delay
        session.findById("wnd[0]").sendVKey(15)

    except:
        print(sys.exc_info()[0])


    finally:
        session = None
        connection = None
        application = None
        SapGuiAuto = None
        
        
        
saplogin()

Below is the output I get.
Basically, under "Test 4" I am not getting anything

test 1

SAPGUI.GridViewCtrl.1

test 2

SAPGUI.CONTAINERCTRL.1

test 3 I need this data

SAP.HTMLControl.1

test 4

I really need this data

test 5

Doc. contents

test 6

Please help me sir.

Stefan-Schnell
Active Contributor
0 Likes

Hello rainmankim,

let us know how your alternative path by downloading and reading the data.

Thanks and best regards
Stefan

waddoum81
Explorer
0 Likes

Hi stefan.schnell , I know this thread is a bit old but I thought i try... I have an SAP transaction that generates a list (BoM) unfortunately there's no option to download/save the data except with ctrl+y mark text then copy/paste... my challenge now is i'm trying to automate some data retrieval using SAP scripting and the script doesn't capture this ctrl+y... any suggestions?

Note: i tried list> save (option is greyed out).. i also tried right click to save table to spreadsheet (option doesn't exist).

Thanks a lot!!

BR/Wissam