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.
Related
I'm trying to use Selenium in Python to fill out a form, which is why I need it to run in an existing Firefox window where I'm logged in due to the sensitive login data. But I can't seem to find a way to prevent the driver launching in a new window where I am logged out. Is there a way to do this?
I've tried using options to set preference:
`from selenium.webdriver.firefox.options import Options
options = Options()
options.set_preference("browser.tabs.loadInBackground", False)
driver = webdriver.Firefox(options=options)`
I also tried using the -no-remote command on Firefox in cmd before running the script, but it didn't work either.
From chrome driver you can achieve this.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=C:/Users/Designer1/AppData/Local/Google/Chrome/User Data/Profile 1")
driver = webdriver.Chrome(chrome_options=options,executable_path="C:\webdrivers\chromedriver.exe")
I try to open browser using Selenium in Python and after the browser opens, it exits from it, I tried several ways to write my code but every possible way works this way.
Thank you in advance for help
`from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
s=Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)
driver.get("https://amazon.com")`
I expected the browser to open amazon.com and stay like this until I close or the programme close it.
Actual result - when the browser loads the website, it exists from itself.
Use driver.close() function after getting result ;)
My OS → Microsoft Windows 11
GOOGLE CHROME:
I have Google website open and I want to open the Stack Overflow website in a new tab but the screen keeps showing the Google website, like this:
My first attempt was trying it with the webbrowser module and its autoraise argument:
sof = 'https://stackoverflow.com'
webbrowser.open(sof, new=0, autoraise=False)
webbrowser.open(sof, new=2, autoraise=False)
webbrowser.open_new_tab(sof)
None of the above options caused the tab in Chrome to open in the background keeping focus on the tab that was already open.
So I went for another try using subprocess and its getoutput function:
r = subprocess.getoutput(f"google-chrome-stable https://stackoverflow.com")
r
That option didn't even open a new tab in my browser.
MOZILLA FIREFOX:
My attempt was trying it with the webbrowser module and its autoraise argument (As my default browser is different I need to set the browser):
sof = 'https://stackoverflow.com'
webbrowser.register('firefox',
None,
webbrowser.BackgroundBrowser("C://Program Files//Mozilla Firefox//firefox.exe"))
webbrowser.get('firefox').open(sof, new=0, autoraise=False)
In neither of the two I managed to make this functionality work.
How should I proceed?
Chrome:
I don't think it is feasible (at least not w/ chrome).
See this StackExchange answer for details. Especially the mentioned bug that most likely will never get fixed.
Firefox:
Same here, did some research and the only solution to get it to work is changing the config option
'browser.tabs.loadDivertedInBackground' to 'true'
launch background tab like this (or from py with os or subprocess module):
"C:\Program Files\Mozilla Firefox\firefox.exe" -new-tab "https://stackoverflow.com/"
See https://stackoverflow.com/a/2276679/2606766. But again I don't think this solves your problem, does it?
maybe you can try to stimulate the keyboard using pynput library,
then stimulating crtl + Tab to change to the new open website?
*edit: to open the previous tab, press crtl + shift + tab
import webbrowser, time
from pynput.keyboard import Key,Controller
keyboard = Controller()
webbrowser.open("https://www.youtube.com/")
time.sleep(3)
keyboard.press(Key.ctrl)
keyboard.press(Key.shift)
keyboard.press(Key.tab)
keyboard.release(Key.ctrl)
keyboard.release(Key.shift)
keyboard.release(Key.tab)
Are you familiar with CDP and Selenium?
Option A:
CDP Via Selenium Controlled browser:
from selenium import webdriver
driver = webdriver.Chrome('/path/bin/chromedriver')
driver.get("https://example.com/")
driver.execute_cdp_cmd(cmd="Target.createTarget",cmd_args={"url": 'https://stackoverflow.com/', "background": True})
"background": True is key
EDIT:
On linux the browser doesn't close, at least for me.
If it dies when the code dies, try the following:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
CHROME_DRIVER_PATH = '/bin/chromedriver'
chrome_options = Options()
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(CHROME_DRIVER_PATH, chrome_options=chrome_options)
driver.get("https://example.com/")
driver.execute_cdp_cmd(cmd="Target.createTarget",cmd_args={"url": 'https://stackoverflow.com/', "background": True})
Option B:
Manually run chrome with a debug port (via cmd, subprocess.popen or anything else)
chrome.exe --remote-debugging-port=4455
and then either use a python CDP Client such as trio
or tell selenium to use your existing browser:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:4455")
# Change chrome driver path accordingly
CHROME_DRIVER_PATH= r"C:\chromedriver.exe"
driver = webdriver.Chrome(CHROME_DRIVER_PATH, chrome_options=chrome_options)
driver.get("https://example.com/")
driver.execute_cdp_cmd(cmd="Target.createTarget",cmd_args={"url": 'https://stackoverflow.com/', "background": True})
Simpliest is to switch to -1 window_handles with chromedriver
from selenium import webdriver
driver = webdriver.Chrome('chrome/driver/path')
driver.switch_to.window(driver.window_handles[-1])
I need to load my own Default chrome profile where all my saved login info is, instead of selenium logging me into my websites.
I have taken this code from stackoverflow, cannot find the exact user. My issue with this code is selenium opens chrome but does not load the correct profile. Every time I open run the code it creates a new "scoped_dir" folder and runs the profile "default" from there chrome version
(C:\Users\farja\AppData\Local\Temp\scoped_dir[bunch of numbers]\Default).
I have tried closing chrome and then running my code which doesn't work
I am thinking there is a big flaw in my code but do not know what it is or how to find it. A relevant answer for 2022 would be very very much appreciated as I have literally been stuck on this for a week now and have tried multiple answers on stackoverflow, the web and youtube but nearly all give me a deprecation error.
Thank you for taking the time to read.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
ser = Service(r'C:\Users\farja\Documents\Instagram Programmes\Scheduler 2\chromedriver')
op = webdriver.ChromeOptions()
s = webdriver.Chrome(service=ser, options=op)
op.add_argument(r"--user-data-dir=C:\\Users\\farja\\AppData\\Local\\Google\\Chrome\\User Data")
op.add_argument("--profile-directory=Default")
s.get("chrome://version/")
As your usecase is to load the Default Chrome Profile you can use the argument user-data-dir and remove the argument --profile-directory as follows:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
op = webdriver.ChromeOptions()
op.add_argument(r"--user-data-dir=C:\\Users\\farja\\AppData\\Local\\Google\\Chrome\\User Data\\Default")
ser = Service(r'C:\Users\farja\Documents\Instagram Programmes\Scheduler 2\chromedriver')
s = webdriver.Chrome(service=ser, options=op)
s.get("chrome://version/")
References
You can find a couple of relevant detailed discussions in:
How to open a Chrome Profile through Python
How to open a Chrome Profile through --user-data-dir argument of Selenium
The Selenium Chrome Webdriver does not load my default Chrome profile.
I already tried many other Stack Overflow solutions including changing the path (and using the local Chrome App, which gives a 'permission denied' Error).
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=~/Library/Application Support/Google/Chrome/Default")
options.add_argument("--profile-directory=Default")
driver = webdriver.Chrome()
driver.get("https://tagmanager.google.com/#/home")
assert "Google Analytics" in driver.title
account_element = driver.find_element_by_css_selector("div.account.card div#149385038 table")
accountPublicId = driver.find_element_by_css_selector("div#149385038 table td.account__public-id")
The result is still the same; it loads only a 'naked' Chrome Webdriver instead of loading the local default profile (I use everyday for work).
Update:
I don't know how or why but now, when I quit Chrome and start Chrome through the Python Script, Google Chrome starts with my profile but does not use the cookies I have in that profile.
I will see if I can add the cookies "manually" with an options.add_arguments.
Try this:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\ProfilePath") ##profile path
w = webdriver.Chrome(executable_path="C:\\chromedriver.exe", chrome_options=options)
you may try this
options.add_argument("user-data-dir=C:\\Test")
did not add anything to the
options arguments (looking via debugger it was an empty list)
options.arguments.append("user-data-dir=C:/test/") managed to update the list of arguments
and it worked.