Answer the question
In order to leave comments, you need to log in
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()
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()
Answer the question
In order to leave comments, you need to log in
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');")
print(driver.window_handles)
driver.switch_to.window(driver.window_handles[0])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question