H
H
Held69912022-02-18 00:36:23
Python
Held6991, 2022-02-18 00:36:23

How to parallelize one browser window into several processes?

Hello, I ran into the problem of breaking the login session on a certain site, if you launch a new browser, the idea came up not to launch a new browser, but open new windows from the current one, then the session does not break, but for some reason my first browser closes, please tell me how to fix it and maybe there are some tips for improving the code, other logic or maybe more universal options (you can’t store cookies, the site breaks the session when you load them)

import os
import threading
import warnings
from time import sleep
from selenium.webdriver.common.keys import Keys
from selenium import webdriver

warnings.filterwarnings("ignore")

def app(i):
    chrome2 = webdriver.Chrome(chrome_options=options, executable_path=os.getcwd() + '\\chromedriver.exe')
    chrome2.get("site url")
    chrome.quit()

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument("--disable-blink-features=AutomationControlled")
# options.add_argument("--headless")
chrome = webdriver.Chrome(chrome_options=options, executable_path=os.getcwd() + '\\chromedriver.exe')

try:
    chrome.get("site url")
    chrome.find_element_by_xpath('//a[@href="login/index.php"]').click()
    sleep(1)
    chrome.find_element_by_xpath('//input[@id="username"]').send_keys("login")
    chrome.find_element_by_xpath('//input[@id="password"]').send_keys("pass")
    chrome.find_element_by_xpath('//button[@id="loginbtn"]').click()
    for i in range(4):
        t = threading.Thread(target=app, args=(i,))
        t.start()
except:
    print("Error")
finally:
    chrome.quit()


I also tried to open new windows with a keyboard shortcut like this, but for some reason it does not create new windows, but does everything in one
import os
import threading
import warnings
from time import sleep
from selenium.webdriver.common.keys import Keys
from selenium import webdriver

warnings.filterwarnings("ignore")

category = ['siteurl/category1',
            'siteurl/category2',
            'siteurl/category3',
            'siteurl/category4']

def app(i):
    body = chrome.find_element_by_tag_name("body")
    body.send_keys(Keys.CONTROL + 'N')
    sleep(2)
    chrome.get(f"{category[i]}")
    chrome.quit()

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument("--disable-blink-features=AutomationControlled")
# options.add_argument("--headless")
chrome = webdriver.Chrome(chrome_options=options, executable_path=os.getcwd() + '\\chromedriver.exe')

chrome.get("site url")
chrome.find_element_by_xpath('//a[@href="login/index.php"]').click()
sleep(1)
chrome.find_element_by_xpath('//input[@id="username"]').send_keys("login")
chrome.find_element_by_xpath('//input[@id="password"]').send_keys("pass")
chrome.find_element_by_xpath('//button[@id="loginbtn"]').click()
for i in range(4):
    t = threading.Thread(target=app, args=(i,))
    t.start()


Many thanks in advance to all

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
goctio, 2022-02-18
@goctio

Good afternoon. Why open new browsers? If the work is going on on one site, it is necessary that cookies are not lost - you need to open new tabs, not browsers.
It is done like this:

driver.execute_script("window.open('https://www.google.com');")

The list of active tabs can be displayed like this:
print(driver.window_handles)
Since this is really a list, you can move the browser along it, that is, navigate through the active tabs. This is done like this: Where 0 is the very first tab, and 1 is the new one you opened. The mechanics are clear. Hope it helps.
driver.switch_to.window(driver.window_handles[0])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question