Home > Net >  python selenium opens new browser
python selenium opens new browser

Time:12-17

I'm signed in to Whatsapp (from the main browser) then i coded the message bot in selenium but then when i opened it a new browser was opened.

from selenium import webdriver
from selenium.webdriver.common.by import By
import time 
from selenium.webdriver.common.keys import Keys
driver = webdriver.Edge()
driver.get("https://web.whatsapp.com/")
driver.maximize_window()
time.sleep(10)
driver.close()

When the site opened, it wasn't logged in in the browser, but I did.

CodePudding user response:

You can load the profile data. You can get the Profile data using edge://version

enter image description here

Copy the path and paste it after user-data-dir=

Also the path is something like this ...\...\...\Edge\User Data\Default so remove the last dir name like this Edge\User Data

from selenium import webdriver
from selenium.webdriver.common.by import By
import time 
from selenium.webdriver.common.keys import Keys
options = webdriver.EdgeOptions()
options.add_argument(r"user-data-dir=PATH/TO/PROFILE/DATA",)
driver = webdriver.Edge(options=options)

driver.get("https://web.whatsapp.com/")
driver.maximize_window()
time.sleep(10)
driver.close()

  • If you get any error like

unknown error: Microsoft Edge failed to start: exited normally. (unknown error: DevToolsActivePort file doesn't exist)

So, Use chrome: To get the data path chrome://version Paste the dir path after used-data-dir and remove /Default from the path

from selenium import webdriver
from selenium.webdriver.common.by import By
import time 
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions()
options.add_argument(r"user-data-dir=PATH/TO/PROFILE/DATA",)
driver = webdriver.Chrome(options=options)

driver.get("https://web.whatsapp.com/")
driver.maximize_window()
time.sleep(10)
driver.close()

  • Related