0

My head is spinning after a few days on this one. I am running TKinter with Selenium to simplify navigation to get a few items from a complicated internal website. I have a TKinter UI file "smallui.py" and my functions in "gesmall.py". If I run "gesmall.py 12345" from the command prompt, it runs like a champ - no problem. If I import gesmall and call the functions from smallui.pi it crashes. After I have created the variable for the webpage - called webpage as a "type:<class 'selenium.webdriver.chrome.webdriver.WebDriver'>" I then pass that variable to the next function so I can parse some data from a few fields. I get the error "searchbar = webpage.find_element_by_id("sidebarCollapse") AttributeError: 'tuple' object has no attribute 'find_element_by_id'" I can see no reason when my variable webpage got changed from a class to a tuple? Here is the code for the UI and the functions.

import sys
import pandas as pd
from selenium import webdriver
from selenium.webdriver.support.select import Select
import time

def get_user_pswd():
    #geuser.xlsx
    df = pd.read_excel("geuser.xlsx", usecols=['Field_Name','Field_Data'], index_col = "Field_Name")
    user_name = df.loc['User_Name', 'Field_Data']
    pswd = df.loc['User_Pswd', 'Field_Data']
    webtoolurl = df.loc['webtoolurl','Field_Data']  
    return user_name, pswd, webtoolurl

def webtoollogin(user_name, pswd, webtoolurl):
    PATH = "chromedriver.exe"
    options = webdriver.ChromeOptions()
    options.add_experimental_option('excludeSwitches', ['enable-logging'])
    webpage = webdriver.Chrome(executable_path = PATH, options=options)
    webpage.get(webtoolurl)
    time.sleep(1)

    web_user_name = webpage.find_element_by_xpath('//*[@id="id_username"]')
    web_user_name.send_keys(user_name)

    web_pswd =  webpage.find_element_by_xpath('//*[@id="id_password"]')
    web_pswd.send_keys(pswd.strip())

    #Find LoginIn button and click it
    log_in = webpage.find_element_by_xpath('//*[@id="loginForm"]/form/div[3]/div/input[2]')
    log_in.click()
    loggedin = True
    time.sleep(1)
    print(f"\n\nline35 type:{type(webpage)} webpage:{webpage}\n\n")

This print returns ---- a class line35 type:<class 'selenium.webdriver.chrome.webdriver.WebDriver'> webpage:<selenium.webdriver.chrome.webdriver.WebDriver (session="872043d70783f14c62137a68449247a0")>

    return webpage, loggedin

def webnavigate (webpage, siteID):
    print(f"\n\nline39 type:{type(webpage)} webpage:{webpage}\n\n")

This print returns --- a tuple even though in line35 from the previous function it is a class. line39 type:<class 'tuple'> webpage:(<selenium.webdriver.chrome.webdriver.WebDriver (session="872043d70783f14c62137a68449247a0")>, True)

    #Find Magnifying Glass icon and click on it
    searchbar = webpage.find_element_by_id("sidebarCollapse")
    searchbar.click()
    time.sleep(1)

    #find Site Idneticication and click on it to bring up entry boxes
    SiteIDsidebar = webpage.find_element_by_xpath('//*[@id="sidebar"]/ul/li[1]/a')
    SiteIDsidebar.click()
    time.sleep(1)

    # Find Unique Identified Box and enter SiteID variable
    uniqueID = webpage.find_element_by_id('yadcf-filter--addressDatatable-3')
    time.sleep(.5)
    uniqueID.send_keys(siteID)
    time.sleep(2)

    webpage.find_element_by_link_text("Details").click()
    
    rtype, origaddress = getwebtooldata(webpage)

    return (rtype, origaddress)

def getwebtooldata(webpage):
    webpage.find_element_by_xpath('//*[@id="summary_tab"]').click()
    time.sleep(.5)
    selection_list = Select(webpage.find_element_by_id("id_site_type"))
    rtype = selection_list.first_selected_option
    rtype = rtype.text
    address = webpage.find_element_by_name("original_address_line")
    origaddress = address.get_attribute('value')
    return rtype, origaddress


