Once all functions have been completed, Selenium will autonomously shut down the browser

Having recently started programming, I have a question about Selenium. I am confused as to why the browsers opened by Selenium close at the end of the code.

from lib2to3.pgen2 import driver

from selenium import webdriver

def Online_PLatform():
   
    Driver = webdriver.Chrome()

    Driver.get('https://elearningmarikina.ph/')

    Gmail = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[1]/input')

    Gmail.send_keys('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a39a969bd1c6c0d7cacde3c7c6d3c6c7cec2d1cac8cacdc28dc0ccce">[email protected]</a>')

    Pass = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[2]/input')

    Pass.send_keys('33112')

    Button = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[3]/button')

    Button.click()

    

Answer №1

If you want to keep your driver open, there are two different methods you can use:
1.
First, add the 'detach' option to your driver settings:

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)

Alternatively, you can simply add a delay at the end of your test code as a less elegant but simpler approach.

from lib2to3.pgen2 import driver

from selenium import webdriver
import time

def Online_PLatform():
   
    Driver = webdriver.Chrome()

    Driver.get('https://elearningmarikina.ph/')

    Gmail = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[1]/input')

    Gmail.send_keys('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7d4448450f181e0914133d19180d1819101c0f141614131c531e1210">[email protected]</a>')

    Pass = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[2]/input')

    Pass.send_keys('33112')

    Button = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[3]/button')

    Button.click()

    time.sleep(50)

Answer №2

Once all functions have been executed, the code halts and this causes selenium to terminate.

If you want to introduce a delay, you can make use of the time module.

import time
from lib2to3.pgen2 import driver

from selenium import webdriver

def Online_PLatform():
   
    Driver = webdriver.Chrome()

    Driver.get('https://elearningmarikina.ph/')

    Gmail = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[1]/input')

    Gmail.send_keys('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6fff3feb4a3a5b2afa886a2a3b6a3a2aba7b4afadafa8a7e8a5a9ab">[email protected]</a>')

    Pass = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[2]/input')

    Pass.send_keys('33112')

    Button = Driver.find_element_by_xpath('/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[3]/button')

    Button.click()
    time.sleep(50) #---> 50 second delay

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What sets Robot Resource Files apart from Robot Test Suites in Robot Framework?

Currently, I am delving into the world of Robot framework using Python. To streamline my development process, I have seamlessly integrated it with my Eclipse IDE through the RED plugin available in the marketplace. However, upon creating my robot framework ...

Selenium C# failing to identify newly opened browser window

I am currently experiencing some synchronization issues in my code. While executing a process, I click a button that opens a new window. To switch to the new window, I am using the following code snippet: _webdriver.SwitchTo().Window(_webdriver.WindowHand ...

Press the "show more" button multiple times to display all information in the table and retrieve all data from the table

I am trying to extract all the data from a table on the following page: However, I keep clicking the "show more" button but the table remains limited to showing only 30 rows. import sys import time from pyvirtualdisplay import Display from selenium impor ...

Effective strategies for organizing Selenium RC tests into modular components

Currently, I am challenged with re-factoring my Selenium RC test scripts written in Visual Studio using C#. All my tests are currently located within a single file, and I am seeking advice, recommendations, or resources on how to modularize these tests eff ...

What are some strategies for bypassing a Proxy server in Selenium WebDriver?

My system is set up with a proxy server, but when I use selenium web driver to open a URL, the browser launches without passing the URL in the address bar. How can I work around this issue with the proxy server setup? I have attempted the following code ...

Tips on personalizing the BROWSERSTACK_BUILD_NAME within Jenkins

Currently, I am in the process of incorporating BrowserStack into my Selenium Python framework and utilizing Jenkins for running tests. However, I am encountering difficulties when trying to personalize the build name on the BrowserStack dashboard. ...

python 3 selenium is encountering a ConnectionRefusedError with the error code [WinError 10061]

I'm encountering an issue with my Python program that utilizes Selenium to visit a website and retrieve the value of an element. I have a list of dictionaries, each containing a url parameter. The program iterates through the list, visiting the URL fr ...

Utilizing Python logging filters

I implemented a logging filter to handle my error messages and it involves setting an environment variable. However, once I integrate the filter with my logger, the error messages cease to get printed on the terminal or written to the logging file. The fi ...

Tips for locating the span element using XPath in the Google Chrome Developer Tools:

I'm attempting to locate the following HTML code: <span data-login="true" class="button-tab-links--gray hide-for-medium-only"> Olá, Visitante</span> using //span[@class="button-tab-links--gray hide-for-medium-only"] in Google Chrome t ...

Obtaining a list of child span elements using xpath in Selenium

I am looking to retrieve all child span elements based on the following xpath //label[@for='someText']/span Expected result: <span class="someClass">"Find this text"</span> <span id="someId">" ...

Avoid using a dollar sign in a Selenium xpath locator

I need help finding an element that has the attribute $9a, but I am encountering issues because of the dollar sign. Here is the XPath expression I tried to use: //td[@id='isc_6T']//span[@$9a='browse'] However, I received an exception ...

Retrieve the names of the features for the top-performing estimator found in the Gridsearch

After implementing GridsearchCV with a Ridge model and utilizing PolynomialFeatures in the preprocessing pipeline, I have successfully trained the model. To access the coefficients of the best model, I used the following code snippet: pipe = Pipeline( ...

Is there a way to perform a bulk apply or replace operation on multiple columns in pandas?

The following code snippet is functional: df['Forecast'] = df['Forecast'].apply(lambda x: '0' if x == '' else x) df['Yield'] = df['Yield'].apply(lambda x: '0' if x == '' else ...

Automate page scrolling using Python and Selenium

Here's the issue at hand: I am currently using selenium to extract all successful projects from a specific webpage (""). Despite my efforts, the URL remains constant even after clicking on different buttons. I am particularly interested in successful ...

Collecting information from an HTML table with the use of xpath and either LXML or selenium

Looking to scrape data from an HTML table on a specific webpage: Visit this link I am utilizing Python, selenium, and lxml with xpath for this task. The challenge lies in extracting match odds that are spread across two rows: first in tr followed by tr ...

{% How to use something in Django: A comprehensive tutorial %}

The Django tutorial (part 3) includes the template syntax displayed below: {% if latest_poll_list %} <ul> {% for poll in latest_poll_list %} <li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li> {% endf ...

Exploring how to display JSON objects containing spaces in an HTML table

Sorry if this question has been asked already, but I can't find the answer. I'm brand new to html/java/django and struggling with a seemingly simple issue. I am developing a web application that retrieves json data from firebase using python/pyre ...

Are you familiar with textbox interactions?

I have been utilizing Kantu to automate the process of completing various forms. One particular textbox is designed in such a way that when an individual's ID number is input and you click into another field or tab out of the box, it automatically loa ...

Analyzing two dataframes with Python to identify differences

Having two datasets with matching columns, I aim to compare their values by aligning them based on the first column. Despite using various join methods, including outer, left, and inner joins, the results are similar and accurate. However, I only wish to ...

How does list[::] differ from just list?

I am trying to understand why I can't use the following code to rotate a matrix 90 degrees clockwise: matrix = zip(*matrix[::-1]) Instead, I found that this code works: class Solution: def rotate(self, matrix): """ :type matrix: ...