Python selenium multithreading, “separate WebDriver instance”
I am confused what they mean by “separate WebDriver instance” to make it Thread Safe, does this mean for each thread in my code I will use different webdriver?
for example if I have 3 threads that means I will have to add 3 different webdrive to my project to use each one of them separately is that true?
Here is what people say is thread safe, which is I am confused why:
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()
and here is what I am trying to do, am I doing it correctly?
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.")
How to make sure my webdrive is thread safe
My new code for opening 5 browsers at the same time, is this thread safe?
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()
Read more here: Source link
