from selenium import webdriver
from time import sleep,ctime
import threading
html = "https://www.vmall.com/product/10086368169358.html"
#开启事件个数
event_count = 5
page_count = 1
#配置信息
phone_name = "夏日胡杨"
phone_set = "5G全网通 8GB+128GB"
click_text = "已售完"
# 创建线程数组
#线程组
threads = []
def huawei_main():
'''华为手机'''
#1.创建Chrome浏览器对象,这会在电脑上在打开一个浏览器窗口
browser = webdriver.Chrome()
threads2 = []
for i in range(page_count):
open_html(browser)
def open_html(browser):
'''开启网页'''
# 2.通过浏览器向服务器发送URL请求
browser.get(html)
browser.refresh()
# 3.刷新浏览器
element = browser.find_element_by_link_text(phone_name)
element.click()
element = browser.find_element_by_link_text(phone_set)
element.click()
doSomething(browser)
def doSomething(browser):
'''开始循环点击时间'''
while (1): # 这里需要子线程处理
# 4.设置浏览器的大小
browser.set_window_size(800, 300)
# 5.设置链接内容
element = browser.find_element_by_link_text(click_text)
element.click()
print("点击了一下按钮:"+ctime())
sleep(0.5)
def start_do(count):
'''开启浏览器个数'''
if count <= 0:
count = 1
if count > 20:
count = 20;
for i in range(count):
#创建线程个数
t2 = threading.Thread(target=huawei_main, args=())
threads.append(t2)
# 启动线程
for t in threads:
t.start()
# 守护线程
for t in threads:
t.join()
if __name__ == '__main__':
'''代码主入口'''
start_do(event_count)