我想在 tkinter 視窗中輸入用戶名和密碼后登錄 Facebook。問題是,在輸入用戶名和密碼后,當我點擊登錄按鈕時,facebook 頁面打開但沒有登錄。可能是 driver.find_element 有問題。怎么解決?注意:請不要更改我與 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()
uj5u.com熱心網友回復:
如果您要在控制臺中運行代碼,那么您應該會看到有助于您發現問題的錯誤。
你有兩個錯誤。
您
find_element()以錯誤的方式使用 - 它find_element(By.ID, 'email')最終必須更舊find_element_by_id('email')(對于pass和button你必須使用
.get()來獲取文本Entry- 所以你需要email.get()和password.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()
還有其他問題(至少在我的計算機上) - 在開始時它顯示有關 cookie 的訊息,我必須關閉它,因為它隱藏loginbutton并且 Selenium 無法單擊它。
編輯:
要關閉 cookie 訊息,您必須使用正確的 css 選擇器
(By.CSS_SELECTOR, 'button[data-cookiebanner="accept_only_essential_button"]')
你必須等待超過 1 秒。
完整的作業代碼:
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——Python 代碼風格指南
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/460091.html
標籤:Python python-3.x 硒 硒网络驱动程序 tkinter
上一篇:如何使用Selenium Chromedrive更改保存的PDF頁面名稱
下一篇:AttributeError:“WebElement”物件沒有屬性“select_by_value”使用Selenium選擇下拉選單
