How to run Selenium and Tkinter at the same time without stopping to answer

Asked

Viewed 108 times

0

Good morning to you all! I have a python code that I use from Tkinter and Selenium to get all the books that Amazon recently made available for free, but Tkinter stops responding as Selenium opens, and I would like to introduce to the user the amount of books that have been picked up and pages that have been passed on, but that becomes unviable, since Tkinter doesn’t work, if anyone has ever had this problem and can help I would appreciate it. #Code below# ps: If you have something to comment on that can improve the code you can say, I am open and improve. Obg.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import random
from tkinter import *
import tkinter.messagebox





class InstagramBot():
    def __init__(self, username, password, url, code):
        self.username = username
        self.password = password
        self.url = url
        self.code = code
        firefoxProfile = webdriver.FirefoxProfile()
        firefoxProfile.set_preference("int1.accept_languages", "pt,pt-BR")
        firefoxProfile.set_preference("dom.webnotifications.enable", False)
        self.driver = webdriver.Firefox(
            firefox_profile=firefoxProfile, executable_path=r"C:\Users\User\Desktop\Gecko\geckodriver.exe"
        )

    def login(self,):
        try:

            driver = self.driver
            driver.get("https://www.amazon.com.br/ap/signin?openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com.br%2Fref%3Dnav_signin&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=brflex&openid.mode=checkid_setup&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&")
            loginusuario = driver.find_element_by_xpath("//input[@name='email']")
            loginusuario.clear()
            loginusuario.send_keys(self.username)
            buto = driver.find_element_by_id('continue').click()
            passwd = driver.find_element_by_xpath( "//input[@name='password']")
            passwd.clear()
            passwd.send_keys(self.password)
            time.sleep(2)
            buton = driver.find_element_by_id('signInSubmit').click()
            time.sleep(2)
        except:
            time.sleep(60)
        try:
            buton = driver.find_element_by_id('continue').click()
            time.sleep(30)
        except:
            pass

        self.takebooks()

    def takebooks(self):
        driver = self.driver
        driver.get(self.url)
        self.a = 0
        time.sleep(1)
        while True:
            for n in range(1,1000):

                try:
                    clicar = driver.find_element_by_id('a-autoid-'+str(n)+'-announce').send_keys(Keys.SHIFT + Keys.CONTROL + Keys.ENTER)
                    self.a += 1
                    print("Livro Pego({})".format(self.a))
                    time.sleep(0.5)
                    driver.switch_to_window(driver.window_handles[-1])
                    time.sleep(1)
                    driver.close()
                    driver.switch_to_window(driver.window_handles[0])
                    time.sleep(1)
                except Exception as error:
                    #print("----")
                    pass
            try:

                nextpage = driver.find_element_by_class_name('a-last').click()
            except Exception as error:
                print(error)

            time.sleep(3)


def main():
    root = Tk()
    app = Window1(root)
    root.mainloop()
class Window1():
    def __init__(self, master):
        self.master = master
        self.master.title("BotInstagtram")
        self.master.geometry("700x500")
        self.frame = Frame(self.master)
        self.frame.pack()
        self.user = StringVar()
        self.passwd = StringVar()
        self.url = StringVar()
        self.LabelTitle = Label(self.frame, text="BOT AMAZON FREE BOOKS", font=("verdana", 30), bd=20, justify=LEFT)
        self.LabelTitle.grid(row=0, columnspan=2, pady=20, sticky=W)
        #-----------------------------------------------------------
        #Frames
        self.Loginframe1 = Frame(self.frame, width=500, height=150, bd=20, relief='ridge')
        self.Loginframe1.grid(row=1, column=0, sticky=W)
        self.Loginframe2 = Frame(self.frame, width=500, height=50, bd=10, relief='ridge')
        self.Loginframe2.grid(row=2, column=0, sticky=W)

        #-----------------------------------------------------------
        #StringVar
        self.url.set('https://www.amazon.com.br/s?i=digital-text&bbn=5475882011&rh=n%3A5308307011%2Cn%3A5308308011%2Cn%3A5475882011%2Cp_36%3A5560478011%2Cp_n_feature_browse-bin%3A6406077011&dc&language=pt_BR&_encoding=UTF8&fst=as%3Aoff&linkCode=sl2&linkId=c74a3b3b1606b3a2c5d5249ccab38e13&primeCampaignId=prime_assoc_ft&qid=1591627747&rnid=6406076011&tag=ynv-20&ref=sr_nr_p_n_feature_browse-bin_2')
        self.code = StringVar()
        #-----------------------------------------------------------
        #Frame1
        self.LabelUser = Label(self.Loginframe1, text="Nome", font=('arial', 15, "bold"))
        self.LabelUser.grid()
        self.LabeltxtUser = Entry(self.Loginframe1, text="Nome", font=('arial', 15, "bold"), textvariable=self.user)
        self.LabeltxtUser.grid(row=0, column=1)
        self.LabelPasswd = Label(self.Loginframe1, text="Senha", font=('arial', 15, "bold"))
        self.LabelPasswd.grid(row=1, column=0)
        self.LabeltxtPasswd = Entry(self.Loginframe1, text="Senha", font=('arial', 15, "bold"), show="*", textvariable=self.passwd)
        self.LabeltxtPasswd.grid(row=1, column=1)
        self.Botar = Label(self.Loginframe1, text="URL", font=('arial', 12, 'bold'))
        self.Botar.grid(row=2, column=0)
        self.LabeltxtURL = Entry(self.Loginframe1, text="URL", font=('arial', 15, "bold"), textvariable=self.url)
        self.LabeltxtURL.grid(row=2, column=1)
        #self.Botarcode = Label(self.Loginframe1, text="Code", font=('arial', 12, 'bold'))
        #self.Botarcode.grid(row=3, column=0)
        #self.Labeltxtcode = Entry(self.Loginframe1, text="Codigo", font=('arial', 15, "bold"), textvariable=self.code)
        #self.Labeltxtcode.grid(row=3, column=1)

        #-----------------------------------------------------------
        #Frame2

        self.TestarConta = Button(self.Loginframe2, text="Executar", width=10, font=('arial', 12, 'bold'), command=self.Login)
        self.TestarConta.grid()
        #-----------------------------------------------------------
        #Frame3
        self.duvida = Button(self.Loginframe2, text="?", width=6, font=('arial', 12, 'bold'))
        self.duvida.grid(row=0, column=1)


    def Login(self):
        name = (self.user.get())
        passwd = (self.passwd.get())
        url = (self.url.get())
        code = self.code
        self.lucasbot = InstagramBot(name, passwd, url, code)
        self.lucasbot.login()



if __name__ == '__main__':
    main()

  • Dude, when I have a screen, and I have to click a button, and what the button does takes time to execute. If it’s in the same Thread of the screen, the screen hangs. The ideal is to use different Threads, one to run the screen and the other to run Selenium. Or you can create a second application, which receives the parameters of the screen and calls it by Tkinter passing the parameters and releasing the call command. That’s basically the idea.

  • 1

    Ahh ok, I’ll try to see this thread stop, and use thank you very much!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.