โดย Jom07 » 15/02/2018 6:47 pm
การทำ webdriver บน Python GUI (Tkinter)
โค้ด: เลือกทั้งหมด
from tkinter import *
from tkinter import messagebox as ms
from selenium import webdriver
import sqlite3
เริ่มแรกเป็นการ import tkinter,selenium webdriver เพื่อสร้างหน้าต่างกับ เรียก wabdriver ขึ้นมา
โค้ด: เลือกทั้งหมด
with sqlite3.connect('quit.db') as db:
c = db.cursor()
find_user = ('SELECT * FROM user WHERE username = ? and password = ?')
c.execute(find_user,[(self.username.get()),(self.password.get())])
result = c.fetchall()
if result:
self.logf.pack_forget()
self.head['text'] = self.username.get() + '\n Loged In'
self.head['pady'] = 150
else:
chromedriver = 'D:\\chromedriver_win32\\chromedriver.exe'
brower = webdriver.Chrome()
brower.get('------------------------------------------------------------'')
เป็นการเรียก webdriver มาใช้งาน โดยโหลด webdriver แล้วกำหนดที่อยู่ ในที่นี้ จะเอาใว้ไดร์ D
โค้ด: เลือกทั้งหมด
def widgets(self):
self.head = Label(self.master,text = 'LOGIN',font = ('',25),pady = 10)
self.head.pack()
self.logf = Frame(self.master,padx =10,pady = 10)
Label(self.logf,text = 'Host Name : ',font = ('',20),pady=5,padx=5).grid(sticky = W)
Entry(self.logf,textvariable = self.hostname,bd = 5,font = ('',15)).grid(row=0,column=1)
Label(self.logf,text = '-------------',font = ('',20),pady=5,padx=5).grid(sticky = W)
Entry(self.logf,textvariable = self.password,bd = 5,font = ('',15)).grid(row=1,column=1)
Label(self.logf,text = 'Username : ',font = ('',20),pady=5,padx=5).grid(sticky = W)
Entry(self.logf,textvariable = self.username,bd = 5,font = ('',15)).grid(row=2,column=1)
Label(self.logf,text = 'Password : ',font = ('',20),pady=5,padx=5).grid(sticky = W)
Entry(self.logf,textvariable = self.password,bd = 5,font = ('',15),show = '*').grid(row=3,column=1)
Label(self.logf,text = '-------------',font = ('',20),pady=5,padx=5).grid(sticky = W)
Entry(self.logf,textvariable = self.password,bd = 5,font = ('',15)).grid(row=4,column=1)
Button(self.logf,text = ' RUN ',bd = 3 ,font = ('',14),padx=5,pady=5,command=self.login).grid(row=7,column=1)
Button(self.logf,text = ' --- ',bd = 3 ,font = ('',14),padx=5,pady=5,command=self.login).grid(row=7,column=2)
self.logf.pack()
การสร้าง ช่องใส่ข้อมูล และปุ่มคลิก Button ขึ้นมาให้งาน
ผลรัน
จะขึ้นหน้าต่างนี้มาเมื่อกดปุ่มรันจะขึ้น webdriver ที่เรากำหนดใว้
การทำ webdriver บน Python GUI (Tkinter)
[code]from tkinter import *
from tkinter import messagebox as ms
from selenium import webdriver
import sqlite3
[/code]
เริ่มแรกเป็นการ import tkinter,selenium webdriver เพื่อสร้างหน้าต่างกับ เรียก wabdriver ขึ้นมา
[code]
with sqlite3.connect('quit.db') as db:
c = db.cursor()
find_user = ('SELECT * FROM user WHERE username = ? and password = ?')
c.execute(find_user,[(self.username.get()),(self.password.get())])
result = c.fetchall()
if result:
self.logf.pack_forget()
self.head['text'] = self.username.get() + '\n Loged In'
self.head['pady'] = 150
else:
chromedriver = 'D:\\chromedriver_win32\\chromedriver.exe'
brower = webdriver.Chrome()
brower.get('------------------------------------------------------------'')
[/code]
เป็นการเรียก webdriver มาใช้งาน โดยโหลด webdriver แล้วกำหนดที่อยู่ ในที่นี้ จะเอาใว้ไดร์ D
[code]def widgets(self):
self.head = Label(self.master,text = 'LOGIN',font = ('',25),pady = 10)
self.head.pack()
self.logf = Frame(self.master,padx =10,pady = 10)
Label(self.logf,text = 'Host Name : ',font = ('',20),pady=5,padx=5).grid(sticky = W)
Entry(self.logf,textvariable = self.hostname,bd = 5,font = ('',15)).grid(row=0,column=1)
Label(self.logf,text = '-------------',font = ('',20),pady=5,padx=5).grid(sticky = W)
Entry(self.logf,textvariable = self.password,bd = 5,font = ('',15)).grid(row=1,column=1)
Label(self.logf,text = 'Username : ',font = ('',20),pady=5,padx=5).grid(sticky = W)
Entry(self.logf,textvariable = self.username,bd = 5,font = ('',15)).grid(row=2,column=1)
Label(self.logf,text = 'Password : ',font = ('',20),pady=5,padx=5).grid(sticky = W)
Entry(self.logf,textvariable = self.password,bd = 5,font = ('',15),show = '*').grid(row=3,column=1)
Label(self.logf,text = '-------------',font = ('',20),pady=5,padx=5).grid(sticky = W)
Entry(self.logf,textvariable = self.password,bd = 5,font = ('',15)).grid(row=4,column=1)
Button(self.logf,text = ' RUN ',bd = 3 ,font = ('',14),padx=5,pady=5,command=self.login).grid(row=7,column=1)
Button(self.logf,text = ' --- ',bd = 3 ,font = ('',14),padx=5,pady=5,command=self.login).grid(row=7,column=2)
self.logf.pack()[/code]
การสร้าง ช่องใส่ข้อมูล และปุ่มคลิก Button ขึ้นมาให้งาน
ผลรัน
[img]http://snap.mindphp.com/index.php?view=2018Feb15_5a857e2b7f935[/img]
จะขึ้นหน้าต่างนี้มาเมื่อกดปุ่มรันจะขึ้น webdriver ที่เรากำหนดใว้