使用Python在Selenium中进行多处理(Cookie Clicker)

2024-10-03 11:22:26 发布

您现在位置:Python中文网/ 问答频道 /正文

基本上,我想让cookie clicker机器人运行在1个chrome选项卡上,但运行在不同的进程上,因为这会使机器人快速点击

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()

但我的问题是,当我运行它时,它会打开另外12个选项卡,因为我有12个逻辑进程

我认为问题在于如何与其他进程共享内存

Selenium像处理新对象一样处理多个进程

如果我完全错了,请你在回答中解释一下这个概念,谢谢


Tags: fromimportdivby进程defdriverselenium
1条回答
网友
1楼 · 发布于 2024-10-03 11:22:26

每次使用driver = webdriver.Chrome启动selenium时,都会创建一个新的浏览器来运行。这就是该工具的工作原理——它是一个测试工具,需要将自身与可能正在运行的其他测试的状态隔离开来

如果您想尝试在多个进程中与同一浏览器交互,则需要重构脚本,以便只创建一个chrome实例

也就是说

如果你只是想在你的游戏中获得cookie,而不是试图成为一个用户,反复点击,那么看看游戏是如何工作的,并与代码交互

转到devtools控制台并设置所需的值:

Game.cookies = 123456

some cookies

您可以随意上下移动: lots of cookies

相关问题 更多 >