Forum Discussion

mschofer's avatar
mschofer
Occasional Contributor
3 years ago

Python Pointer

Long time no posting. In Python what is the best way to extract information from tables.  I am able to find the values quite easily and write to variables. 
Is beautiful soup the recommended route.? 

  • I got it working

     

    def chechdetails():
    browser = Aliases.browser
    page = browser.Page("*")
    CurrPage = browser.Page("*")
    PropArray = ["ObjectType","ContentText"]
    ValuesArray = ["Cell",aqConvert.DateTimeToFormatStr("12/1/2021", "%m/%d/%Y")];

    myDateTable = page.FindChild(PropArray, ValuesArray,10)
    Log.Message(myDateTable.RowIndex)
    # for r in range(1, 999):
    # checkNum = currPage.FindElements("//form[@id='']/div/table/tbody/tr[" + str(r) + "]/td[1]")
    # if len(rowOfCells) == 0:
    # break
    # if checkNum.innerText == "CK" + marksCheckNumFromExcel:
    # targetRow = r
    # break
    # if targetRow == 0:
    # Log.Error("couldn't find check #")
    # else:
    myText21 = CurrPage.FindElement("//table[@border=1]/tbody/tr["+str(myDateTable.RowIndex + 1)+"]/td[3]/div/table/tbody/tr[2]/td[2]")
    if (myText21.Exists):
    Log.Message(myText21.contentText)
    myText22 = CurrPage.FindElement("//table[@border=1]/tbody/tr["+str(myDateTable.RowIndex + 1)+"]/td[3]/div/table/tbody/tr[3]/td[2]")
    if (myText22.Exists):
    Log.Message(myText22.contentText)
    myText23 = CurrPage.FindElement("//div[@id='amtDtl.0_2']/table/tbody/tr[4]/td[2]")
    if (myText23.Exists):
    Log.Message(myText23.contentText)
    myText24 = CurrPage.FindElement("//div[@id='amtDtl.0_2']/table/tbody/tr[5]/td[2]")
    if (myText24.Exists):
    Log.Message(myText24.contentText)
    myText25 = CurrPage.FindElement("//div[@id='amtDtl.0_2']/table/tbody/tr[6]/td[2]")
    if (myText25.Exists):
    Log.Message(myText25.contentText)
    myText26 = CurrPage.FindElement("//div[@id='amtDtl.0_2']/table/tbody/tr[7]/td[2]")
    if (myText26.Exists):
    Log.Message(myText26.contentText)
    # myText27 = CurrPage.FindElement("//div[@id='amtDtl.0_2']/table/tbody/tr[8]/td[2]")
    # if (myText27.Exists):
    # Log.Message(myText27.contentText)
    myText28 = CurrPage.FindElement("//div[@id='amtDtl.0_2']/table/tbody/tr[9]/td[2]")
    if (myText28.Exists):
    Log.Message(myText28.contentText)
    # myText29 = CurrPage.FindElement("//div[@id='amtDtl.0_2']/table/tbody/tr[10]/td[2]")
    # if (myText29.Exists):
    # Log.Message(myText29.contentText)

  • Hi mschofer 

     

    Are you trying to extract the table data directly from the HTML using Python and BeautifulSoup, or by using TestComplete functionality?

     

    To access and modify table values it a Table in the Stores repository to your project, use the Tables.values[] property: Tables.Values method 

     

    To create a table in Stores based on a table of values in your UI, add the Stores repository to your Project, then add a table object. More info on Tables objects here: About Tables Collections 

    • mschofer's avatar
      mschofer
      Occasional Contributor

      I am using Test Complete and Python and No Soup.  I am just getting back to this.

      So I answered my original question. I can write to a variable or use RETURN making it a function.

      My challenge is getting the correct row. (The table has many rows, It is actually a table within a table. I will be going that in a FOR loop

      with logic to identify the row, 

       

      def chechdetails():
      browser = Aliases.browser
      page = browser.Page("*")
      CurrPage = browser.Page("*")
      myText21 = CurrPage.FindElement("//div[@id='amtDtl.0_2']/table/tbody/tr[2]/td[2]")
      if (myText21.Exists):
      Log.Message(myText21.contentText)
      myText22 = CurrPage.FindElement("//div[@id='amtDtl.0_2']/table/tbody/tr[3]/td[2]")
      if (myText22.Exists):

      • mschofer's avatar
        mschofer
        Occasional Contributor

        I got it working

         

        def chechdetails():
        browser = Aliases.browser
        page = browser.Page("*")
        CurrPage = browser.Page("*")
        PropArray = ["ObjectType","ContentText"]
        ValuesArray = ["Cell",aqConvert.DateTimeToFormatStr("12/1/2021", "%m/%d/%Y")];

        myDateTable = page.FindChild(PropArray, ValuesArray,10)
        Log.Message(myDateTable.RowIndex)
        # for r in range(1, 999):
        # checkNum = currPage.FindElements("//form[@id='']/div/table/tbody/tr[" + str(r) + "]/td[1]")
        # if len(rowOfCells) == 0:
        # break
        # if checkNum.innerText == "CK" + marksCheckNumFromExcel:
        # targetRow = r
        # break
        # if targetRow == 0:
        # Log.Error("couldn't find check #")
        # else:
        myText21 = CurrPage.FindElement("//table[@border=1]/tbody/tr["+str(myDateTable.RowIndex + 1)+"]/td[3]/div/table/tbody/tr[2]/td[2]")
        if (myText21.Exists):
        Log.Message(myText21.contentText)
        myText22 = CurrPage.FindElement("//table[@border=1]/tbody/tr["+str(myDateTable.RowIndex + 1)+"]/td[3]/div/table/tbody/tr[3]/td[2]")
        if (myText22.Exists):
        Log.Message(myText22.contentText)
        myText23 = CurrPage.FindElement("//div[@id='amtDtl.0_2']/table/tbody/tr[4]/td[2]")
        if (myText23.Exists):
        Log.Message(myText23.contentText)
        myText24 = CurrPage.FindElement("//div[@id='amtDtl.0_2']/table/tbody/tr[5]/td[2]")
        if (myText24.Exists):
        Log.Message(myText24.contentText)
        myText25 = CurrPage.FindElement("//div[@id='amtDtl.0_2']/table/tbody/tr[6]/td[2]")
        if (myText25.Exists):
        Log.Message(myText25.contentText)
        myText26 = CurrPage.FindElement("//div[@id='amtDtl.0_2']/table/tbody/tr[7]/td[2]")
        if (myText26.Exists):
        Log.Message(myText26.contentText)
        # myText27 = CurrPage.FindElement("//div[@id='amtDtl.0_2']/table/tbody/tr[8]/td[2]")
        # if (myText27.Exists):
        # Log.Message(myText27.contentText)
        myText28 = CurrPage.FindElement("//div[@id='amtDtl.0_2']/table/tbody/tr[9]/td[2]")
        if (myText28.Exists):
        Log.Message(myText28.contentText)
        # myText29 = CurrPage.FindElement("//div[@id='amtDtl.0_2']/table/tbody/tr[10]/td[2]")
        # if (myText29.Exists):
        # Log.Message(myText29.contentText)