admin管理员组文章数量:1022018
My current code doesn't switch from one profile to other
from DrissionPage import ChromiumOptions, ChromiumPage,Chromium
co = ChromiumOptions()
co.set_user(user='event_name1')
page = ChromiumPage(co)
co = ChromiumOptions()
co.set_user(user='event_name2')
page = ChromiumPage(co)
My current code doesn't switch from one profile to other
from DrissionPage import ChromiumOptions, ChromiumPage,Chromium
co = ChromiumOptions()
co.set_user(user='event_name1')
page = ChromiumPage(co)
co = ChromiumOptions()
co.set_user(user='event_name2')
page = ChromiumPage(co)
Share
Improve this question
edited Nov 19, 2024 at 19:12
Bending Rodriguez
7312 gold badges9 silver badges41 bronze badges
asked Nov 19, 2024 at 7:06
Osama Bin GhazanfarOsama Bin Ghazanfar
112 bronze badges
3
|
1 Answer
Reset to default 1from DrissionPage import Chromium
from concurrent.futures import ThreadPoolExecutor, as_completed
# Dictionary of event links
event_links = {
'1': 'http://drissionpage.cn/browser_control/mode_change/',
'2': 'https://DrissionPage.cn',
'3': 'https://example'
}
# Predefined Chromium instances with specific debugging ports
browsers = {
'1': Chromium(9222), # Browser 1 on port 9222
'2': Chromium(9333), # Browser 2 on port 9333
'3': Chromium(9444) # Browser 3 on port 9444
}
# Function to open a URL in a specific browser instance
def open_browser(args):
session_id, url = args # Unpack session ID and URL
# Retrieve the associated browser instance
browser = browsers[session_id]
tab = browser.latest_tab
# Navigate the browser to the target URL
tab.get(url)
print(f"Browser session {session_id} opened for URL: {url}")
return session_id, url
# Main function to manage concurrent browser sessions
def main():
# Use ThreadPoolExecutor for concurrent execution
with ThreadPoolExecutor(max_workers=len(event_links)) as executor:
# Submit tasks for each session (key, URL pair)
futures = {executor.submit(open_browser, (key, url)): (key, url) for key, url in event_links.items()}
# Process results as they are completed
for future in as_completed(futures):
try:
session_id, url = future.result() # Retrieve session ID and URL
print(f"Session {session_id} completed for URL: {url}")
except Exception as e:
print(f"Error in session: {e}")
# Run the script
if __name__ == "__main__":
main()
My current code doesn't switch from one profile to other
from DrissionPage import ChromiumOptions, ChromiumPage,Chromium
co = ChromiumOptions()
co.set_user(user='event_name1')
page = ChromiumPage(co)
co = ChromiumOptions()
co.set_user(user='event_name2')
page = ChromiumPage(co)
My current code doesn't switch from one profile to other
from DrissionPage import ChromiumOptions, ChromiumPage,Chromium
co = ChromiumOptions()
co.set_user(user='event_name1')
page = ChromiumPage(co)
co = ChromiumOptions()
co.set_user(user='event_name2')
page = ChromiumPage(co)
Share
Improve this question
edited Nov 19, 2024 at 19:12
Bending Rodriguez
7312 gold badges9 silver badges41 bronze badges
asked Nov 19, 2024 at 7:06
Osama Bin GhazanfarOsama Bin Ghazanfar
112 bronze badges
3
-
You would need to give the actual name of profile, what i mean is suppose you created a profile in chrome (Profile 2). Now if you try to switch to this profile using this
co.set_user("Profile 2")
, drission wont switch to this profile because under the hood it is looking for a unique identifier of this profile, if it cant find then drission will create a new profile. But if you want to switch back to default profile which is named as Profile 1. Then you would have to switch like thisco.set_user("Default")
. Then only drission will know ok i have to switch toProfile 1
. – Aniket Commented Nov 19, 2024 at 7:23 - @Aniket Thanks for your answer, I tried this thing multiple times but it does not work. The issue is that Chromium targets a port for opening a browser and if we do not provide a port to it then it will open the browser at its default port so if we have to open multiple instances at a time then for each instance we have to provide different port name or call a auto port for various number of instances – Osama Bin Ghazanfar Commented Nov 20, 2024 at 8:13
- I understand but your question was how can you switch multiple chrome profiles by their names!! So i tried to help you based on that only but nevertheless it worked so its fine!! – Aniket Commented Nov 21, 2024 at 16:27
1 Answer
Reset to default 1from DrissionPage import Chromium
from concurrent.futures import ThreadPoolExecutor, as_completed
# Dictionary of event links
event_links = {
'1': 'http://drissionpage.cn/browser_control/mode_change/',
'2': 'https://DrissionPage.cn',
'3': 'https://example'
}
# Predefined Chromium instances with specific debugging ports
browsers = {
'1': Chromium(9222), # Browser 1 on port 9222
'2': Chromium(9333), # Browser 2 on port 9333
'3': Chromium(9444) # Browser 3 on port 9444
}
# Function to open a URL in a specific browser instance
def open_browser(args):
session_id, url = args # Unpack session ID and URL
# Retrieve the associated browser instance
browser = browsers[session_id]
tab = browser.latest_tab
# Navigate the browser to the target URL
tab.get(url)
print(f"Browser session {session_id} opened for URL: {url}")
return session_id, url
# Main function to manage concurrent browser sessions
def main():
# Use ThreadPoolExecutor for concurrent execution
with ThreadPoolExecutor(max_workers=len(event_links)) as executor:
# Submit tasks for each session (key, URL pair)
futures = {executor.submit(open_browser, (key, url)): (key, url) for key, url in event_links.items()}
# Process results as they are completed
for future in as_completed(futures):
try:
session_id, url = future.result() # Retrieve session ID and URL
print(f"Session {session_id} completed for URL: {url}")
except Exception as e:
print(f"Error in session: {e}")
# Run the script
if __name__ == "__main__":
main()
版权声明:本文标题:python - How can i switch multiple chrome profiles by their names in DrissionPage ChromiumPage? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745578796a2157185.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
co.set_user("Profile 2")
, drission wont switch to this profile because under the hood it is looking for a unique identifier of this profile, if it cant find then drission will create a new profile. But if you want to switch back to default profile which is named as Profile 1. Then you would have to switch like thisco.set_user("Default")
. Then only drission will know ok i have to switch toProfile 1
. – Aniket Commented Nov 19, 2024 at 7:23