pythonselenium-webdriver

Browser gets automatically closed without using quit or close method


Code is :

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.google.com")
print(driver.title)
print(driver.current_url)

How to hold the screen so that it doesn't goes off. I am new to this and this is my first program.


Solution

  • You can use sleep to hold the screen for a given/fixed amount of time.

    from time import sleep
    
    # sleep for 5 seconds
    sleep(5)
    # sleep for 10 seconds
    sleep(10) 
    

    Otherwise, you can also use implicit and explicit waiting strategies in case you want to hold/wait until certain web elements load. https://www.selenium.dev/documentation/webdriver/waits/

    using sleep:

    from time import sleep
    from selenium.webdriver import Chrome
    driver = Chrome()
    driver.get("https://www.google.com")
    print(driver.title)
    print(driver.current_url)
    sleep(10)