Selenium Python code stopping when opening chrome with user profile - python

The code works fine, it opens the browser with the user profile I want, but the code stops running in a few seconds after the browser opens, so all the code I put below the webdriver doesn't work
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Users\\I\\AppData\\Local\\Google\\Chrome\\User Data") #Path to your chrome profile
options.add_argument('--profile-directory=Profile 2')
w = webdriver.Chrome(executable_path="Chromedrive/chromedriver.exe", options=options)

Related

How do I make Selenium to launch Chrome with default profile?

I'm trying to get Selenium to launch Chrome with my default profile but fail. for some reason, the window will launch itself with no profile logged in:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:/Users/Me/AppData/Local/Google/Chrome/User Data/Default")
driver = webdriver.Chrome(options=options)
driver.get('https://google.com/')

Why isn't this Selenium opening the website I'm requesting?

import time
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\Users\Owner\AppData\Local\Google\Chrome\User Data")
options.add_argument(r"--profile-directory=Profile 11")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.get('https://www.facebook.com')
time.sleep(25)
driver.quit()
I'm attempting to write a script that opens google, picks a Chrome profile, and goes to a website. The code above opens google and signs into the profile but it does not go to Facebook. Does anyone have any idea why?

Google chrome closes automatically after executing Python selenium code despite using the option class to keep it open

I have checked online and it was mentioned that Selenium closes the browser after running unless you use the option module or the driver.quit() or driver.close() functions but I used the option as shown in the code below but Chrome still closes after 2-3 seconds.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chrome_driver_path = r"C:\Development\chromedriver.exe"
serv = Service(chrome_driver_path)
driver = webdriver.Chrome(service=serv, options=chrome_options)
driver.get("https://www.google.com")

Web Scraping: How to click button in selenium without opening webdriver?

I am using selenium scraping code in Windows VPS, Now code open chrome browser in order to click button, but I need to use the code in Ubuntu VPS.
As you know Ubuntu VPS don't provide any UI. So I need to click button in selenium without opening webdriver - chrome browser.
I tested --headless option but not works.
Follow my code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
chrome_options = Options()
# chrome_options.add_argument('--headless')
driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options = chrome_options)
link = "site url"
driver.get(link)
button = driver.find_element(By.CSS_SELECTOR, "button")
button.click()
print("Clicked Accept_BTN")

driver.get not working when opening chrome driver with cookies

I'm trying to use selenium on OSX to open a chrome window with existing cookies so that I can bypass login. When I don't add the argument 'chrome_options=option' to open chrome with my user settings, the driver.get function works fine and it opens a chrome window with no extensions loaded and browses to the url. But when I use the code as shown below, it simply opens chrome (with all extensions loaded) but does not browse to the URL. Am I missing something simple?
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=/Users/me/Library/Application Support/Google/Chrome/")
driver = webdriver.Chrome("./assets/chromedriver",chrome_options=options)
driver.implicitly_wait(30)
driver.maximize_window()
driver.get("https://www.gmail.com/")

Categories