from selenium import webdriver
import time
import tkinter as tk
#点击按钮,出现网页进行赋值
def func():
browser = webdriver.Chrome()
#获取访问的网址
browser.get('https://www.baidu.com')
#发送文字到目标id
kw=browser.find_element_by_id("kw")
su=browser.find_element_by_id("su")
time.sleep(3)
kw.send_keys("hello")
time.sleep(3)
#点击目标id
su.click()
#设置窗口
window = tk.Tk()
#设置标题
window.title("自动回答问题")
window.geometry("300x200")
#设置按钮
button=tk.Button(text="开始执行",width=10,height=3,command=func)
#放置按钮
button.pack()
window.mainloop()
升级版
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import tkinter as tk
#点击按钮,出现网页进行赋值
def func():
browser = webdriver.Chrome()
#获取访问的网址
browser.get('https://www.baidu.com')
n = 0
while n<=10:
n+=1
print(n)
time.sleep(10)
#发送文字到目标id
kw=browser.find_element_by_id("kw")
su=browser.find_element_by_id("su")
time.sleep(12)
try:
# 输入内容:selenium
kw.send_keys('selenium'+str(n))
print('搜索关键词:selenium')
except Exception as e:
print('fail')
time.sleep(13)
#点击目标id
su.click()
time.sleep(15)
#再次获得输入框
kw1 = browser.find_element_by_id("kw")
try:
kw1.clear()
print('成功清空输入框')
except Exception as e:
print('fail清空输入框')
if n==10:
print("最终完成")
continue
#设置窗口
window = tk.Tk()
#设置标题
window.title("自动回答问题")
window.geometry("300x200")
#设置按钮
button=tk.Button(text="开始执行",width=10,height=3,command=func)
#放置按钮
button.pack()
window.mainloop()
|