Keep browser open after executing function in Selenium Python - python

I want to create a function that opens the browser, just to have it in a more compact way every time I need to call it, using Selenium Python.
This the code:
def prepare_website():
chrome_driver = ChromeDriverManager().install()
driver = Chrome(service=Service(chrome_driver))
driver.maximize_window()
driver.get(some_link)
prepare_website()
The problem is that after executing it closes the browser. How to keep it open instead, as it actually would do if it were not in a function?

#Luca Giovanni Voglino, you can try the following modified code. basically detach option will close the driver but keep the browser open:
def prepare_website():
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chrome_driver = ChromeDriverManager().install()
driver = Chrome(options=chrome_options, service=Service(chrome_driver))
driver.maximize_window()
driver.get("http://www.google.com")
prepare_website()
you will need to add the following import:
from selenium.webdriver.chrome.options import Options

Related

how to make selenium run 'chrome.exe --remote-debugging-port=9222 --user-data-dir='C:\\selenium\\ChromeProfile'

I am writing code to make selenium take over an instance of chrome that has all my bookmarks and stuff. So I created a chrome profile and I have the command
chrome.exe --remote-debugging-port=9222 --user-data-dir='C:\\selenium\\ChromeProfile'
this works when you run it in the python terminal, but I can't just put it into the code.
I have tried using
import os
os.system("chrome.exe --remote-debugging-port=9222 --user-data-dir='C:\\selenium\\ChromeProfile'")
but that returns with
Failed To Create Data Directory: Google Chrome cannot read and write to its data directory: C\selenium\ChromeProfile
Does someone know what I am doing wrong or a command that will run the chrome command to open this specific profile?
My total code so far looks like this
import subprocess
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
os.system("chrome.exe --remote-debugging-port=9222 --user-data-dir='C:\\selenium\\ChromeProfile'")
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
chrome_driver = "C:\\selenium\\chromedriver.exe"
driver = webdriver.Chrome(chrome_driver, chrome_options=chrome_options)
#Print website title to make sure its connected properly
driver.get('https://google.com')
print(driver.title)
search_bar = driver.find_element_by_name('q')
search_bar.send_keys('test')
Nevermind people, I figured it out. I am posting this here for anyone else in the future who may run into the same issue I did where you want python to open the browser in debug mode on the port.
Here is the code
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--remote-debugging-port=9222")
chrome_options.add_argument('user-data-dir=C:\\selenium\\ChromeProfile')
chrome_driver = "C:\\selenium\\chromedriver.exe"
driver = webdriver.Chrome(chrome_driver, chrome_options=chrome_options)
#Print website title to make sure its connected properly
driver.get('https://google.com')
print(driver.title)
search_bar = driver.find_element_by_name('q')
search_bar.send_keys('test')
I had to add two lines of
chrome_options.add_argument()
for some reason it didn't like when I put them in the same parenthesis.
I hope I help someone in the future.

How to maximize chrome browser in default when using selenium in python

When I run chrome driver from selenium the browser opens in minimized windows. but I want it to open by default as maximized
You can either use
driver.maximize_window() or
chrome_options.add_argument("--start-maximized") which will maximize the browser when ever it opens.
The following code was taken from this link. https://pythonbasics.org/selenium-maximize/
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.maximize_window()
time.sleep(5)
driver.get("https://www.python.org")
This is one of the methods to do so.

Python Selenium Firefox - how to enable headless-mode as part of a class/object?

