python - How can i switch multiple chrome profiles by their names in DrissionPage ChromiumPage? - Stack Overflow

My current code doesn't switch from one profile to otherfrom DrissionPage import ChromiumOptions,

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 this co.set_user("Default"). Then only drission will know ok i have to switch to Profile 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
Add a comment  | 

1 Answer 1

Reset to default 1
from 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()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745578797a4634130.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信