我很困惑他们所说的“单独的WebDriver实例”使其成为线程安全,这是否意味着对于我的代码中的每个线程我将使用不同的WebDriver?
例如,如果我有 3 个线程,这意味着我必须向我的项目添加 3 个不同的网络驱动器才能单独使用其中的每一个,这是真的吗?
这就是人们所说的线程安全,这就是我很困惑的原因:
import threading
from selenium import webdriver
def thread_function():
# Initialize a new WebDriver instance for this thread
driver = webdriver.Chrome()
# Perform actions with this WebDriver instance
driver.get("https://www.example.com")
# ...
# Close the WebDriver instance when done
driver.quit()
# Create multiple threads, each with its WebDriver
thread1 = threading.Thread(target=thread_function)
thread2 = threading.Thread(target=thread_function)
# Start the threads
thread1.start()
thread2.start()
# Wait for threads to complete
thread1.join()
thread2.join()
这就是我正在尝试做的事情,我做得正确吗?
import threading
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# Define a function for the first thread
def thread_one():
# Initialize the WebDriver instance for the first thread
driver1 = webdriver.Chrome()
# Navigate to a website and interact with it
driver1.get("https://www.example.com")
search_box = driver1.find_element_by_name("q")
search_box.send_keys("Example")
search_box.send_keys(Keys.RETURN)
# Close the browser for the first thread
driver1.quit()
# Define a function for the second thread
def thread_two():
# Initialize the WebDriver instance for the second thread
driver2 = webdriver.Chrome()
# Navigate to a different website and interact with it
driver2.get("https://www.example.org")
link = driver2.find_element_by_link_text("Link")
link.click()
# Close the browser for the second thread
driver2.quit()
# Create two threads
thread1 = threading.Thread(target=thread_one)
thread2 = threading.Thread(target=thread_two)
# Start both threads
thread1.start()
thread2.start()
# Wait for both threads to complete
thread1.join()
thread2.join()
print("Both threads have completed.")
如何确保我的网络驱动器是线程安全的
我的新代码同时打开5个浏览器,这个线程安全吗?
import threading
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
# ---------------------------------------------------------------------------- #
# Selenium set up #
# ---------------------------------------------------------------------------- #
chrome_options = webdriver.ChromeOptions()
driver = webdriver.Chrome('drivers\chromedriver.exe',options=chrome_options)
# Function to open a browser and perform actions
def open_browser_and_do_stuff(thread_num):
# Initialize a new WebDriver instance for this thread
driver = webdriver.Chrome('drivers\chromedriver.exe',options=chrome_options)
try:
# Perform actions with this WebDriver instance
driver.get("https://whatismyipaddress.com/")
time.sleep(20)
search_box = driver.find_element_by_name("q")
search_box.send_keys("Thread " + str(thread_num))
search_box.send_keys(Keys.RETURN)
finally:
# Ensure the WebDriver instance is properly closed
driver.quit()
# Number of threads (browsers) to open
num_threads = 5
# Create and start threads
threads = []
for i in range(num_threads):
thread = threading.Thread(target=open_browser_and_do_stuff, args=(i + 1,))
threads.append(thread)
thread.start()
# Wait for all threads to complete
for thread in threads:
thread.join()
print("All threads have completed.")
# Wait for threads to complete
#thread1.join()
#thread2.join()
您在第一个示例中提供的代码是为每个线程创建单独的 WebDriver 实例的正确方法。
import threading
from selenium import webdriver
def thread_function():
driver = webdriver.Chrome()
driver.get("https://www.example.com")
driver.quit()
thread1 = threading.Thread(target=thread_function)
thread2 = threading.Thread(target=thread_function)
thread1.start()
thread2.start()
thread1.join()
thread2.join()