Selenium with Python Tutorial: How to Get Current URL with Python
Tags: testing, tutorial, python, testdev
Published: 2022-12-29 14:12:14

If you use Selenium WebDriver, you probably know that there are many methods to perform specific actions or interact with elements on a web page. The Selenium Python module gives you the methods you need to be able to automate many tasks when working with a [web browser online](https://www.lambdatest.com/web-browser-online?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog).

As a tester, you may need to know the URL of the page displayed by your Selenium-controlled web browser. This can be useful to keep track of the URL from which you have extracted certain data so that you can update this data in the automation test script. You may also need to know the current URL while making an assertion to check a keyword in the page title.

You can easily get the current URL of a web page when performing [Python automation testing](https://www.lambdatest.com/python-automation-testing?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog) — to do that, you just need to access the **current_url **method of the [Selenium WebDriver](https://www.lambdatest.com/blog/selenium-webdriver-tutorial-with-examples/?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog) object.

In this [Selenium Python tutorial](https://www.lambdatest.com/learning-hub/python-tutorial?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog), I will show you how to get current URL in Selenium Python using Pytest. If you are new to Pytest, you can go through this [Selenium Pytest tutorial](https://www.lambdatest.com/learning-hub/selenium-pytest-tutorial?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog) on [getting started with Pytest](https://www.lambdatest.com/blog/selenium-python-pytest-testing-tutorial/?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog).

Let’s get started.

***Run [Appium](https://www.lambdatest.com/appium-mobile-testing?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=webpage) mobile testing of native and web apps. Improve your app quality with instant access to real devices on LambdaTest. Register now for free!***

## What is Pytest?

Pytest is one of the most popular [Python test automation frameworks](https://www.lambdatest.com/blog/top-python-frameworks-for-automation/?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog) (primarily used for unit testing). It’s a highly configurable and extensible testing framework that comes with a rich set of features to help you write better code for testing in Python.

Pytest is described as a scalable framework with less boilerplate code than other testing frameworks. [Pytest fixtures](https://www.lambdatest.com/blog/end-to-end-tutorial-for-pytest-fixtures-with-examples/?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog) provide context for tests by passing parameter names in test cases; its parameterization eliminates duplicate code for testing multiple sets of input and output, and its rewritten assert statements provide detailed output for causes of failure.

{% youtube UzkuOACmBpA %}

## How to set the test environment in Selenium using Pytest?

In this section of this article on how to get current URL in Selenium Python, we will learn to set up a test environment.

## Installation of Pipenv

Before we start, we will set up a test environment. For this, please follow the steps below:

 1- We will install Pipenv to manage our dependencies better. Pipenv is a dependency manager for Python projects. If you’re familiar with Node.js’ npm or Ruby’s bundler, it is similar in spirit to those tools.

The following command is to install pipenv on macOS. On the official pipenv website, you can see how to install it on other operating systems.

 

```
   brew install pipenv
```

2- Once the installation is complete, enter the command:

```
    pipenv
```

The result should look like this:

![](https://cdn-images-1.medium.com/max/2000/0*BAyUuCKvAseUAjgy.png)

There are several other alternatives to Pipenv for package management, which can also be used like pyenv, poetry, virtualenv, autoenv, etc. However, adding them is beyond the scope of this blog.

3- Creation of **Pipfile** in an empty directory. This file is essential for using Pipenv. It’s used to track your project’s dependencies if you need to reinstall them.

```
    [[source]]
    name = "pypi"
    url = "https://pypi.org/simple"
    verify_ssl = true
    [packages]
    pytest = "*"
    selenium = "*"
    [requires]
    python_version = "*"
```

The **python_version** parameter is the version of the base interpreter you specified when creating a new pipenv environment.

The **packages** section is the place where you can list the packages required for your project.

“*” is for installing the latest stable versions of the packages. At the time of writing this blog on how to get current URL in Selenium Python, the latest versions of Pytest and Selenium are Pytest 7.1.2 and 4.2.2, respectively.

4- In your terminal, go to the directory and install the latest stable versions of the Pytest and Selenium packages with the command:

```
    pipenv install
```

![](https://cdn-images-1.medium.com/max/2000/0*Wx-F6r7HkJD3UUMx.png)

5- Download the latest WebDriver for the browser you wish to use. A WebDriver is a remote control interface that enables introspection and control of user agents. It provides a platform- and language-neutral wire protocol for out-of-process programs to instruct the behavior of web browsers remotely.

The WebDriver Wire Protocol (aka “WebDriver protocol”) is an open standard that allows the software to interact with a browser as if a human was interacting with the browser.

Here are the download links for the most commonly used browsers.

* Firefox: GeckoDriver

* Chrome: ChromeDriver

* Opera: OperaDriver

* Edge: Microsoft Edge WebDriver

Make sure you have the correct version for your browser, otherwise it won’t work. However, you do not need a WebDriver, in case you are running your test on the [cloud Selenium Grid](https://www.lambdatest.com/selenium-automation?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog).

The Selenium test configuration in the cloud is managed by a cloud-based Selenium Grid, such as LambdaTest, which will help you run Selenium tests for your web application to ensure [cross browser compatibility](https://www.lambdatest.com/learning-hub/cross-browser-compatibility?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog).

Cloud-based [cross browser testing](https://www.lambdatest.com/?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog) platforms like LambdaTest allow you to perform [automation testing](https://www.lambdatest.com/automation-testing?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog) on an [online browser farm](https://www.lambdatest.com/online-browser-farm?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog) of over 3000+ browsers and operating systems.

Now, let’s add some code to demonstrate how to get current URL in Selenium Python from our Selenium-controlled browser.

***[Emulator online](https://www.lambdatest.com/mobile-emulator-online?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=webpage)  from LambdaTest allows you to seamlessly test your mobile applications, websites, and web apps on mobile browsers and mobile devices.***

## How to get current URL in Selenium Python using Pytest?

Now that our project is ready, it is time to add our first test scenario. Here I’ve created:

* “**test_ecommerce_playground.py**” for scenario 1

* “**test_selenium_playground.py**” for scenario 2.

This is what the structure of our project should look like:

![](https://cdn-images-1.medium.com/max/2000/0*MJeQlTIXX2x-DrBq.png)

In Python projects, it is conventional to create a “**tests**” directory under the project root directory to hold all the test cases. In this module, we simply add functions for our test cases.

![](https://cdn-images-1.medium.com/max/2000/0*hIRDQSH3eXBMcyEN.png)

We’ll write page object stubs for both pages and then use them to write our test case function. In our project, I’ve created a directory called **pages** right here.

![](https://cdn-images-1.medium.com/max/2000/0*OWjW_s38k-sJ1nDr.png)

Notice that this directory is outside of the **tests **directory.

* We also need it outside because we want this **pages** directory to be a Python package.

* Python packages are denoted by this **__init__.py** file (pronounced “dunder init”). It’s just an empty file inside a directory that says, “Hey, this is a Python package, and you can treat it as such, specifically for import statements.”

![](https://cdn-images-1.medium.com/max/2000/0*ZpQitWhf8lu0QECh.png)

Let’s add a **config.json** file to our project. It should be located at the root of our project. The **config.json** is our configuration file. It allows us to define some settings or preference parameters that will be applied to our project.

It contains a browser, which I have set to “Firefox,” and an implicit timeout. We have hard-coded an implicit timeout of 10 seconds for all interactions. The best way for our tests to read this configuration file will be to use a pytest fixture. You will see this later in our **conftest.py** file

***Which are the most wanted [automated testing tools](https://www.lambdatest.com/blog/automation-testing-tools/?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog) that have climbed the top of the ladder so far? Let’s take a look.***

## Scenario 1:

Step 1: Navigate to ‘[https://ecommerce-playground.lambdatest.io/](https://ecommerce-playground.lambdatest.io/?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog)‘
Step 2: Search with the keyword “iPhone”.
Step 3: Display the current URL of the page in the console.
Step 4: Check if the search keyword is in the current URL of the page.
Step 5: Save the current URL of the page in a urls.txt file.

Let’s look at the code.

**Implementation**:

**ecommerce_playground.py**

```
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    class EcommercePlaygroundPage:
       #url
       url = 'https://ecommerce-playground.lambdatest.io/'
       #locators
       search_form = (By.NAME, "search")
       search_button = (By.CSS_SELECTOR, ".type-text")
       # Initializer
       def __init__(self, browser):
           self.browser = browser
     
       # Interaction methods
       def load(self):
           self.browser.get(self.url)
           self.browser.implicitly_wait(30)
     
       def search(self , keyword):
           search_form = self.browser.find_element(*self.search_form)
           search_form.send_keys(keyword)
           search_button = self.browser.find_element(*self.search_button)
           search_button.click()
```

**Code Walkthrough:**

 

```
   from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
```

The **selenium.webdriver** module provides all the WebDriver implementations. Currently supported WebDriver implementations are Firefox, Chrome, Microsoft Edge, IE and Remote.

* The **By** class is used to locate elements within a document.

* The **Keys** class provides methods through which you can access keys like RETURN, F1, ALT, ENTER, and more. Please read through our earlier blog on [handling keyboard actions in Selenium](https://www.lambdatest.com/blog/handling-keyboard-actions-in-selenium-webdriver/?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog) to learn more about it.

**test_ecommerce_playground.py**

 

```
   import pytest
    from pages.ecommerce_playground import EcommercePlaygroundPage
    keyword = "iPhone"
    def test_ecommerce_playground(browser):
       ecommerce_page = EcommercePlaygroundPage(browser)
       ecommerce_page.load()
       ecommerce_page.search(keyword)
       get_url = browser.current_url
       title = browser.title
       print("The current url is: "+str(get_url))
       assert "iPhone" in get_url
       with open('urls.txt', 'a') as f:
           f.write(title + " : "+ get_url)
           f.write('\n')
```

**Execution Output:**

Shown below is the page URL:

![](https://cdn-images-1.medium.com/max/2800/0*O2wHb2Xdpk5sVGoR.png)

urls.txt content is below:

```
    Search - iPhone : https://ecommerce-playground.lambdatest.io/index.php?route=product%2Fsearch&search=iPhone
```

***Through this usability testing tutorial, you will learn how [usability testing](https://www.lambdatest.com/learning-hub/usability-testing?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=learning_hub) is a great way to discover unexpected bugs, find what is unnecessary or unused before going any further, and have unbiased opinions from an outsider.***

## Scenario 2:

Step1: Navigate to ‘[https://www.lambdatest.com/selenium-playground/](https://www.lambdatest.com/selenium-playground/?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog)‘.
Step 2: Click Simple Form Demo.
Step 3: Display the current URL of the page in the console.
Step 4: Save the current URL of the page to a urls.txt file.

**Implementation:**

**FileName — selenium_playground.py**

```
    from selenium.webdriver.common.by import By
    class SeleniumPlaygroundPage:
       #url
       url = 'https://www.lambdatest.com/selenium-playground/'
       #locators
       simple_form_demo = (By.XPATH, "//a[normalize-space()='Simple Form Demo']")
       # Initializer
       def __init__(self, browser):
           self.browser = browser
       # Interaction methods
       def load(self):
           self.browser.get(self.url)
       def simple_form(self):
           simple_form = self.browser.find_element(*self.simple_form_demo)
           simple_form.click()

```
**test_selenium_playground.py**

```
    import pytest
    from pages.selenium_playground import SeleniumPlaygroundPage
    def test_selenium_playground(browser):
       playground_page = SeleniumPlaygroundPage(browser)
       playground_page.load()
       playground_page.simple_form()
       # Get the current url of the page in a variable
       get_url = browser.current_url
       title = browser.title
       # Display the url in the console
       print("The current url is: "+str(get_url))
       # Save the url in a urls.txt file
       with open('urls.txt', 'a') as f:
           f.write(title + " : "+ get_url)
           f.write('\n')
```

**Code Walkthrough:**

Automation uses locators to find elements on a page. [Selenium locators](https://www.lambdatest.com/learning-hub/selenium-locators?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog) are simple query strings for finding elements. They will return all elements that match their query.

Selenium WebDriver supports many types of locators — ID, Name, Class Name, CSS Selectors, XPath, Link Text, Partial Link Text, and Tag Name.

Now let’s look at how to get locators for the target items we need for our test scenario.

![](https://cdn-images-1.medium.com/max/2800/0*LgiIUhaJJg9qAN1a.png)

Here I have used the Inspect Element to store the [XPath](https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog) of the **Simple Form Demo** link in a variable named **simple_form_demo**.

```
       simple_form_demo = (By.XPATH, "//a[normalize-space()='Simple Form Demo']")
```

We will use “**find_element**“, which is called directly from the WebDriver instance, which we have named **browser** in our code.

```
       simple_form = self.browser.find_element(*self.simple_form_demo)
```

To locate the Simple Form Demo element, we’ll need to say **browser.find_element** and we’ll pass in our simple_form_demo locator.

Now you may be wondering what this asterisk is? Well, the [find_element method in Selenium](https://www.lambdatest.com/blog/findelement-and-findelements-in-selenium/?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog) takes two arguments — the locator type and then the query — but **simple_form_demo** is a tuple.

By adding this here, I can pass my tuple, my locator, into the **find_element** method. If it’s successful, it will return an object representing the **simple_form** element on the page.

```
       simple_form.click()
```

Now, we will use the calls to the element objects returned by the **find_element** methods. **click()** allows you to click on an element. Once I have this element, I can click on it to go to the page it redirects to.

 

```
      # Get the current url of the page in a variable
       get_url = browser.current_url
```

**current_url** returns the URL of the page currently loaded in the browser. We used this WebDriver Call to store the Current URL of the page in a variable called **get_url**.

```
       # Save the url in a urls.txt file
       with open('urls.txt', 'a') as f:
           f.write(title + " : "+ get_url)
           f.write('\n')

```
We then saved the contents of this variable in a **urls.txt** file.

**Execution Output**:

![](https://cdn-images-1.medium.com/max/2800/0*aAIU0V3KYU37R7tq.png)

After clicking on Simple Form Demo, you are redirected to this page:

![](https://cdn-images-1.medium.com/max/2800/0*CzCLVQjIKX0rtwOX.png)

Content of **urls.txt**:

Selenium Grid Online | Run Selenium Test On Cloud :
https://www.lambdatest.com/selenium-playground/simple-form-demo

***This smoke testing tutorial covers what [smoke testing](https://www.lambdatest.com/learning-hub/smoke-testing?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=learning_hub) is, its importance, benefits, and how to perform it with real-time examples.***

## How to get current URL in Selenium Python on the cloud Grid?

Cloud Selenium Grid provides us with an option to run tests on a range of [virtual browsers](https://www.lambdatest.com/intl/en-in/virtual-browsers?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog), browser versions, and operating systems securely over the cloud. LambdaTest is one such platform that provides a secure, scalable, and super reliable cloud-based Selenium Grid infrastructure that lets you run tests at scale.

LambdaTest is a cloud-based [cross browser testing](https://www.lambdatest.com/?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog) platform that allows you to run tests on an [online device farm](https://www.lambdatest.com/online-device-farm?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog) of 3000+ real devices running on real operating systems for mobile, desktop, and tablets. LambdaTest Selenium Grid also allows you to perform [parallel testing](https://www.lambdatest.com/blog/what-is-parallel-testing-and-why-to-adopt-it/?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog).

{% youtube WZlsHlReRww %}

You can also Subscribe to the [LambdaTest YouTube Channel](https://www.youtube.com/channel/UCCymWVaTozpEng_ep0mdUyw?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog) and stay updated with the latest tutorials around [Selenium testing](https://www.lambdatest.com/selenium-automation?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog), [Cypress E2E testing](https://www.lambdatest.com/cypress-e2e-testing?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog), CI/CD, and more.

To use LambdaTest Selenium Grid with Pytest to perform [Python web automation](https://www.lambdatest.com/python-web-automation?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog), you will need a LambdaTest account. Here are steps you can follow:

 1- Navigate to the [LambdaTest website](https://www.lambdatest.com/?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog) and log in if you already have an account, or create one if you don’t have an account yet.

![](https://cdn-images-1.medium.com/max/2000/0*93Gf6MgUZdTkxyzg.png)

2- Once you are logged in, navigate to the automation page by clicking **Automation** on the left sidebar of your screen.

![](https://cdn-images-1.medium.com/max/2000/0*0Jt-GOEsKT37W7z_.png)

3- To use Pytest with LambdaTest Grid, you need LambdaTest credentials (i.e. username and access key). To get the credentials, click the “**Access Key**” button on the right side of your screen.

![](https://cdn-images-1.medium.com/max/2000/0*psX9j7mAg6dqz0Oo.png)

4- Let us now save the credentials (username and access key) to environment variables LT_USERNAME and LT_ACCESS_KEY.

We will create an .env file in which we will save our credentials.

```
    nano .env
```

Then Paste

```
    LT_USERNAME=YOUR_USERNAME
    LT_ACCESS_KEY=YOUR_ACCESS_KEY
```

An .env file should appear in your project once you save the changes.

![](https://cdn-images-1.medium.com/max/2000/0*p6XYyGXTHOsvSxjq.png)

**Implementation:**

You must now update your **conftest.py** file as follows:

```
    import json
    import pytest
    import selenium.webdriver
    from os import environ
    from selenium import webdriver
    from selenium.common.exceptions import WebDriverException
    from selenium.webdriver.remote.remote_connection import RemoteConnection
    @pytest.fixture
    def config(scope='session'):
     
     # Read the file
     with open('config.json') as config_file:
       config = json.load(config_file)
      # Assert values are acceptable
     assert config['browser'] in ['Firefox', 'Chrome', 'Headless Chrome']
     assert isinstance(config['implicit_wait'], int)
     assert config['implicit_wait'] > 0
     
     # Return config so it can be used
     return config
    @pytest.fixture
    def browser(config):
     # Initialize the WebDriver instance
     if config['browser'] == 'Firefox':
       b = selenium.webdriver.Firefox()
     elif config['browser'] == 'Chrome':
       b = selenium.webdriver.Chrome()
     elif config['browser'] == 'Headless Chrome':
       opts = selenium.webdriver.ChromeOptions()
       opts.add_argument('headless')
       b = selenium.webdriver.Chrome(options=opts)
     else:
       raise Exception(f'Browser "{config["browser"]}" is not supported')
     
     # Make its calls wait for elements to appear
     b.implicitly_wait(config['implicit_wait'])
     
     # Return the WebDriver instance for the setup
     yield b
     
     # Quit the WebDriver instance for the cleanup
     b.quit()
    @pytest.fixture(scope='function')
    def browser(request):
       desired_caps = {}
       browser = {
           'LT:Options' : {
             "user" : environ.get('LT_USERNAME', None),
             "accessKey" : environ.get('LT_ACCESS_KEY', None),
             "build" : "lamdatest_build",
             "name" : "lamdatest_demo",
             "platformName" : "Windows 11",
           },
           "browserName" : "Firefox",
           "browserVersion" : "99.0",
       }
       desired_caps.update(browser)
       test_name = request.node.name
       build = environ.get('BUILD', "Sample PY Build")
       username = browser.get('LT:Options', {}).get('user')
       access_key = browser.get('LT:Options', {}).get('accessKey')
       selenium_endpoint = "@hub.lambdatest.com/wd/hub">http://{}:{}@hub.lambdatest.com/wd/hub".format(username, access_key)
     
       executor = RemoteConnection(selenium_endpoint, resolve_ip=False)
       browser = webdriver.Remote(
           command_executor=executor,
           desired_capabilities=desired_caps
       )
       yield browser
       def fin():
           #browser.execute_script("lambda-status=".format(str(not request.node.rep_call.failed if "passed" else "failed").lower()))
           if request.node.rep_call.failed:
               browser.execute_script("lambda-status=failed")
           else:
               browser.execute_script("lambda-status=passed")
               browser.quit()
       request.addfinalizer(fin)
     
    @pytest.hookimpl(tryfirst=True, hookwrapper=True)
    def pytest_runtest_makereport(item, call):
       # this sets the result as a test attribute for LambdaTest reporting.
       # execute all other hooks to obtain the report object
       outcome = yield
       rep = outcome.get_result()
       # set an report attribute for each phase of a call, which can
       # be "setup", "call", "teardown"
       setattr(item, "rep_" + rep.when, rep)

```
You can use the [Selenium Desired Capabilities Generator](https://www.lambdatest.com/capabilities-generator/?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog) from LambdaTest to automatically generate the capability class needed to run your Selenium automation test scripts on LambdaTest’s Selenium Grid. The Selenium Capabilities Generator will provide you with the complete capability class code based on your mouse-interactions from the user interface.

In our project we use [Selenium 4](https://www.lambdatest.com/learning-hub/selenium-4?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog), so this is what the declaration of the Desired Selenium Capabilities class looks like.

```
    desired_caps = {}
       browser = {
           'LT:Options' : {
             "user" : environ.get('LT_USERNAME', None),
             "accessKey" : environ.get('LT_ACCESS_KEY', None),
             "build" : "lamdatest_build",
             "name" : "lamdatest_demo",
             "platformName" : "Windows 11",
           },
           "browserName" : "Firefox",
           "browserVersion" : "99.0",
       }
       desired_caps.update(browser)

```
As we have saved our credentials in an environment file, we made this import to be able to use them in our **conftest.py** file

```
    from os import environ

```
Once the tests have run successfully, log in to [LambdaTest Automation Dashboard](https://automation.lambdatest.com/build?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog) to check the status of the test execution.

![](https://cdn-images-1.medium.com/max/2000/0*hNcm-97bfvaFp7PY.png)

![](https://cdn-images-1.medium.com/max/2000/0*hf30qwSYonUZ59Cg.png)

If you are looking to develop advanced, hands-on expertise in [Selenium Python testing](https://www.lambdatest.com/selenium-python-testing?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=blog) and take your career to the next level, you can take the Selenium Python 101 certification from LambdaTest.

Here’s a short glimpse of the Selenium Python 101 certification from LambdaTest:

{% youtube KeSWHW4Lx1E %}

***In this Data driven testing tutorial, let us deep dive into what data driven testing is, its pros & cons, its types, [data driven testing](https://www.lambdatest.com/learning-hub/data-driven-testing?utm_source=devto&utm_medium=organic&utm_campaign=dec29_pk&utm_term=pk&utm_content=learning_hub) in an agile environment, benefits, and their best practices.***

## Conclusion


In this Selenium Python tutorial, we have seen how to get current URL in Selenium Python using the Pytest framework. We started by setting up the test environment in Selenium using Pytest and learned core methods to get the current URL in Selenium using Python. It is generally employed in cases where you require an intermediate URL from a redirect chain or need to do a series of navigations among different URLs. Finally, we saw the code execution on cloud Selenium Grid like LambdaTest.

I hope you enjoyed reading this article on how to get current URL in Selenium Python.