Selenium - python webdriver exits from browser after loading - python

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 ;)

Related

When I start my program (Selenium) it opens web page then it closes immediately after whithout error in log

My code is very short. When I try it, the program opens web page but program closes immediately after.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
driver = webdriver.Chrome()
driver.get("https://google.com")
I believe the problem is coming from my environment, so I have not tried any changes to the code to resolve this.
You have to add the below chrome options:
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options)
driver.get("https://google.com")

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.

Wait for cookie with Selenium python webdriver

I basically here the same question as here, only with Python.
I want to use Selenium to get the user to log in with a browser, then grab the session cookie that will contain the log in information, and finally close the browser. In an interactive session, it's easy to just wait until the authentication is complete before calling get_cookie(). But in run mode, I'm stuck.
From the Selenium documentation, I get that it is about defining a wait strategy. So I tried to repurpose their code example as follows:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.get('https://examplecom/')
sessionId = WebDriverWait(driver, timeout=120).until(lambda d: d.get_cookie('session_cookie')['value'])
driver.quit()
Unfortunately, the following error is immediately raised:
TypeError: 'NoneType' object is not subscriptable
What should I do instead?
Duh! I was calling the value key as part of the wait function, thus even before it was available! This works:
sessionId = WebDriverWait(driver, timeout=120).until(lambda d: d.get_cookie('TK'))['value']
To save cookies in Selenium, you can do as follows:
import os
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
expanduser = os.path.expanduser('~')
options = webdriver.ChromeOptions()
and then add option of user-data-dir:
options.add_argument(f'--user-data-dir={expanduser}\\AppData\\Local\\Google\\Chrome\\any_Name_You_Want')
and then create your browser with these option:
browser = webdriver.Chrome(ChromeDriverManager().install(), options=options)

I want to open a website using chrome web driver and python

import time
import pandas as pd
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
#Open browser
driver = webdriver.Chrome(ChromeDriverManager().install())
url='https://www.premierleague.com/players'
driver.get(url)
This the code I have. It only opens for a second and then closes again. Does anyone know why?
I am assuming the problem is with the chrome driver manager, but I can't figure out how to fix it.
You can use time.sleep(10) to keep the window open. I'm using 10 as an example for the number of seconds.

Chrome browser closes immediately after loading from selenium

I am running a basic python program to open the Chrome Window but as soon as the code executes, the window is there for a sec and then it closes immediately.
from selenium import webdriver
import time
browser = webdriver.Chrome(executable_path=r"C:\APIR\chromedriver.exe")
browser.maximize_window()
browser.get("https://www.google.com")
Chromedriver version: 91.0.4472.101
Chrome Version: 91.0.4472.164
Any help would be appreciated.
Thank you
It closes because the program ends.
You can:
Wait with time.sleep, for example time.sleep(10) to keep the browser open for 10 seconds after everything is done
Have the user press enter with input()
Or detect when the browser is closed. Many ways to do that.
Example: https://stackoverflow.com/a/52000037/8997916
You could also catch the BrowserUnreachable exception in a loop with a small delay
For Edge Browser
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from webdriver_manager.microsoft import EdgeChromiumDriverManager
options = webdriver.EdgeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Edge(options=options, service=Service(EdgeChromiumDriverManager().install()))
driver.maximize_window()
driver.get('https://stackoverflow.com/questions/68543285/chrome-browser-closes-immediately-after-loading-from-selenium')
For Chrome Browser
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(options=options, service=Service('./path/to/chromedriver'))
driver.maximize_window()
driver.get('https://stackoverflow.com/questions/68543285/chrome-browser-closes-immediately-after-loading-from-selenium')

Categories

Resources