Search code examples
pythonseleniummultiprocessingselenium-chromedriverpython-multiprocessing

Multi Processing in Selenium with Python(Cookie Clicker)


Basically, I want to make a cookie clicker bot run on 1 chrome tab but on different processes because it would make the bot click fast

import math
import os
from multiprocessing import Process, Pool, queues
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome("C:/Users/hazim/Desktop/Main/Coding/Projects/Webscraping/drivers/chromedriver")
web = "https://orteil.dashnet.org/cookieclicker/"
driver.get(web)

cookieX = "/html/body/div[2]/div[2]/div[15]/div[8]/div[1]"

def Click(xpath):
    driver.find_element_by_xpath(xpath).click()

def SendKeys(xpath, input):
    driver.find_element_by_xpath(xpath).send_keys(input)

def Wait(time, xpath):
    WebDriverWait(driver, time).until(EC.presence_of_element_located((By.XPATH, xpath)))

def Run():
    Wait(10, cookieX)
    Click(cookieX)
    


processes = []

if __name__ == "__main__":
    for i in range(os.cpu_count()):
        print('registering process %d' % i)
        processes.append(Process(target=Run))
        processes[-1].start()

    for process in processes:
        process.join()

But my problem is that when I run this it opens up 12 more tabs because I have 12 logical processes

What I think is the problem is how I can share memory with other processes

or

Selenium handles multi processes like a new object.

If I'm completely wrong can you please explain the concept in your answer thanks?


Solution

  • Every time you start selenium with driver = webdriver.Chrome you're creating a new browser to run in. That's how the tool is designed to work - it's a testing tool that needs to isolate itself from the state of other tests that may be running.

    If you want to try and interact with the same browser across several processes you'll need to refactor your script so that only create one chrome instance.

    That said...

    IF you just want to get cookies on your game, instead of trying to be a user and clicking over and over, look how the game works and interact with the code instead.

    Go to the devtools console and set the value you want:

    Game.cookies = 123456
    

    some cookies

    You can go up and down as you like: lots of cookies