我想同时打开和登录5个标签之间没有延误在标签。我有尝试:
import threading
import time
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def openurl(threadId):
print(threading.currentThread().getName(),' Thread')
url = ('https://www.facebook.com/')
print(url)
driver.execute_script("window.open('{0}')".format(url))
#driver.title(threadId)
time.sleep(0.1)
driver.set_window_size(920, 680)
driver.find_element(By.ID, "email").send_keys("xx")
driver.find_element(By.ID, "pass").send_keys("yy")
driver.find_element(By.ID, "loginbutton").click()
if __name__=='__main__':
driver = webdriver.Chrome()
windows_before = driver.current_window_handle
for i in range(5):
t1 = threading.Thread(name=i,target=openurl, args=(i,))
t1.start()
t1.join()
但它在扔: 如果我增加了睡眠时间,中间会有延迟。我试图使用驱动程序导航。但我的案例标题是相同的所有标签。 问题来源StackOverflow 地址:/questions/59387309/multithreading-in-python-selenium
它是一个例子,使用线程来运行独立的浏览器,这些浏览器在列表按钮中填充表单并设置为True,以通知登录按钮已经准备好点击。当列表按钮中的所有浏览器都设置为True时,所有浏览器都会单击按钮。 看起来它几乎是同时运行的——也许只有系统才有一些同时连接这么多的连接。 我使用列表按钮来同步所有线程。每个线程的get号在列表中都有自己的位置。我不使用append(True),因为我不确定它是否可以是线程安全的。
import time
from selenium import webdriver
from threading import Thread
def func(number):
driver = webdriver.Chrome()
#driver.set_window_size(920, 680)
driver.get(url)
driver.find_element_by_id("email").send_keys("xx")
driver.find_element_by_id("pass").send_keys("yy")
b = driver.find_element_by_id("loginbutton")
buttons[number] = True
print(buttons)
# wait for other buttons
while not all(buttons):
pass
print('click', number)
b.click()
# ---
url = 'https://www.facebook.com/'
number_of_threads = 5
#buttons = [False * number_of_threads] # create place
buttons = []
threads = []
for number in range(number_of_threads):
t = Thread(target=func, args=(number,)) # get number for place in list `buttons`
t.start()
threads.append(t)
buttons.append(False) # create place
for t in threads:
t.join()
EDIT:与thread . barrier(5)相同,5个线程必须运行barrier.wait()才能前进。
import time
from selenium import webdriver
from threading import Thread, Barrier
def func(barrier):
driver = webdriver.Chrome()
#driver.set_window_size(920, 680)
driver.get(url)
driver.find_element_by_id("email").send_keys("xx")
driver.find_element_by_id("pass").send_keys("yy")
b = driver.find_element_by_id("loginbutton")
print('wait for others')
barrier.wait()
print('click')
b.click()
# ---
url = 'https://www.facebook.com/'
number_of_threads = 5
barrier = Barrier(number_of_threads)
threads = []
for _ in range(number_of_threads):
t = Thread(target=func, args=(barrier,))
t.start()
threads.append(t)
for t in threads:
t.join()
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。