我登录facebook的tkinter用户界面无法正常工作

2024-09-18 18:40:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python有点陌生,我想为一个从我正在学习的书中得到的程序制作一个UI。该程序在chrome上打开Facebook并让你登录,但不能正常使用UI。在

from tkinter import *
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

class Facebook(Frame):
    def __init__(self,window):

        Frame.__init__(self,window,padx=12,pady=12)

        window.title("FBGiriş")
        window.geometry("350x200+50+50")
        self.lab1 = Label(window, text="email adress: ", bg="grey", 
borderwidth=2, relief=GROOVE)
        self.lab1.place(x=20, y=40)
        self.lab2 = Label(window, text="password: ", bg="grey", 
borderwidth=2, relief=GROOVE)
        self.lab2.place(x=40, y=80)


       self.ent1 = Entry(window, bg="cyan", relief=GROOVE, borderwidth=2)
       self.ent2 = Entry(window, bg="cyan", relief=GROOVE, borderwidth=2)

      self.ent1.place(x=140, y=40)
      self.ent2.place(x=140, y=80)

      self.gButton = Button(window, text="Login", relief=GROOVE, 
command=self.face, borderwidth=5, bg="red")
      self.gButton.place(x=260, y=120)

   def face(self):

       self.driver = webdriver.Chrome("C:\webdrivers\chromedriver.exe")
       self.driver.get("https://www.facebook.com/")
       self.fbLogin()

   def fbLogin(self):

       #self.face()
       driver = self.driver

       self.ent1 = str(self.ent1)
       self.ent2 = str(self.ent2)
       username = self.ent1

       password = self.ent2

       emailid = 'email'
       passid = 'pass'
       loginid = "loginbutton"
       logoclass = "_48pw"
       emailbox = WebDriverWait(driver,10).until(lambda driver: 
driver.find_element_by_id(emailid))
       passbox = WebDriverWait(driver,10).until(lambda driver: 
driver.find_element_by_id(passid))
       loginButton = WebDriverWait(driver,10).until(lambda driver: 
driver.find_element_by_id(loginid))
       emailbox.click()
       emailbox.clear()
       emailbox.send_keys(username)

       passbox.click()
       passbox.clear()
       passbox.send_keys(password)


       loginButton.click()

       WebDriverWait(driver,10).until(lambda driver: 
driver.find_element_by_class_name(logoclass))

if __name__ == '__main__':

    root = Tk()
    app = Facebook(root)
    app.mainloop()

当我在文本框中输入电子邮件和密码,然后单击“登录”时,用户界面就会在chrome上打开facebook。但不管我在输入文本框中输入什么,它都会在facebook上输入随机数字:

屏幕截图:

输入

enter image description here

预期产量

enter image description here

电流(不正确)输出

Screenshot


Tags: lambdaselfdriverplacefindwindowuntilbg
1条回答
网友
1楼 · 发布于 2024-09-18 18:40:27

随机数是对tkinterEntrysself.ent1和{}实例的引用。您需要的是这些条目的内容,您需要对这些条目调用get方法来获取它们的内容。在

更改此项:

username = self.ent1
password = self.ent2

收件人:

^{pr2}$

另外,在Tkinter小部件的实例上调用str不会给您任何有用的输出,因此以下代码没有帮助:

self.ent1 = str(self.ent1)
self.ent2 = str(self.ent2)

你似乎也有一些缩进错误这里是完整的代码,我可以用它登录Facebook:

from tkinter import *
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

class Facebook(Frame):
    def __init__(self,window):
        Frame.__init__(self,window,padx=12,pady=12)

        window.title("FBGiris")
        window.geometry("350x200+50+50")
        self.lab1 = Label(window, text="email adress: ", bg="grey", borderwidth=2, relief=GROOVE)
        self.lab1.place(x=20, y=40)
        self.lab2 = Label(window, text="password: ", bg="grey", borderwidth=2, relief=GROOVE)
        self.lab2.place(x=40, y=80)


        self.ent1 = Entry(window, bg="cyan", relief=GROOVE, borderwidth=2)
        self.ent2 = Entry(window, bg="cyan", relief=GROOVE, borderwidth=2)

        self.ent1.place(x=140, y=40)
        self.ent2.place(x=140, y=80)

        self.gButton = Button(window, text="Login", relief=GROOVE, command=self.face, borderwidth=5, bg="red")
        self.gButton.place(x=260, y=120)


    def face(self):
        self.driver = webdriver.Chrome("C:\webdrivers\chromedriver.exe")
        self.driver.get("https://www.facebook.com/")
        self.fbLogin()

    def fbLogin(self):
        #self.face()
        driver = self.driver

        username = self.ent1.get()
        password = self.ent2.get()

        emailid = 'email'
        passid = 'pass'
        loginid = "loginbutton"
        logoclass = "_48pw"
        emailbox = WebDriverWait(driver,10).until(lambda driver: driver.find_element_by_id(emailid))
        passbox = WebDriverWait(driver,10).until(lambda driver: driver.find_element_by_id(passid))
        loginButton = WebDriverWait(driver,10).until(lambda driver: driver.find_element_by_id(loginid))

        emailbox.click()
        emailbox.clear()
        emailbox.send_keys(username)

        passbox.click()
        passbox.clear()
        passbox.send_keys(password)

        loginButton.click()

        WebDriverWait(driver,10).until(lambda driver: 
        driver.find_element_by_class_name(logoclass))   

if __name__ == '__main__':

    root = Tk()
    app = Facebook(root)
    app.mainloop()

相关问题 更多 >