{"id":13751,"date":"2024-02-15T01:44:18","date_gmt":"2024-02-14T20:14:18","guid":{"rendered":"https:\/\/techbeamers.com\/?p=13751"},"modified":"2025-11-30T11:25:37","modified_gmt":"2025-11-30T16:25:37","slug":"selenium-4-with-python-guide","status":"publish","type":"post","link":"https:\/\/techbeamers.com\/selenium-4-with-python-guide\/","title":{"rendered":"Python Selenium 4 Automation Guide"},"content":{"rendered":"\n<p>Selenium 4 has been around now for quite a bit of time. It came out with many new features, some you can directly use while some will benefit you in the background. This version was released quite a while ago, but not many resources are available telling how to use it with Python. That&#8217;s where this tutorial will help you by providing a step-by-step guide to set up Selenium 4 with Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-get-started-with-selenium-4-using-python\">Get Started with Selenium 4 Using Python<\/h2>\n\n\n\n<p>Python always gives a seamless experience when used with Selenium. With this new release, the integration is going to get better. Let&#8217;s quickly learn what&#8217;s new inside Selenium 4.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-what-s-new-in-selenium-4\">What&#8217;s New in Selenium 4<\/h3>\n\n\n\n<p>Let&#8217;s first quickly go through the <a href=\"https:\/\/techbeamers.com\/selenium-version-4-features-whats-new\/\">list of Selenium 4 features<\/a>:<\/p>\n\n\n<div style=\"--icon-code: &quot;\\e951&quot;; --icon-color: #00D084; --dark-icon-color: #FFFFFF; \" class=\"list-style-element is-icon wp-block-foxiz-elements-list-style\">\n\n<ul class=\"wp-block-list\">\n<li>From an automation testing point of view, the most useful feature is the support for <a href=\"https:\/\/techbeamers.com\/selenium-4-relative-locators\/\">relative locators in Selenium 4<\/a>. One of the main advantages of this is to ensure that your scripts don&#8217;t fail because of a duplicate element. With the help of these relative locators, you can differentiate it from a potential duplicate element. Moreover, you can use this feature to get more reliable test results.<\/li>\n\n\n\n<li>With the new version, you can download and install the correct web driver with one line of code. Though, doing it is not as easy as it sounds. \ud83d\ude42<\/li>\n\n\n\n<li><a href=\"https:\/\/techbeamers.com\/selenium-grid-webdriver-code-example-java\/\">Selenium Grid<\/a> is more capable now. It can itself register and re-register nodes and has self-healing ability. It can choose a new leader if the main hub fails. The communication between its components is now over HTTPS and it can enforce authentication and authorization for controlling access to resources.<\/li>\n\n\n\n<li>The new Selenium now uses built-in Chrome&#8217;s headless renderer which speeds up test execution. It shows web pages exactly as they should.<\/li>\n\n\n\n<li>Earlier Selenium was using the JSON Wire Protocol, now it has become W3C compliant. For you, it means: You can write tests once and run them everywhere. You can run the same tests in more browsers as your scripts are compatible. Moreover, it ensures the tests are future-ready as any new browser is more likely to use the W3C rules.<\/li>\n<\/ul>\n\n<\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-set-up-selenium-4-with-python-on-ubuntu-22-04\">Set up Selenium 4 with Python on Ubuntu 22.04<\/h3>\n\n\n\n<p>Here&#8217;s the step-by-step guide on setting up Selenium 4 on Ubuntu 22.04. The following are the details of the target OS, we will use to set up Selenium:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cat \/etc\/os-release | grep -E '^(NAME|VERSION_ID|VERSION|ID|CODENAME|UBUNTU_CODENAME)'\n\nAME=\"Ubuntu\"\nVERSION_ID=\"22.04\"\nVERSION=\"22.04.3 LTS (Jammy Jellyfish)\"\nVERSION_CODENAME=jammy\nID=ubuntu\nID_LIKE=debian\nUBUNTU_CODENAME=jammy<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-step1-sync-system-packages\">Step1. Sync System Packages<\/h4>\n\n\n\n<p>Firstly, open the console and take this safe step to sync your installed packages:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo apt update; apt upgrade<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-step2-get-the-helper-tools\">Step2. Get the Helper Tools<\/h4>\n\n\n\n<p>Install the helper tools we&#8217;ll need during the setup.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo apt install unzip wget<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-step3-download-chromedriver\">Step3. Download ChromeDriver<\/h4>\n\n\n\n<p>Since we&#8217;ll run our tests in the Chrome browser, we need to get the compatible version of its web driver. For this, let&#8217;s first check the version of Chrome running on our system:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ google-chrome --version \nGoogle Chrome 121.0.6167.160<\/code><\/pre>\n\n\n\n<p>Now we know the version of our browser. The next thing for you is to get a compatible web driver version. There are two ways to do it. The first one is to follow the below steps to download it manually.<\/p>\n\n\n<div style=\"--icon-code: &quot;\\e984&quot;; --icon-color: #00D084; --dark-icon-color: #FFFFFF; \" class=\"list-style-element is-icon wp-block-foxiz-elements-list-style\">\n\n<ul class=\"wp-block-list\">\n<li>Goto this download page: <a href=\"https:\/\/googlechromelabs.github.io\/chrome-for-testing\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/googlechromelabs.github.io\/chrome-for-testing\/\u00a0<\/a><\/li>\n\n\n\n<li>Choose the compatible version for your Chrome browser.<\/li>\n\n\n\n<li>Download the&nbsp;<code>.zip<\/code>&nbsp;file for Linux.<\/li>\n<\/ul>\n\n<\/div>\n\n\n<p>Another way to install the package is by using the pip command. Check the steps below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ pip install chromedriver-py # Install the default version\n$ pip install chromedriver-py==121.0.6167.85 # Install a specific version<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"948\" height=\"138\" src=\"https:\/\/techbeamers.com\/wp-content\/uploads\/2024\/02\/installing-chrome-driver-using-pip.png\" alt=\"To set up Selenium with Python, install chrome driver using pip\" class=\"wp-image-13754\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-step4-extract-chromedriver\">Step4. Extract ChromeDriver<\/h4>\n\n\n\n<p>If you downloaded the web driver file via the link, unzip it and copy its content following the below instructions.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo unzip chromedriver_linux64.zip -d \/opt\/chromedriver<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-step5-set-env-vars\">Step5. Set Env Vars<\/h4>\n\n\n\n<p>Add the path to the chrome driver dir to your system env variables:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo 'export PATH=\"\/opt\/chromedriver:$PATH\"' &gt;&gt; ~\/.bashrc\nsource ~\/.bashrc<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-step6-install-selenium-libraries\">Step6. Install Selenium Libraries<\/h4>\n\n\n\n<p><a href=\"https:\/\/techbeamers.com\/python-pip-usage\/\">Use <code>pip<\/code> to install<\/a> the required Selenium libraries:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ pip install selenium webdriver-manager<\/code><\/pre>\n\n\n\n<p>After this check the installation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ pip list | grep  selenium\nselenium                 4.17.2<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-step7-verify-selenium-version\">Step7. Verify Selenium Version<\/h4>\n\n\n\n<p>Open a Python interpreter and try importing the libraries:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import selenium\nprint(selenium.__version__)<\/code><\/pre>\n\n\n\n<p>Save the file as &#8220;check_version.y&#8221;. This should print the installed Selenium version.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python3 check_version.py \n4.17.2<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-test-with-a-simple-selenium-python-script\">Test with a Simple Selenium Python Script<\/h3>\n\n\n\n<p>Let&#8217;s create a sample Selenium 4  Python script to test your setup:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from selenium import webdriver as wd\n\ndr = wd.Chrome()\ndr.get(\"https:\/\/www.google.com\")\ndr.quit()<\/code><\/pre>\n\n\n\n<p>Save the script as basic.py and run it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python basic.py<\/code><\/pre>\n\n\n\n<p>If all goes well, your browser should open and switch to Google.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-demo-script-to-search-string-in-google\">Demo Script to Search String in Google<\/h3>\n\n\n\n<p>Now, let&#8217;s do a short exercise to get familiar with Selenium 4 and Python. Prepare a demo script for this purpose. It should open Google search and find a keyword for us.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from selenium import webdriver as dr\nfrom selenium.webdriver.common.by import By\nfrom selenium.webdriver.support.ui import WebDriverWait as wt\nfrom selenium.webdriver.support import expected_conditions as EC\nfrom chromedriver_py import binary_path as bin\n\nsvc = dr.ChromeService(executable_path=bin)\nwd = dr.Chrome(service=svc)\n\n# Replace with your desired URL\nurl = \"https:\/\/www.google.com\/\"\n\n# Navigate to the URL\nwd.get(url)\n\n# Find the search bar element using its name attr\nsearch_bar = wt(wd, 10).until(\n    EC.visibility_of_element_located((By.NAME, \"q\"))\n)\n\n# Type \"Selenium test\" into the search bar\nsearch_bar.send_keys(\"Selenium test\")\n\n# Find the search button using its name attribute\nsearch_btn = wt(wd, 10).until(\n    EC.element_to_be_clickable((By.NAME, \"btnK\"))\n)\n\n# Click the search button\nsearch_btn.click()\n\n# Optional: Wait for a specific element on the next page if needed\n# For example, wait for the search results to load\n# wt(wd, 10).until(EC.presence_of_element_located((By.ID, \"search-results\")))\n\n# Print the page title\ntitle = wd.title\nprint(f\"Page title: {title}\")\n\n# Close the browser window\nwd.quit()<\/code><\/pre>\n\n\n\n<p>Save the above Selenium 4 Python script as gsearch.py. Run it using the following command. It should search for &#8220;Selenium test&#8221; and print the page title of the search results page.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python3 gsearch.py\nPage title: Selenium test - Google Search<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-python-script-to-demonstrate-selenium-4-relative-locators\">Python Script to Demonstrate Selenium 4 Relative Locators<\/h3>\n\n\n\n<p>As we know Selenium 4 added new relative locators like above, below, to_right_of, to_left_of, and near, so let&#8217;s get to know how to use them with Python.<\/p>\n\n\n\n<p>This example is partially booking a flight from the Google flight page. It finds and fills certain fields on the web page using the relative locators.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from selenium import webdriver as wd\nfrom selenium.webdriver.common.keys import Keys\nfrom selenium.webdriver.common.by import By\nfrom selenium.webdriver.support.relative_locator import *\nfrom time import sleep\n\n# Replace with your desired departure and arrival airports\nsrc_city = \"Lucknow\"\ndstn_city = \"Australia\"\n\n# Initiate Chrome session\ndr = wd.Chrome()\n\n# Navigate to Google Flights\ndr.get(\"https:\/\/www.google.com\/travel\/flights\")\n\n# Wait for page to load (adjust timeout if needed)\ndr.implicitly_wait(10)\n\nelem_label = dr.find_element(By.XPATH, \"\/\/div&#91;text()='Flights']\")\nprint(\"elem_label = \", elem_label)\nprint()\n\n# Find search form field below \"Flight\" label\nsrc = dr.find_element(\n    with_tag_name(\"input\").below(elem_label)\n)\nprint(\"src= \", src)\nprint()\n\ndstn = dr.find_element(\n    with_tag_name(\"input\").to_right_of(src)\n)\nprint(\"dstn= \", dstn)\nprint()\n\n# Enter departure and arrival airports\nsrc.clear()\nsrc.send_keys(src_city)\nsleep(1)\n\nele1 = dr.find_element(with_tag_name(\"div\").below(src))\nprint(\"ele1= \", ele1)\nprint()\n\nele1.click()\n\ndstn.clear()\ndstn.send_keys(dstn_city)\nsleep(1)\n\nele2 = dr.find_element(with_tag_name(\"div\").below(dstn))\nprint(\"ele2= \", ele2)\nprint()\n\nele2.click()\n\n# ... (continue with your booking process)\n\nsleep(5)\ndr.quit()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-additional-notes\">Additional Notes<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If you set up the Chrome driver manually, then you need to specify its path in your script.&nbsp;However, you won&#8217;t need it if you have updated the <code>bashrc<\/code> file with the driver path.<\/li>\n\n\n\n<li>You can use an alternative driver like GeckoDriver (for Firefox) by following similar steps and downloading the respective driver.<\/li>\n\n\n\n<li>To set up multiple versions of Selenium, use a tool &nbsp;<code>virtualenv<\/code>&nbsp;to create project-specific Python environments and dependencies.<\/li>\n<\/ul>\n\n\n\n<p><strong>Ethical Considerations<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Be aware of and respect website terms of service when using automation tools.<\/li>\n\n\n\n<li>Avoid actions that might overload servers or violate ethical guidelines.<\/li>\n<\/ul>\n\n\n\n<p>We hope this becomes your go-to guide to set up Selenium 4 on your Ubuntu system. By the way, don&#8217;t leave without attempting the following quiz.<\/p>\n\n\n\n<div class=\"wp-block-foxiz-elements-affiliate-product gb-wrap af-product yes-shadow\" style=\"--rating-size:20px;--border-style:dotted;--border-width:0 0 0 0;--bg:#8ed1fc;--desktop-padding:30px 30px 30px 30px;--tablet-padding:25px 25px 25px 25px;--mobile-padding:20px 20px 20px 20px\"><a href=\"https:\/\/techbeamers.com\/selenium-python-quiz\/\" class=\"af-link\"><\/a><div class=\"af-inner\"><div class=\"af-content\"><h3 class=\"af-heading gb-heading\">Selenium Python Quiz for Automation<\/h3><p class=\"af-description gb-description\">Do a quick self-assessment of your Selenium Python knowledge.<\/p><\/div><div class=\"af-cta-wrap\"><span class=\"af-price h3\">25 Questions<\/span><a href=\"https:\/\/techbeamers.com\/selenium-python-quiz\/\" class=\"af-button is-btn\">Attempt<\/a><\/div><\/div><\/div>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button has-custom-font-size has-medium-font-size\"><a class=\"wp-block-button__link has-midnight-gradient-background has-background wp-element-button\" href=\"https:\/\/techbeamers.com\/selenium-python-test-suite-unittest\/\" style=\"border-radius:30px\">Click to Build a Selenium Python Test Suite<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Selenium 4 has been around now for quite a bit of time. It came out with many new features, some you can directly use while some will benefit you in the background. This version was released quite a while ago, but not many resources are available telling how to use it with Python. That&#8217;s where [&hellip;]<\/p>\n","protected":false},"author":321,"featured_media":13756,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_post_was_ever_published":false},"categories":[92],"tags":[7471,7009,6957,6949],"class_list":{"0":"post-13751","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python-programming-tutorials","8":"tag-python-selenium-tutorial","9":"tag-book-a-flight-using-selenium-webdriver-and-testng","10":"tag-selenium-example-code","11":"tag-run-selenium-tests-in-linux"},"jetpack_featured_media_url":"https:\/\/techbeamers.com\/wp-content\/uploads\/2024\/02\/Selenium-4-With-Python-Guide.png","_links":{"self":[{"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/posts\/13751","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/comments?post=13751"}],"version-history":[{"count":1,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/posts\/13751\/revisions"}],"predecessor-version":[{"id":23626,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/posts\/13751\/revisions\/23626"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/media\/13756"}],"wp:attachment":[{"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/media?parent=13751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/categories?post=13751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techbeamers.com\/wp-json\/wp\/v2\/tags?post=13751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}