How to keep the chrome browser open with Selenium in Python? - python

I want to open google page with a python code in visual studio editor using the following code :
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
#browser = webdriver.Chrome(ChromeDriverManager().install())
Path = "C:\Program Files (x86)\chromedriver.exe"
browser = webdriver.Chrome(Path)
browser.get('https://www.google.com/')
My problem is the chrome window it opens for 2 or 3 secondes and it close. Did some one face this issue ?

The default framework of visual studio editor would close the Chrome browser, per say any browser client after the last line of your program gets executed.
Hence, once the line of code:
browser.get('https://www.google.com/')
gets executed, the Chrome browser is CLOSED. This is the expected behavior.
Incase you like to keep the Chrome browser OPEN even after the last line of your program gets executed, you can pass the experimental option detach as true as follows:
selenium4 compatible code
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_experimental_option("detach", True)
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get('https://www.google.com/')

Related

Open Chromium with selenium in read-mode

My goal is to be able to get the main text content of a webpage without anything else.
Firefox do this using with the reader view feature. It seems that Chrome haves this as experimental feature. Despite activating the feature from code, the icon doesn't show up.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service as ChromiumService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.utils import ChromeType
chrome_options = Options()
chrome_options.add_argument("--reader-mode=true")
chrome_options.add_argument("--enable-reader-mode")
driver = webdriver.Chrome(service=ChromiumService(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()), options=chrome_options)
driver
url = "https://www.google.com"
driver.get(url)
print(driver.page_source)
driver.quit()
The command line information chrome://version/ in are showing the parameters :
Command Line
/Applications/Google Chrome.app/Contents/MacOS/Google Chrome --allow-pre-commit-input --disable-background-networking --disable-client-side-phishing-detection --disable-default-apps --disable-hang-monitor --disable-popup-blocking --disable-prompt-on-repost --disable-sync --enable-automation --enable-blink-features=ShadowDOMV0 --enable-logging --enable-reader-mode --log-level=0 --no-first-run --no-service-autorun --password-store=basic --reader-mode=true --remote-debugging-port=0 --test-type=webdriver --use-mock-keychain --user-data-dir=/var/folders/qx/y0sp0wpn5g524st9yn6f3cdm0000gn/T/.com.google.Chrome.L3pFOZ --flag-switches-begin --flag-switches-end
chrome://flags/ doesn't show the reader mode activated. Even if I activate it manually and restart, the reader mode icon is not shown in the binaries as well as Chrome on mac.
Is this feature still exists? If yes, how do I use it from selenium?
I'm unsure what you mean by this in your question:
My goal is to be able to get the main text content of a webpage
without anything else
Also I'm not sure that the search page Google.com has a reader mode.
The following code is able to toggle the Reader Mode on using selenium with a Chrome browser.
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--disable-popup-blocking")
chrome_options.add_argument('--ignore-certificate-errors')
# reference:
# https://source.chromium.org/chromium/chromium/src/+/main:docs/accessibility/browser/reader_mode.md
chrome_options.add_argument("--enable-features=ReaderMode")
# disable the banner "Chrome is being controlled by automated test software"
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=chrome_options)
url_main = "https://www.cnn.com/us/live-news/baldwin-rust-shooting-charges-decision/index.html"
driver.get(url_main)
sleep(60)
driver.quit()
Here is a screenshot showing the webpage without the reader icon using selenium with a Chrome browser.
Here is a screenshot showing the webpage with the reader icon using selenium with a Chrome browser.'
My development environment is:
----------------------------------------
My system information
----------------------------------------
Platform: macOS
Python: 3.9.0
Selenium: 3.141.0
Chromedriver: 109.0.5414.74
----------------------------------------

Selenium closes after it opens

So I'm using Python 3.9.5
Everytime I use the driver.get function, chrome appears for a second and then immediately closes the window. How do I make it like it stays open? It used to work, but when I closed VSCode and re-opened it, I did something that made it so that chrome doesn't appear to be opened.
I think you can solve this by adding the detatch option.
For example:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
Check the logs for more information. Sometimes your web browser has been updated and the chromedriver does not match with the version, so, you have to download the specific version of your browser.

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.

When trying to use a custom profile in Chrome with selenium webdriver in Python I keep getting this error

WebDriverException: Message: Can not connect to the Service /usr/lib/chromium/chromium
One of the only places which seemed to show you how, but that might only work on Windows. That's where I got the code from. How to load default profile in chrome using Python Selenium Webdriver?
My code.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=/home/me/.config/chromium/Default") #Path to your chrome profile
w = webdriver.Chrome(executable_path="/usr/lib/chromium/chromium", chrome_options=options)
w.get("https://google.com")
The browser opens up, pauses, doesn't go to the URL, then gives me that error message.
This code stops giving me the error message, but my user data does not show up.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
config = "_config/"
chromedriver = config+"chromedriver"
options.add_argument("--profile-directory='Default'") #Path to your chrome profile
w = webdriver.Chrome(chromedriver, chrome_options=options)
w.get("https://google.com")
And if I used this executable path instead, with everything else being the same, it opens up the browser with all of the desired user data, but then gives this error. WebDriverException: Message: Service chromium unexpectedly exited. Status code was: 0 The browser stays open:
w = webdriver.Chrome(executable_path="chromium", chrome_options=options)

Opening a web browser in full screen python

I am currently trying to code a basic smartmirror for my coding II class in high school with python. One thing I'm trying to do is open new tabs in full screen (using chrome). I currently have it so I can open url's, but I am not getting them in full screen. Any ideas on code I can use to open chrome in full screen?
If you're using selenium, just code like below:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://google.com')
driver.maximize_window()
As suggested, selenium is a good way to accomplish your task.
In order to have it full-screen and not only maximized I would use:
chrome_options.add_argument("--start-fullscreen");
or
chrome_options.add_argument("--kiosk");
First option emulates the F11 pressure and you can exit pressing F11. The second one turns your chrome in "kiosk" mode and you can exit pressing ALT+F4.
Other interesting flags are:
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
Those will remove the top bar exposed by the chrome driver saying it is a dev chrome version.
The complete script is:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
# chrome_options.add_argument("--start-fullscreen");
chrome_options.add_argument("--kiosk");
driver = webdriver.Chrome(executable_path=rel("path/to/chromedriver"),
chrome_options=chrome_options)
driver.get('https://www.google.com')
"path/to/chromedriver" should point to the chrome driver compatible with your chrome version downloaded from here

Categories

Resources