if __name__ == "__main__":
    siteID = sys.argv[1]
    user_name,pswd, webtoolurl = get_user_pswd()
    webpage, loggedin = webtoollogin(user_name, pswd, webtoolurl)
    rtype,origaddress, = webnavigate(webpage, siteID)
    print(rtype, origaddress)

Here is the TKinter code

import gesmall

from tkinter import *
global webpage
loggedin = False

#SiteID Submit function
def submitwebpage(loggedin):
    siteID=siteIDentry.get()  #collect text from the text entry box
    instructionbox.delete(0.0, END)
    print("13 siteID",siteID)
    
    if loggedin:
        rtype, origaddress= gesmall.webnavigate(webpage, siteID)
        instructions = f"""rtype: {rtype} \n  
        Site Address: {origaddress}"""
    else: 
        user_name,pswd, webtoolurl = gesmall.get_user_pswd()
        webpage = gesmall.webtoollogin (user_name, pswd, webtoolurl)
        print(f"\n\nline20 type:{type(webpage)} webpage:{webpage}\n\n")

this print returns --- a class line20 type:<class 'tuple'> webpage:(<selenium.webdriver.chrome.webdriver.WebDriver (session="872043d70783f14c62137a68449247a0")>, True) it now calls the function gesmall.webnavigate, and from the line39 print statement it shows it is now a tuple ???? what happened?

        rtype, origaddress= gesmall.webnavigate(webpage, siteID)
        instructions = f"""rType: {rtype} \n 
        Site Address: {origaddress}"""
            
    instructionbox.insert(END, instructions)
    print(f"\n{instructions}\n")
    return rtype, origaddress, webpage, instructions

def close_window():
    window.destroy()
    exit()

#  *** Main ***
window = Tk()
window.title ("Helper")
window.geometry("500x350")
window.configure(background = "white")

# Create text entry box
Label (window, text = "Enter ID", bg='white',fg='blue', font = 'none 14 bold').grid(row =2, column=0 )
siteIDentry =Entry(window, width = 8, bg="light grey", font = 'none 14 bold')
siteIDentry.grid(row=2, column=1, sticky=W)

#instruction box
Label (window, text = "Instructions", bg='white', fg = 'blue', font='none 14 bold').grid(row=4, column=0, sticky=W)
#output box for the response
instructionbox = Text(window, width=55, height=4, fg = "blue", font='none 12 bold', wrap=WORD, background = 'light grey')
instructionbox.grid(row=5, column=0, columnspan=4, sticky=W)
# Submit button
Button(window, text="Submit", width=10, command= lambda: submitwebpage(loggedin)).grid(row=2, column=2, sticky=E)
# )
# Add Site ID Summary box
Label (window, text = f"\nSite Information", bg='white', fg = 'blue', font='none 14 bold').grid(row=10, column=0, sticky=W)
infobox = Text(window, width=55, height=4, fg = "blue", font='none 12 bold', wrap=WORD, background = 'light grey')
infobox.grid(row=11, column=0, columnspan=4, sticky=W)

Button(window, text="Click to Exit", width=12, command=close_window) .grid(row=15, column=0, sticky=W)

window.mainloop()
Greg Davey
  • 81
  • 1
  • 5
  • Try changing `webpage = gesmall.webtoollogin (user_name, pswd, webtoolurl)` to `webpage, _ = gesmall.webtoollogin (user_name, pswd, webtoolurl)` inside `submitwebpage()`. – acw1668 Aug 13 '21 at 07:20
  • WOW... that worked. Thank-you so much. But, Why? After so many hours chasing this a ", _" seems like a cruel answer. – Greg Davey Aug 13 '21 at 15:49
  • You can refer to item #3 of this [answer](https://stackoverflow.com/a/5893946/5317403). – acw1668 Aug 13 '21 at 15:53

0 Answers0