Issue
I would like to log into Facebook after entering username and password in a tkinter window. The problem is that, after entering the username and password, when I click on the Login button, the facebook page opens but without logging in. Maybe it's some problem with driver.find_element. How to solve? NOTE: Please don't change the way I connect with Firefox.
import tkinter as tk
from tkinter import ttk
from tkinter import *
from time import sleep
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
import os
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
root = tk.Tk()
root.title("Login")
root.geometry('630x500')
topbar = tk.Frame(root, bg='#3c5999', height=42)
topbar.pack(fill='x')
#TEXTBOX
label_email = Label(root, text="email", bg='#3c5999', foreground="white")
label_email.place(x=2, y = 10)
email = tk.Entry(root)
email.place(x=50, y = 9)
label_password = Label(root, text="password", bg='#3c5999', foreground="white")
label_password.place(x=260, y = 10)
password = tk.Entry(root)
password.place(x=335, y = 9)
def login():
#Access Facebook
profile_path = '/usr/bin/firefox/firefox'
#/usr/bin/firefox/firefox
options=Options()
options.set_preference('profile', profile_path)
options.set_preference('network.proxy.type', 4)
options.set_preference('network.proxy.socks', '127.0.0.1')
options.set_preference('network.proxy.socks_port', 9050)
options.set_preference("network.proxy.socks_remote_dns", False)
service = Service('/home/jass/bin/geckodriver')
driver = Firefox(service=service, options=options)
driver.get("http://www.facebook.com")
WebDriverWait(driver, 1).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "accept_only_essential_button"))).click()
username_box = driver.find_element('email')
username_box.send_keys(email)
password_box = driver.find_element('pass')
password_box.send_keys(password)
login_box = driver.find_element('loginbutton')
login_box.click()
button = Button(root, text="Login", bg='white', foreground='black', width=7, command=login)
button.place(x=530, y=5)
root.mainloop()
Solution
If you would run code in console then you should see errors which should help you to see problem.
You have two mistakes.
you use
find_element()
in wrong way - it has to befind_element(By.ID, 'email')
eventually olderfind_element_by_id('email')
(the same forpass
andbutton
you have to use
.get()
to get text fromEntry
- so you needemail.get()
andpassword.get()
username_box = driver.find_element(By.ID, 'email')
username_box.send_keys(email.get())
password_box = driver.find_element(By.ID, 'pass')
password_box.send_keys(password.get())
login_box = driver.find_element(By.ID, 'loginbutton')
login_box.click()
There is also other problem (at least on my computer) - at start it shows message about cookies and I have to close it because it hides loginbutton
and Selenium can't click it.
EDIT:
To close cookie message you have to use correct css selector
(By.CSS_SELECTOR, 'button[data-cookiebanner="accept_only_essential_button"]')
and you have to wait longer than 1 second.
Full working code:
import os
import tkinter as tk # PEP8: `import *` is not preferred
from tkinter import ttk
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# --- functions --- # PEP8: all functions after imports
def login():
profile_path = '/usr/bin/firefox/firefox'
options = Options()
options.set_preference('profile', profile_path)
options.set_preference('network.proxy.type', 4)
options.set_preference('network.proxy.socks', '127.0.0.1')
options.set_preference('network.proxy.socks_port', 9050)
options.set_preference("network.proxy.socks_remote_dns", False)
service = Service('/home/jass/bin/geckodriver')
driver = Firefox(service=service, options=options)
driver.get("https://www.facebook.com") # https instead of http
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[data-cookiebanner="accept_only_essential_button"]'))).click()
username_box = driver.find_element(By.ID, 'email')
username_box.send_keys(email.get())
password_box = driver.find_element(By.ID, 'pass')
password_box.send_keys(password.get())
login_box = driver.find_element(By.ID, 'loginbutton')
login_box.click()
# --- main ---
root = tk.Tk()
root.title("Login")
root.geometry('630x500')
topbar = tk.Frame(root, bg='#3c5999', height=42)
topbar.pack(fill='x')
label_email = tk.Label(topbar, text="email", bg='#3c5999', foreground="white")
label_email.place(x=2, y=10)
email = tk.Entry(topbar)
email.place(x=50, y=9)
label_password = tk.Label(topbar, text="password", bg='#3c5999', foreground="white")
label_password.place(x=260, y=10)
password = tk.Entry(topbar)
password.place(x=335, y=9)
button = tk.Button(topbar, text="Login", bg='white', foreground='black', width=7, command=login)
button.place(x=530, y=5)
root.mainloop()
PEP 8 -- Style Guide for Python Code
Answered By - furas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.