我想写一个机器人程序,打开许多浏览器,并在每个浏览器上登录到不同的帐户。我正在尝试使用Python selenium和多处理。告诉我是否可以做到这一点以及如何做到。我附上了我的代码示例。
来自selenium导入webdriver从selenium.webdriver.common.by导入依据从selenium.webdriver.common.action_chains导入ActionChains从配置导入登录,密码导入时间来自多处理导入池进口泡菜
urls_list = ["https://discord.com/login","https://discord.com/login"]
def get_data(url):
try:
options = webdriver.ChromeOptions()
options.add_argument(
"User-agent= Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36")
driver = webdriver.Chrome(
executable_path="D:chromedriver.exe",
options=options
)
driver.get(url=url)
time.sleep(5)
email_input = driver.find_element(By.CLASS_NAME, "inputField-2RZxdl")
password_input = driver.find_element(By.NAME, "password")
email_input.clear()
password_input.clear()
email_input.send_keys(f"{login}")
password_input.send_keys(f'{password}')
btn_entry = driver.find_element(By.CLASS_NAME, "button-1cRKG6").click()
time.sleep(4)
folder_find = driver.find_element(By.CLASS_NAME, "closedFolderIconWrapper-3tRb2d").click()
time.sleep(1)
project_find = driver.find_element(By.CSS_SELECTOR, "#folder-items-2001708806 > div:nth-child(1) > div:nth-child(2) > div > div > svg > foreignObject > div").click()
driver.execute_script("window.scrollTo(0, 200)")
time.sleep(1)
chanel_find = driver.find_element(By.XPATH, '//*[@id="channels"]/ul/li[32]/div/div/a')
actions = ActionChains(driver)
actions.move_to_element(chanel_find).perform()
chanel_find.click()
time.sleep(1)
message_find = driver.find_element(By.CSS_SELECTOR, "#message-reactions-1008818995081781248 > div:nth-child(1) > div > div")
actions.move_to_element(message_find).perform()
message_find.click()
except Exception as ex:
print(ex)
finally:
driver.close()
driver.quit()
if __name__ == "__main__":
p = Pool(processes=2)
p.map(get_data, urls_list)
查看这篇文章。您搜索的可能是ThreadPoolExecutor()
。这给了我一些帮助,但你需要自己尝试,我建议你在测试时尽可能减少变量。此外,如果你想要多个线程,这篇文章有一个庞大的实现,并且解释很中肯。