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()
3
  • 3
    Does this answer your question? Python selenium keep browser open Commented Nov 17, 2019 at 13:15
  • 1
    Can't reproduce. Is that the exact code? do you have driver.close()/driver.quit() somewhere? Commented Nov 17, 2019 at 13:17
  • Are you waiting for the last action to complete (and then presumably asserting on a condition)? Commented Nov 17, 2019 at 18:24

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

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)

Comments

0

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

Comments

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".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.