I have the following code:
options = Options()
options = options.set_headless( headless=True)
class Sel_Driver():
def __init__(self):
self.driver = webdriver.Firefox(firefox_options=options)
I can then use self.driver.get(url) as part of a method to open urls I feed in. This works - I can feed in and open the URLs, but they don't in headless mode.
(I initially defined the driver as self.driver = webdriver.Firefox(firefox_options=Options().set_headless(headless=True) - but that didn't work, so I tried it as above).
What am I missing? I don't understand why the driver is able to open pages, but the options aren't enabled.
Please try following code :
options = Options()
options.add_argument("--headless")
driver = webdriver.Firefox(firefox_options=options)
This will work for you for sure. Try it.Please specify the path of the driver. It is for chrome change it to firefox.
from pyvirtualdisplay import Display
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=options, executable_path="C:\\Users\\Username\\Downloads\\chromedriver.exe")
print("Firefox Headless Browser Invoked")
driver.get('https://www.facebook.com/')
jks = driver.find_element_by_id("email").get_attribute("class")
print(jks)
driver.quit()

Selenium Python minimize browser window

I know how to call a method to maximize window from driver object.
driver.maximize_window()
But what method should I use when I need to minimize browser window (hide it)?
Actually, driver object hasn't maximize_window attribute.
My goal to work silently with the browser window. I don't want to see it on my PC.
Option 1: Use driver.minimize_window()
Option 2: Use --headless
Example 1:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver = webdriver.Chrome(options=options)
driver.get("enter your url here")
Example 2:
from selenium import webdriver
driver = webdriver.Chrome()
driver.minimize_window()
driver.get("enter your url here")
I would like to give a suggestion and make you correct that there's a method to minimize the window as you are required to do.
driver.minimize_window()
I would also like to mention that this will definitely work in python3, hope you were working in python.
Just driver.minimize_window() or use a headless browser as PhamtonJS or Chromium.
Example:
PROXY_SERVER = "127.0.0.1:5566" # IP:PORT or HOST:PORT
options = webdriver.ChromeOptions()
options.binary_location = r"/usr/bin/opera" # path to opera executable
options.add_argument('--proxy-server=%s' % PROXY_SERVER)
driver = webdriver.Opera(executable_path=r"/home/prestes/Tools/operadriver_linux64/operadriver", options=options)
driver.minimize_window()
driver.get(url)
html = driver.page_source
soup = BeautifulSoup(html, "lxml")
print(html)
driver.quit()
Maybe try a headless browser? Chrome headless or PhantomJS.
http://phantomjs.org/
Keep in mind that development is suspended for Phantom js. You may want to use other alternatives if it doesn't work or gives errors -
https://github.com/dhamaniasad/HeadlessBrowsers
A headless browser is a web browser without a GUI.
Try this,
driver.set_window_position(0, 0)

How to load default profile in Chrome using Python Selenium Webdriver?

I'd like to launch Chrome with its default profile using Python's webdriver so that cookies and site preferences persist across sessions.
How can I do that?
This is what finally got it working for me.
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Path") #Path to your chrome profile
w = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", chrome_options=options)
To find path to your chrome profile data you need to type chrome://version/ into address bar . For ex. mine is displayed as C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default, to use it in the script I had to exclude \Default\ so we end up with only C:\Users\pc\AppData\Local\Google\Chrome\User Data.
Also if you want to have separate profile just for selenium: replace the path with any other path and if it doesn't exist on start up chrome will create new profile and directory for it.
This solved my problem. (remove Default at the end)
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=/home/username/.config/google-chrome")
cls.driver = webdriver.Chrome(options=options,
executable_path="./../ext/chromedriver")
Chrome_Options ist deprecated. Use options instead
I solved my problem with answer of "Yoannes Geissler".
In my case My profile was named "Profile 2"
My Code :
options = webdriver.ChromeOptions()
options.add_argument('--user-data-dir=C:/Users/GOD/AppData/Local/Google/Chrome/User Data')
options.add_argument('--profile-directory=Profile 2')
wd = webdriver.Chrome(options=options)
The below line solved my problem:
options.add_argument('--profile-directory=Profile 2')
Just to share what worked for me. Using default's profile was complicated, chrome keeps crashing.
from pathlib import Path
from selenium import webdriver
driver_path = Path("{}/driver/chromedriver75.exe".format(PATH_TO_FOLDER))
user_data_dir = Path("{}/driver/User Data".format(PATH_TO_FOLDER))
options = webdriver.ChromeOptions()
# TELL WHERE IS THE DATA DIR
options.add_argument("--user-data-dir={}".format(user_data_dir))
# USE THIS IF YOU NEED TO HAVE MULTIPLE PROFILES
options.add_argument('--profile-directory=Default')
driver = webdriver.Chrome(executable_path=driver_path, options=options)
driver.get("https://google.com/")
By doing this Chrome will create the folder User Data and keep all the data in it where I want and it's easy to just move your project to another machine.
This answer is pretty simple and self-explained.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
exec_path_chrome = "path/to/Google Chrome" #Do not use this path that is extracted from "chrome://version/"
exec_path_driver = "path/to/chromedriver"
ch_options = Options() #Chrome Options
ch_options.add_argument("user-data-dir = /path/to/Chrome Profile") #Extract this path from "chrome://version/"
driver = webdriver.Chrome(executable_path = exec_path_driver, options = ch_options) #Chrome_Options is deprecated. So we use options instead.
driver.get("https://stackoverflow.com/a/57894065/4061346")
As #MadRabbit said type chrome://version/ into the address bar to find the path to your chrome profile data.
It appears like this in Windows C:\Users\pc\AppData\Local\Google\Chrome\User Data\Default
It appears like this in Mac /Users/user/Library/Application Support/Google/Chrome/Default
So all you have to do is to erase the last portion Default from the profile path.
Note: Make sure you don't run more than one session at the same time to avoid problems.
This is what I did.
import chromedriver_binary
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
chrome_options.add_argument(r"--user-data-dir=User Data Directory")
chrome_options.add_argument(r"--profile-directory=Profile name")
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches",["enable
logging"])
chrome_options.add_experimental_option("excludeSwitches", ["enable
automation"])
chrome_options.add_argument("start-maximized")
self.driver = webdriver.Chrome(chrome_options=chrome_options)
self.wait = WebDriverWait(self.driver, 20)
Here we do not need to give the chrome driver path.

Categories

Resources