selenium 搭配python多线程

from selenium import webdriver


from time import sleep, ctime
import threading


def test_baidu(browser, search):
print('start:%s' % ctime())
print('browser:%s,' % browser)

if browser == "firefox":
driver = webdriver.Firefox()
if browser == 'chrome':
driver = webdriver.Chrome()
else:
#因为没有ie驱动,所以和其他的浏览器一样,都让他默认为chrome
print("browser 参数有误,默认转为谷歌")
driver = webdriver.Chrome()


driver.get("http://www.baidu.com")
driver.find_element_by_id("kw").send_keys(search)
driver.find_element_by_id("su").click()
sleep(2)
driver.quit()

if __name__ == '__main__':
# 启动参数(指定浏览器与百度收缩内容)
lists = {'chrome': 'threading',"ie":"ssjs" ,'firefox': 'python'}
threads = []
files = range(len(lists))
print(files)

# 创建线程
print(lists.items())
for browser, search in lists.items():
t = threading.Thread(target=test_baidu, args=(browser, search))
threads.append(t)
print(threads)

# 启动线程
for t in threads:
t.start()
for t in files:
threads[t].join()
print('end:%s' % ctime())

转载于:https://www.cnblogs.com/wangqianpeng/p/11336820.html

你可能感兴趣的:(selenium 搭配python多线程)