Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

multithreading - How to I stop multiple selenium/chromium instances on different threads from interfearing with each other in python

I have a little selenium script that just types a string into an input box, and I am doing this a bunch of times and I also need to keep the windows open together. And I am multi threading it to make it faster. Basically it goes through a loop like for i in range(10). And in each iteration, it spawns a new chrome driver, goes to a url, but the problem is that the last thread that spawns is the one with the input box the is receiving all the input.

I will show you a simplified version of my code, sorry if my explanation isn't clear

from threading import Thread
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions()
options.add_argument('headless')
# These are not the actual values i am sending
def createBot():
    global driver
    driver = webdriver.Chrome(options = options)
    driver.get("https://example.com")
    inputbox = driver.find_element_by_id("input-box-whatever")
    inputbox.send_keys("username")
    inputbox.send_keys(Keys.ENTER)
#I keep this here to keep the program running, idk if it does anything
createBot()
amount-=1
for i in range(100):
    Thread(target=lambda: createBot()).start()

Basically instead of typing 'username' once on each instance, instead it types something like usernameusernameusername on one instance. Sorry if didn't make much sense, but I had to take away some of my original code for reasons. I heard that declaring driver as a global stops it from closing but I don't know if that's true.

Update: I fixed it, instead of using global to stop it from closing, I just asked the user how long they want the chromium instances to stay up, and than I time.sleep() for that amound. Another update: Tried with 100 bots, nearly crashed my computer, so I think I am going to find a better solution. I looked through the code of other things like this and it turns out they are just using the api for the service. But it's not a public api and it's really hard to use.

question from:https://stackoverflow.com/questions/66058064/how-to-i-stop-multiple-selenium-chromium-instances-on-different-threads-from-int

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...