Python selenium keep browser open

前端 未结 2 803
说谎
说谎 2020-12-17 23:50

I am using selenium to open some browser windows for marketing reasons. I simply open my marketing sources, login via selenium and start working.

The problem is, tha

2条回答
  •  抹茶落季
    2020-12-18 00:13

    You can also add global browser like so:

        try:
            # Connect
            chrome_options = webdriver.ChromeOptions()
            chrome_options.add_argument("--incognito")
            global browser # this will prevent the browser variable from being garbage collected
            browser = webdriver.Chrome('drivers/chromedriver.exe', chrome_options=chrome_options)
            browser.set_window_size(1800, 900)
            browser.get("https://www.instagram.com/accounts/login/?hl=de")
            browser.find_element(By.NAME, 'username').send_keys('MYEMAIL', Keys.TAB, 'MYPW', Keys.ENTER)
        except Exception as e:
            print (e, 'Instagram')
    
    open_instagram()
    

    Source

提交回复
热议问题