pythonseleniumbrave-browser

Selenium opens new browser instance (Brave Browser)


absolute noob here but I recently started a python automation project which checks the day of the week, and if it's a weekday it should open a browser with my college schedule for example, and if it's a weekend day it should open up different websites.

I use the brave browser as my default browser but I was wondering if it is possible to open up my regular browser profile when I run my code instead of the new instance where it does not have all my bookmarks and passwords?

Thanks in advance!

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
import datetime

day = datetime.datetime.now()
dag = day.weekday()
def Testing():
    if dag >= 5:
        return Weekend()
    else:
        return Weekday()


def Weekend():
    options = Options()
    options.add_argument("--window-size=1920,1080")
    ## options.add_argument("/Users/vadim/Library/Application Support/BraveSoftware/Brave-Browser")
    options.binary_location = '/Applications/Brave Browser.app/Contents/MacOS/Brave Browser'
    driver_path = '/usr/local/bin/chromedriver'
    driver = webdriver.Chrome(options=options, executable_path=driver_path)
    driver.get('https://outlook.live.com/mail/0/inbox')
    Outlook_Aanmelden = driver.find_element_by_xpath('/html/body/header/div/aside/div/nav/ul/li[2]/a')
    Outlook_Aanmelden.click()
    Email_Field = driver.find_element_by_xpath('//*[@id="i0116"]')
    Email_Field.send_keys('@live.com')
    Outlook_Volgende = driver.find_element_by_xpath('//*[@id="idSIButton9"]')
    Outlook_Volgende.click()
    time.sleep(0.5)
    Password_Field = driver.find_element_by_xpath('//*[@id="i0118"]')
    Password_Field.send_keys('pass')
    Password_Field.send_keys(Keys.ENTER)
    Inlog_Outlook = driver.find_element_by_xpath('//*[@id="idSIButton9"]')
    Inlog_Outlook.click()

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

def Weekday():
    options = Options()
    options.add_argument("--window-size=1920,1080")
    ## options.add_argument("/Users/vadim/Library/Application Support/BraveSoftware/Brave-Browser")
    options.binary_location = '/Applications/Brave Browser.app/Contents/MacOS/Brave Browser'
    driver_path = '/usr/local/bin/chromedriver'
    driver = webdriver.Chrome(options=options, executable_path=driver_path)
    driver.get('https://outlook.live.com/mail/0/inbox')
    Outlook_Aanmelden = driver.find_element_by_xpath('/html/body/header/div/aside/div/nav/ul/li[2]/a')
    Outlook_Aanmelden.click()
    Email_Field = driver.find_element_by_xpath('//*[@id="i0116"]')
    Email_Field.send_keys('@live.com')
    Outlook_Volgende = driver.find_element_by_xpath('//*[@id="idSIButton9"]')
    Outlook_Volgende.click()
    time.sleep(0.5)
    Password_Field = driver.find_element_by_xpath('//*[@id="i0118"]')
    Password_Field.send_keys('pass')
    Password_Field.send_keys(Keys.ENTER)
    Inlog_Outlook = driver.find_element_by_xpath('//*[@id="idSIButton9"]')
    Inlog_Outlook.click()

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

Testing()

Solution

  • I recommend using webbrowser instead of selenium, it's part of the python standard library so you should already have it.

    import webbrowser
    webbrowser.open('https://stackoverflow.com/')
    

    Here's the doc if you wan't to play around with it.