After executing selenium python code google chrome closes automatically
Asked Answered
L

6

5

This code runs without any error but it automatically closes the google chrome after searching w3school

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()

def google():
    driver.get("https://www.google.com")
    driver.find_element_by_xpath('//*[@id="tsf"]/div[2]/div[1]/div[1]/div/div[2]/input').send_keys('w3school')
    driver.find_element_by_xpath('//*[@id="tsf"]/div[2]/div[1]/div[3]/center/input[1]').send_keys(Keys.ENTER)

google()
Lumpfish answered 17/11, 2019 at 13:13 Comment(3)
Does this answer your question? Python selenium keep browser openSpinous
Can't reproduce. Is that the exact code? do you have driver.close()/driver.quit() somewhere?Limpet
Are you waiting for the last action to complete (and then presumably asserting on a condition)?Cyclograph
B
12

Try the experimental options provided in the webdriver like so:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)

driver = webdriver.Chrome(options=options, executable_path="path/to/executable")

Caveat: This does leave chrome tabs open and detached, which you will have to manually close afterwards

Bicorn answered 17/8, 2021 at 5:4 Comment(0)
E
3

you must open a browser instance outside the function so it will remain open after executing the codes inside the function

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://www.google.com")    

def google():
    driver.find_element_by_xpath('//*[@id="tsf"]/div[2]/div[1]/div[1]/div/div[2]/input').send_keys('w3school')
    driver.find_element_by_xpath('//*[@id="tsf"]/div[2]/div[1]/div[3]/center/input[1]').send_keys(Keys.ENTER)

google()
Earthaearthborn answered 20/3, 2022 at 21:11 Comment(0)
I
1

Because "driver" variable is local variable in your function, so when the function have done, the "driver" variable will be remove and then the browser close automatically.

=> Solution is you set "driver" is global variable in your program.

Interpretation answered 19/8, 2022 at 9:2 Comment(0)
P
0

I use the chromedriver to open chrome.

  1. download the chrome driver based on your chrome version.
  2. unzip the chrome driver to the below path in your C drive.
  3. Then use the below code to open the chrome webpage.
chromepath = 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe'
driver = webdriver.Chrome(executable_path=chromepath)
Pessimism answered 17/3, 2023 at 1:22 Comment(0)
G
0

in my case the version of chrome driver needed to be replaced by currenct version of my chrome.

Giule answered 17/5, 2023 at 12:59 Comment(0)
H
0

Using Edge, i didn't find the option "executable_path" to follow the steps Prithu Srinivas gave, but using the code below, the window stoped closing automatically:

options = webdriver.EdgeOptions()
options.add_experimental_option("detach", True)

browser = webdriver.Edge(options=options, keep_alive=True)

The same goes if i don't use the argument "keep_alive=True".

Handhold answered 30/12, 2023 at 16:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.