<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: jainishpatel78</title>
    <description>The latest articles on Forem by jainishpatel78 (@jainishpatel78).</description>
    <link>https://forem.com/jainishpatel78</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1235482%2F7b5c6829-11d7-4b9c-ac88-f7f41b298356.png</url>
      <title>Forem: jainishpatel78</title>
      <link>https://forem.com/jainishpatel78</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jainishpatel78"/>
    <language>en</language>
    <item>
      <title>Use Cases of XPath Python In Selenium With Examples</title>
      <dc:creator>jainishpatel78</dc:creator>
      <pubDate>Mon, 13 May 2024 12:06:16 +0000</pubDate>
      <link>https://forem.com/testmuai/use-cases-of-xpath-python-in-selenium-with-examples-lec</link>
      <guid>https://forem.com/testmuai/use-cases-of-xpath-python-in-selenium-with-examples-lec</guid>
      <description>&lt;p&gt;Despite having a variety of programming languages to choose from, like JavaScript, Java, Typescript, C#, Ruby, etc., Python is considered the easiest to learn due to its easy-to-learn and understandable syntax. &lt;/p&gt;

&lt;p&gt;It has various use cases, web apps using Flask and Django, desktop apps using Tkinter, and libraries like Scikit-learn and TensorFlow for implementing AI and ML models. Testing is also a domain where Python has a stronghold with support from various testing frameworks like Selenium, &lt;a href="https://www.lambdatest.com/appium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Appium&lt;/a&gt;, etc. &lt;/p&gt;

&lt;p&gt;During testing, locating specific elements plays pivotal to recognize DOM manipulation. This manipulation allows interaction with the web elements within the user interface. This is where Python **XPath **comes into play, offering a powerful toolset for traversing the &lt;a href="https://www.lambdatest.com/blog/document-object-model/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Document Object Model&lt;/a&gt; and pinpointing elements efficiently.&lt;/p&gt;

&lt;p&gt;In this article, we will cover everything, starting from what XPath Python is, how to install and use XPath Python, extraction of text from HTML, types of XPath, Using Python with various functions and mathematical operators, and some common advanced techniques.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Need to &lt;a href="https://www.lambdatest.com/free-online-tools/extract-text-from-html?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;extract text from HTML&lt;/a&gt; code? Our free online tool makes it easy and fast. Save time by extracting only the text you need from HTML code in no time.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What is XPath and Why is it used with Selenium?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Automation testing&lt;/a&gt; is a viable substitute for &lt;a href="https://www.lambdatest.com/learning-hub/manual-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;manual testing&lt;/a&gt; due to the expanding variety of browser types like Chrome, Firefox, Safari, Microsoft Edge, etc., and their multiple versions. &lt;a href="https://www.lambdatest.com/learning-hub/webdriver?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Selenium WebDriver&lt;/a&gt; is an open-source application programming interface (API) used to automate the testing of web applications. &lt;/p&gt;

&lt;p&gt;In Selenium, &lt;a href="https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;XPath&lt;/a&gt; uses an XML path to go through a web page’s HTML code. You can refer to the following video on our &lt;a href="https://www.youtube.com/@LambdaTest?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=youtube" rel="noopener noreferrer"&gt;YouTube Channel&lt;/a&gt; for an in-depth tutorial on Selenium WebDriver.&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/HpL6EX2kjq4"&gt;
&lt;/iframe&gt;
&lt;br&gt;
XPath refers to locating components and attributes inside an HTML / XML document. Similar to how file systems utilize paths to explore directories and files, XPath uses path expressions to choose specific nodes or groups of nodes within an HTML document. XPath is recommended when other &lt;a href="https://www.lambdatest.com/learning-hub/css-selectors?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=youtube" rel="noopener noreferrer"&gt;CSS locators&lt;/a&gt; (ID, Class, etc.) are absent from an XML/HTML page.&lt;/p&gt;

&lt;p&gt;Choosing the right element locator is a critical aspect of effective test automation. It’s not just about functionality; it’s about efficiency and accuracy. &lt;/p&gt;

&lt;p&gt;To understand the preferences of the testing community, we conducted a social media poll with the question, “&lt;strong&gt;What is your favorite element locator in test automation?&lt;/strong&gt;” The responses offer valuable insights into the most favored locator strategies.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmjnhzucy5beu3wf2njql.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmjnhzucy5beu3wf2njql.png" width="776" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/feed/update/urn:li:activity:7125005859511795714/" rel="noopener noreferrer"&gt;Source&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A considerable percentage of participants prefer XPath as their primary element locator due to its versatility and capability, making it a prominent selection for complex web elements.&lt;/p&gt;

&lt;p&gt;XPath can be used for various purposes apart from Automation testing. Different use cases of it will be covered in the upcoming sections. First, let’s learn how to use Python XPath with Selenium and then with other libraries like &lt;strong&gt;&lt;em&gt;requests&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.lambdatest.com/free-online-tools/extract-text-from-json?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;Extract text from JSON&lt;/a&gt; data with our powerful and easy-to-use online free tool. Get the data you need quickly and efficiently, no technical skills required.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  How to Install and Setup Python XPath
&lt;/h2&gt;

&lt;p&gt;To use Python XPath with &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt;, you are only required to install and run Selenium correctly, and to use Xpath with other libraries, you need the lxml library.&lt;/p&gt;

&lt;h2&gt;
  
  
  Install Selenium
&lt;/h2&gt;

&lt;p&gt;1.Install Python 3 using the installer, which can be downloaded from the official website; follow the instructions and set up Python for your system(A prerequisite).&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Now open the command prompt or terminal and run the following command:&lt;/p&gt;

&lt;p&gt;python --version&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frkz1jijlzyopmm86xvut.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frkz1jijlzyopmm86xvut.png" width="800" height="85"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3.Now open your preferred IDE or editor and set up the *venv *(Virtual Environment) if you are using various versions of Python. This article uses VS Code.&lt;/p&gt;

&lt;p&gt;4.Open a VS Code Terminal and run the following command to install Selenium. The version of Selenium used in this article is 4.10.0. Check our blog on &lt;a href="https://www.lambdatest.com/blog/selenium-with-python/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium 4&lt;/a&gt; to know more.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install selenium
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ArZ81-mnoiQSAFKZ_.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ArZ81-mnoiQSAFKZ_.png" width="800" height="415"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Get plain text from XML documents. Simply copy and paste your XML data to extract text using our online free &lt;a href="https://www.lambdatest.com/free-online-tools/extract-text-from-xml?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;Extract Text from XML&lt;/a&gt; tool. Give it a try now!&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Install lxml Library
&lt;/h2&gt;

&lt;p&gt;A robust and effective Python library for handling XML and HTML documents is the lxml. It offers a Python interface to the C-written libxml2 and libxslt libraries. With the aid of lxml, you may traverse and modify the structures of XML and HTML documents as well as create new ones.&lt;/p&gt;

&lt;p&gt;To install the lxml library run the following command:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install lxml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2696%2F0%2Ar5AfB5m53DF89cj4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2696%2F0%2Ar5AfB5m53DF89cj4.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Verifying the Selenium Installation
&lt;/h2&gt;

&lt;p&gt;By running the following script you can make sure that everything is working fine. The below code will open the LambdaTest E-commerce playground, a demo website and get its title (Your Store) printed.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver

driver = webdriver.Chrome()

driver.get("https://ecommerce-playground.lambdatest.io/")

# Print the title of the website 
print(driver.title)
driver.close()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ARPmjdzGjiZoidA2F.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ARPmjdzGjiZoidA2F.png" width="800" height="75"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding an Element Using Python XPath in Selenium
&lt;/h2&gt;

&lt;p&gt;Now that we are done with the installation part, let’s find an element using XPath Python in Selenium.&lt;/p&gt;

&lt;p&gt;To find the location of a specific element in a document, you need to obtain the XPath of that element. For that, follow the steps:&lt;/p&gt;

&lt;p&gt;1.Inspect the &lt;a href="https://www.lambdatest.com/selenium-playground/table-pagination-demo?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;website&lt;/a&gt; by &lt;strong&gt;&lt;em&gt;right-clicking &amp;gt; Inspect&lt;/em&gt;&lt;/strong&gt; or (&lt;em&gt;ctrl + Shift + C or cmd + Option + C&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;2.Select the element to see the HTML code. Now right-click on the code and Go to the copy option where you can find the XPath of that element.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Ajsw18CIcM9tBIz5-.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Ajsw18CIcM9tBIz5-.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;XPath will look something like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//*[@id=”table-id”]/tbody/tr[1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Am-NCoebN3gd-ifeO.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Am-NCoebN3gd-ifeO.png" width="800" height="302"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Strip HTML is a free online tool that strips out tags and other formatting from text. Simply paste any text into the tool and click on &lt;a href="https://www.lambdatest.com/free-online-tools/strip-html?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;Strip HTML&lt;/a&gt; to remove unnecessary formatting.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Using Python XPath to Locate Elements
&lt;/h2&gt;

&lt;p&gt;To locate an element, the &lt;em&gt;find_element()&lt;/em&gt; method is used. It returns the first matching element in the HTML document. To illustrate this, we will find the element by the XPath which we copied.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")

row_data = driver.find_element(By.XPATH,'//*[@id="table-id"]/tbody/tr[1]')
print("Row 1: ", row_data.text)
driver.close()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2124%2F0%2A5gT5OarEVBCqJTyl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2124%2F0%2A5gT5OarEVBCqJTyl.png" width="800" height="64"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the XPath &lt;em&gt;//&lt;/em&gt;[&lt;a class="mentioned-user" href="https://dev.to/id"&gt;@id&lt;/a&gt;=”table-id”]/tbody/tr[1]&lt;em&gt;, **tr[1]&lt;/em&gt;* represents the first row of the table; However, If you change the XPath to &lt;strong&gt;&lt;em&gt;//&lt;/em&gt;[&lt;a class="mentioned-user" href="https://dev.to/id"&gt;@id&lt;/a&gt;=”table-id”]/tbody/tr&lt;/strong&gt;* still the output remains the same because the &lt;em&gt;find_element()&lt;/em&gt; method will only get the first appearance of the XPath from the HTML Document.&lt;/p&gt;

&lt;p&gt;To get all the elements, you don’t have to mention each; rather, use the &lt;em&gt;find_elements()&lt;/em&gt; method to get all the elements with the same XPath.&lt;/p&gt;

&lt;p&gt;This is how you can &lt;em&gt;use find_elements()&lt;/em&gt; in the code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
from selenium.webdriver.common.by import By

driver = driver = webdriver.Chrome()

driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")

rows = driver.find_elements(By.XPATH,'//*[@id="table-id"]/tbody/tr')
for row in rows:
    print(row.text)
driver.close()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Following XPath is used in the code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AwtIQodWCayqQawq1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AwtIQodWCayqQawq1.png" width="800" height="323"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2104%2F0%2ALNXJ6JxY-EYlBiTg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2104%2F0%2ALNXJ6JxY-EYlBiTg.png" width="800" height="217"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.lambdatest.com/free-online-tools/json-validator?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;JSON Validator&lt;/a&gt; is a free and easy-to-use tool to validate JSON data, which makes it easier to fix the issue by providing error line and character position.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Retrieving attribute values
&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;get_attribute()&lt;/em&gt; method is used to retrieve an element’s attributes, such as the *href *attribute from an anchor tag. This function will initially attempt to return the value of the specified property. It returns the value of the corresponding attribute if the requested property doesn’t exist. *None *is returned if there isn’t an attribute with that name. You can check out the list of attributes that can be fetched.&lt;/p&gt;

&lt;p&gt;Code on how to use &lt;em&gt;get_attribute()&lt;/em&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;table = driver.find_element(By.XPATH,'//*[@id="table-id"]')
print(table.get_attribute("class"))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Following XPath is used in the code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Avcurrf-uabo4QCqH.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Avcurrf-uabo4QCqH.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AaWgUvrb35zUKkt5z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AaWgUvrb35zUKkt5z.png" width="800" height="71"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.lambdatest.com/free-online-tools/html-validator?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;HTML Validator&lt;/a&gt; is an online tool that checks HTML syntax and quality. Improve your website’s performance and functionality with ease.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Extracting Text Content and Manipulating Using Python
&lt;/h2&gt;

&lt;p&gt;You can extract text from the element using the text property. Apart from this, you can use various methods of Python like &lt;em&gt;split(), sort(), reverse(), replace(),&lt;/em&gt; and many more.&lt;/p&gt;

&lt;p&gt;The code below uses the &lt;em&gt;split()&lt;/em&gt; method to organize the data.&lt;br&gt;
This is how you can use it in the code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rows = driver.find_elements(By.XPATH,'//*[@id="table-id"]/tbody/tr')
data = []
for row in rows:
    row_data = row.text.split(" ")
    if len(row_data) &amp;gt; 1:
        data.append(row_data)

for data_row in data:
    print(data_row)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Following XPath is used in the code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AH3BmqjTqYOyPl3Q1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AH3BmqjTqYOyPl3Q1.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2564%2F0%2AtInIS93Kt6vJ4umn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2564%2F0%2AtInIS93Kt6vJ4umn.png" width="800" height="143"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling multiple matches and filtering results
&lt;/h2&gt;

&lt;p&gt;You can filter the results by adding more precise conditions using XPath. For instance, the following XPath phrase may be used to discover all div elements that belong to a particular class:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;divs = driver.find_elements(By.XPATH,'//*[@id="header"]/nav/div/div/div[2]/div/div/div[1]/div')

for div in divs:
    print(div.text)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;XPath using in the code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2720%2F0%2AoTs_4G85wfyq7Utq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2720%2F0%2AoTs_4G85wfyq7Utq.png" width="800" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A1YovUh5GawCJfKR1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A1YovUh5GawCJfKR1.png" width="800" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To verify the fetched data is correct, you can check by finding the element. To do so, follow this step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Open Developer Tools using the Inspect option.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the &lt;strong&gt;Elements&lt;/strong&gt; panel, press &lt;em&gt;Ctrl+F&lt;/em&gt; or &lt;em&gt;cmd+F&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add the XPath in the Find Box and you will get the number of appearings.&lt;/p&gt;

&lt;p&gt;//*[&lt;a class="mentioned-user" href="https://dev.to/id"&gt;@id&lt;/a&gt;="header"]/nav/div/div/div[2]/div/div/div[1]/div&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Refer to the below image for more understanding.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ApzJ67heXz0EDKl_N.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ApzJ67heXz0EDKl_N.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Minify your JS code without changing its functionality with our easy-to-use &lt;a href="https://www.lambdatest.com/free-online-tools/js-minify?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;JS Minifier&lt;/a&gt; that reduces the size of your scripts and improve website speed.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Additionally, you may traverse and filter elements based on how they relate to other items using XPath axes and predicates. For instance, the following XPath phrase can be used to locate every element included within the button’s particular div element:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;buttons = driver.find_elements(By.XPATH,'//*[@id="header"]/nav/div/div/div[2]/div/div/div[1]/div/button')
for button in buttons:
print(button.text)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;XPath using in the code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2992%2F0%2AIruYfxxmR0bv-zBK.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2992%2F0%2AIruYfxxmR0bv-zBK.png" width="800" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AXtWKVMJhO8Se5poB.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AXtWKVMJhO8Se5poB.png" width="800" height="128"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;These examples show how to use XPath in Selenium with Python at its most basic level. You can modify the XPath expressions based on the structure and properties of the HTML or XML document you are working with.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of XPath in Selenium
&lt;/h2&gt;

&lt;p&gt;In XPath, there are two methods for locating an element: absolute and relative. The XPath node, which can either be the starting point of the path to the node where the element is situated or a point relative to some other point along the DOM, defines the paths.&lt;/p&gt;

&lt;h2&gt;
  
  
  Absolute XPath
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Absolute XPath begins at the document’s root node and descends through the element hierarchy until it reaches the target element.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It is less dependable if the page’s structure changes because it uses the entire path starting with the root node.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A single forward slash (/) marks the beginning of an absolute XPath expression.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To get the Absolute XPath &lt;strong&gt;&lt;em&gt;copy full Xpath&lt;/em&gt;&lt;/strong&gt; from the **copy **option.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/html/body/div[1]/div/section[2]/div/div/div/table
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2528%2F0%2AP1cK7z3AZIpDtYQH.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2528%2F0%2AP1cK7z3AZIpDtYQH.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Minimize the file size of your CSS files for faster loading web pages with our free online &lt;a href="https://www.lambdatest.com/free-online-tools/css-minify?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;CSS Minify&lt;/a&gt; tool. Easy and efficient minification in just a few clicks.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Relative XPath
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Relative XPath employs a reference point to find the target element after starting from any node in the document.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It is more flexible and advised because it considers the element’s position relative to its parent or sibling components.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Expressions in XPath that are relative start with a double forward slash (//).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To get the Absolute XPath to &lt;strong&gt;&lt;em&gt;copy Xpath&lt;/em&gt;&lt;/strong&gt; from the **copy **option.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//*[@id=”table-id”]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ATBfze5ar9jsaD5sT.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ATBfze5ar9jsaD5sT.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Relative XPath expressions are commonly used in Selenium due to their flexibility and robustness. They allow you to locate elements based on their attributes, text content, or their relationship with other elements. Additionally, you can leverage various XPath axes, functions, and predicates to create more specific and powerful expressions for element locating.&lt;/p&gt;

&lt;h2&gt;
  
  
  Breakdown of XPath Expressions
&lt;/h2&gt;

&lt;p&gt;XPath is made of various symbols. Each one has its significance and can be used in different ways. The following table will explain each symbol in detail:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2ATXRtM2DuHOwrKO2RTEUPVw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2ATXRtM2DuHOwrKO2RTEUPVw.png" width="800" height="395"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;XPath Example Syntax:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A1PINuXk19ZVfzCO4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A1PINuXk19ZVfzCO4.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Python XPath with Functions, Mathematical and logical Operators
&lt;/h2&gt;

&lt;p&gt;Using functions, mathematical operators, and logical operators with Python XPath, you may build strong and adaptable expressions to pick out particular web page items. In this section, let’s look at some examples of how to combine these features:&lt;/p&gt;

&lt;h2&gt;
  
  
  Functions in Python XPath
&lt;/h2&gt;

&lt;p&gt;Various functions in XPath Python can be used for Strings, Numbers, Nodes, and Boolean. In this section, we will look at various functions for each of them.&lt;/p&gt;

&lt;h3&gt;
  
  
  String Functions
&lt;/h3&gt;

&lt;p&gt;You can carry out a variety of operations on components using XPath functions, such as extracting attributes and counting items. The following are a few often-used functions: &lt;em&gt;text(), contains(), starts-with(), concat(),&lt;/em&gt; etc. Let’s look at each function in detail.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;text():&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This function helps to extract text from an HTML/XML element. It can be used when you want the test from tags like a, p, h1 to h6, and many more. In the example below, the main h1 tag is fetched, and then the text is extracted from it.&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")

table_title = driver.find_element(By.XPATH,'//*[@id="__next"]/div/section[1]/div/div/h1')
print('Title: ',table_title.text)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Make your XML code easy to read and understand with our free online &lt;a href="https://www.lambdatest.com/free-online-tools/xml-prettify?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;XML Prettify&lt;/a&gt; tool. Format your code for better readability and save time!&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AKYZ16WicXXo7eZCz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AKYZ16WicXXo7eZCz.png" width="800" height="77"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;contains():&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This function checks whether an element contains a specific substring or not. You can compare the substring in various ways like text, ClassNames, IDs, and many more. In the below example, find the string “Roland” in the td(cell) of an HTML document.&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")

table_cell = driver.find_element(By.XPATH,'//tr/td[contains(text(), "Roland")]')
print(table_cell.text)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AMfDaBFcEpIBTUULr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AMfDaBFcEpIBTUULr.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;General Format for &lt;em&gt;contains():&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2684%2F0%2AJh_SkoQwVd7v_UZW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2684%2F0%2AJh_SkoQwVd7v_UZW.png" width="800" height="202"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;starts-with():&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This function checks whether an element’s text starts with a specific substring. There are various ways to compare the substring, like text, ClassNames, IDs, and many more. In the below example, find the string that starts with “R” in the td(cell) of an HTML document.&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")

table_cell = driver.find_element(By.XPATH,'//tr/td[contains(text(), "Roland")]')
print(table_cell.text)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AITSxcQAW07fYaaSp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AITSxcQAW07fYaaSp.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;General Format for &lt;em&gt;contains():&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2684%2F0%2AlRuq0kiv34p5HDur.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2684%2F0%2AlRuq0kiv34p5HDur.png" width="800" height="202"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Concatenation:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;String concatenation means adding strings together. In the example below, the title and sub_title of the HTML doc are combined.&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")

main_title = driver.find_element(By.XPATH,'//*[@id="__next"]/div/section[1]/div/div/h1').text
sub_title = driver.find_element(By.XPATH,'//*[@id="__next"]/div/section[2]/div/div/div/h2').text
combined_text = main_title + " " + sub_title
print(combined_text) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Ar1dLapsRlm38WSOl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Ar1dLapsRlm38WSOl.png" width="800" height="72"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Convert your XML files to JSON format without any hassle using our reliable and efficient &lt;a href="https://www.lambdatest.com/free-online-tools/xml-to-json-converter?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;XML to JSON converter&lt;/a&gt; tool. No installation or download required.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Numeric functions
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;count(node_expression):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This function counts the number of elements in a specified node. There is no direct function in Python; However, it can be achieved using this technique.&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")

# Select the option with "10" from the drop-down to show maximum 10 rows per page
driver.find_element(By.CSS_SELECTOR, '#maxRows option[value="10"]').click()

rows = driver.find_elements(By.XPATH, '//tr/td')

print(len(rows))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ApL_E9F59OSLw-pQg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ApL_E9F59OSLw-pQg.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;number():&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;XPath number function converting string to a number.&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")

# Select the option with "10" from the drop-down to show maximum 10 rows per page
driver.find_element(By.CSS_SELECTOR, '#maxRows option[value="10"]').click()

rows = driver.find_elements(By.XPATH, '//tr/td[1]')
index_list = []
for row in rows:
    if row.text !='':
        index_list.append(int(row.text))

for index in index_list:
    print(index, type(index))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AkHTc3TnvnbhPuGep.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AkHTc3TnvnbhPuGep.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sum(node_expression):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This function returns the sum of element values in a specified node.&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")

# Select the option with "10" from the drop-down to show maximum 10 rows per page
driver.find_element(By.CSS_SELECTOR, '#maxRows option[value="10"]').click()

rows = driver.find_elements(By.XPATH, '//tr/td[1]')
index_list = []
for row in rows:
    if row.text !='':
        index_list.append(int(row.text))

for index in index_list:
    print(index, type(index))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ATMPWxJ9KIyFDXqgE.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ATMPWxJ9KIyFDXqgE.png" width="800" height="71"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;floor():&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;floor function returns the largest integer round value that is equal to or less than a parameter value. Another way we can say the last round value is the value return of the function. To use floor() in Python, you must import the math library. eg. FLOOR(10.45) returns 10.&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")

# Select the option with "10" from the drop-down to show maximum 10 rows per page
driver.find_element(By.CSS_SELECTOR, '#maxRows option[value="10"]').click()

rows = driver.find_elements(By.XPATH, '//tr/td[1]')
index_list = []
for row in rows:
    if row.text !='':
        index_list.append(int(row.text))

for index in index_list:
    print(index, type(index))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AHtMO84ODaKzBlXRA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AHtMO84ODaKzBlXRA.png" width="800" height="71"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ceiling():&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;XPath ceiling function returns the smallest integer round value that is greater than or equal to a parameter value. Another way we can say the next round value is that value returns of the function. To use &lt;em&gt;ceil()&lt;/em&gt; in Python, you must import the &lt;em&gt;math&lt;/em&gt; library. eg. CEILING(10.45) return 11.&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.get("https://ecommerce-playground.lambdatest.io/index.php?route=product/product&amp;amp;product_id=42")

price = driver.find_element(By.XPATH,'//*[@id="entry_216831"]/div/div/h3').text

price = float(price.replace("$",''))
discount = price - (20/price)*100

print("Discounted Price: ", math.ceil(discount))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href="https://www.lambdatest.com/free-online-tools/json-to-xml-converter?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;JSON to XML Converter&lt;/a&gt; is a simple and fast tool that converts JSON to XML data. Use this tool to convert your JSON documents into valid XML for easy data exchange.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AFgTeFvQWuwReIQj1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AFgTeFvQWuwReIQj1.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;round():&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The XPath round function returns the round number of the specified nth number of decimal places. eg. ROUND(10.45) return 10.&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.get("https://ecommerce-playground.lambdatest.io/index.php?route=product/product&amp;amp;product_id=42")

price = driver.find_element(By.XPATH,'//*[@id="entry_216831"]/div/div/h3').text

price = float(price.replace("$",''))
discount = price - (5/price)*100

print("Discounted Price: ", round(discount,3))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Console Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AxkgPoZMbxmD1EfYd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AxkgPoZMbxmD1EfYd.png" width="800" height="69"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Node Functions
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;node():&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and root nodes. The function returns the &lt;em&gt;node&lt;/em&gt; value.&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")

node = driver.find_element(By.XPATH, "//tr/td[node()]").text
print(node)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A4ZXR8jRH4diWJ5Qb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A4ZXR8jRH4diWJ5Qb.png" width="800" height="66"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;last():&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This function returns the size of the total context in a given context. The name of this function lasts, so it means the function does not return the last node value.&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")

last = driver.find_element(By.XPATH,'//tr/td[last()]').text
print(last)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Our &lt;a href="https://www.lambdatest.com/free-online-tools/adler32-hash-calculator?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;Adler32 Hash Calculator&lt;/a&gt; is the perfect tool to create secure hashes to protect data from unauthorized access. Start creating your hashes now!&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A_p49S5D50rtLqc3h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A_p49S5D50rtLqc3h.png" width="800" height="68"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;position():&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;function returns the position of an element in the set (list) of elements in a given context.&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")

position = driver.find_element(By.XPATH, "//tr/td[position()]").text
print(position)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AGIGveHZsKSNQTnT7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AGIGveHZsKSNQTnT7.png" width="800" height="66"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;id(dtd_id):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;function return nodes based on passed DTD unique ID.&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")

id = driver.find_element(By.XPATH, '//*[@id="table-id"]').text
print(id)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2108%2F0%2A13GbyYBO96Lw92pe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2108%2F0%2A13GbyYBO96Lw92pe.png" width="800" height="197"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;name(node_expression):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;function returns the string name of the last expression (node-set).&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")

id = driver.find_element(By.XPATH, '//*[@id="table-id"]').text
print(id)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AzpuXhxBgHdGFUNyB.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AzpuXhxBgHdGFUNyB.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;comment():&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This function matches the comment node and returns that specific comment node.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;local-name(node_expression):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;function returns the last expression’s local string name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2028%2F0%2A4vWncj6bGW022Dix.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2028%2F0%2A4vWncj6bGW022Dix.png" width="800" height="268"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Need to create secure Gost hashes? Protect your sensitive information with our &lt;a href="https://www.lambdatest.com/free-online-tools/gost-hash-calculator?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;Gost hash calculator&lt;/a&gt;. Get started today and keep your data safe from hackers.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Boolean functions
&lt;/h3&gt;

&lt;p&gt;XPath Boolean functions are used to convert an argument(as a number, string) to a boolean and return either True or False.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;boolean(number|string|node-expression|object):&lt;/strong&gt; returns true if expression is positive.&lt;br&gt;
&lt;strong&gt;true():&lt;/strong&gt; returns true if the passed string is a normal string.&lt;br&gt;
**false(): **returns false if the passed string is not a normal string.&lt;br&gt;
**lang(): **returns true if the context node language is the same as the specified string argument.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mathematical and logical operators
&lt;/h2&gt;

&lt;p&gt;You may execute arithmetic operations and combine several criteria to generate more complicated expressions using XPath’s mathematical and logical operators. Based on a variety of parameters, these operators can be used to navigate and choose particular items.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mathematical Operators
&lt;/h3&gt;

&lt;p&gt;There are various types of mathematical operators like &lt;strong&gt;‘=’, ‘!=’, ‘+’, ‘-‘, ‘*’, ‘/’, ‘&amp;gt;=’, and ‘&amp;lt;=’&lt;/strong&gt;, which are provided by Python and can be used with XPath.&lt;/p&gt;

&lt;p&gt;There are various use cases for using these operators, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Calculating Engagement on a Social Media Post:&lt;/strong&gt; You are creating a feature to show the engagement on a particular post; in this case, you have to get the data and then apply some calculations to get the engagement.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.lambdatest.com/blog/scraping-dynamic-web-pages/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;**Dynamic Web scraping&lt;/a&gt;:** Consider a situation where you want to scrap article data from a blogging site, but the blogs are uploaded daily. In this case, you can scrape the number of web pages available and then run the loop according to it.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s look at some basic operators and how to use them with Python XPath. For the below examples, we are using a demo web application maintained by LambdaTest.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Addition (+) and Subtraction (-)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We are getting the total viewers and reward points to get the product’s engagement.&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.get("https://ecommerce-playground.lambdatest.io/index.php?route=product/product&amp;amp;product_id=42")

viewers = driver.find_element(By.XPATH,'//*[@id="entry_216826"]/ul/li[2]/span[2]').text
reward_points = driver.find_element(By.XPATH,'//*[@id="entry_216826"]/ul/li[3]/span[2]').text

total_points = int(viewers) + int(reward_points)

print("Total rewards: ", total_points)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AEjmVmHSyV4CUq_2v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AEjmVmHSyV4CUq_2v.png" width="800" height="66"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Multiplication (*) and Division (/)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We are getting a discount(10%) on the product by the formula.&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.get("https://ecommerce-playground.lambdatest.io/index.php?route=product/product&amp;amp;product_id=42")

price = driver.find_element(By.XPATH,'//*[@id="entry_216831"]/div/div/h3').text
price = float(price.replace("$",''))
discount = price - (10/price)*100


print("Discounted Price: ", round(discount,2))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AgI9Xo4m0IFUBJnri.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AgI9Xo4m0IFUBJnri.png" width="800" height="76"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Logical Operators
&lt;/h3&gt;

&lt;p&gt;There are three main logical operators we will cover in this section.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;And operator:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Combines two conditions, both of which must be true for the element to be selected. In the below example, there are two conditional checks: &lt;em&gt;First Name = ‘Chelsea’&lt;/em&gt; &lt;strong&gt;and&lt;/strong&gt; &lt;em&gt;email = ‘&lt;a href="mailto:c.harper@randatmail.com"&gt;c.harper@randatmail.com&lt;/a&gt;’&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")

# Select the option with "10" from the drop-down to show maximum 10 rows per page
driver.find_element(By.CSS_SELECTOR, '#maxRows option[value="10"]').click()


rows = driver.find_elements(By.XPATH, '//tr[td[2][contains(text(), "Chelsea")] and td[4][contains(text(), "c.harper@randatmail.com")]]')

# Print the text of each row
for row in rows:
    print(row.text)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AMcBOOXbuMb6jiw9-.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AMcBOOXbuMb6jiw9-.png" width="800" height="70"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Or operator:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Combines two conditions, either of which can be true for the element to be selected. In the below example, there are two conditional checks: &lt;em&gt;First Name = ‘Chelsea’ or email = ‘&lt;a href="mailto:b.grant@randatmail.com"&gt;b.grant@randatmail.com&lt;/a&gt;’&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")

# Select the option with "10" from the drop-down to show maximum 10 rows per page
driver.find_element(By.CSS_SELECTOR, '#maxRows option[value="10"]').click()

rows = driver.find_elements(By.XPATH, '//tr[td[2][contains(text(), "Chelsea")] or td[4][contains(text(), "b.grant@randatmail.com")]]')

# Print the text of each row
for row in rows:
    print(row.text)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AT7yW1HBgcksQakYd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AT7yW1HBgcksQakYd.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Not operator:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Negates a condition, selecting elements that do not meet the specified condition. In the below example, there are two conditional checks: &lt;em&gt;First Name != ‘Chelsea’ and email != ‘&lt;a href="mailto:b.grant@randatmail.com"&gt;b.grant@randatmail.com&lt;/a&gt;’&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.get("https://www.lambdatest.com/selenium-playground/table-pagination-demo")

# Select the option with "10" from the drop-down to show maximum 10 rows per page
driver.find_element(By.CSS_SELECTOR, '#maxRows option[value="10"]').click()

rows = driver.find_elements(By.XPATH, '//tr[not(td[2][contains(text(), "Chelsea")]) and not(td[4][contains(text(), "b.grant@randatmail.com")])]')

# Print the text of each row
for row in rows:
    print(row.text)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2096%2F0%2AgOaA-uuwz4jKcGge.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2096%2F0%2AgOaA-uuwz4jKcGge.png" width="800" height="283"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Custom functions and extensions
&lt;/h2&gt;

&lt;p&gt;XPath also allows you to define custom functions and extensions in some environments. However, the support for custom functions and extensions may vary depending on the XPath implementation and the XML/HTML processor you are using, like Selenium.&lt;/p&gt;

&lt;p&gt;In Python, you can create a custom function that does a certain task or calculation and outputs a result. Create a function, for instance, to determine whether a given string is included in an element’s text.&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def contains_text(driver, element, text):
    try:
        return WebDriverWait(driver, 10).until(
            EC.text_to_be_present_in_element((By.XPATH, element), text)
        )
    except:
        return False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Advanced XPath Techniques
&lt;/h2&gt;

&lt;p&gt;There are various techniques to make XPath more reliable and reusable like Handling dynamic content, XPath- Multiple Attribute, and using variables and conditions which we are going to discuss in this section in detail.&lt;/p&gt;
&lt;h2&gt;
  
  
  Handling dynamic content
&lt;/h2&gt;

&lt;p&gt;The content you’re trying to locate may be dynamic, such as elements with changing IDs or classes. This is a common practice that some websites follow to prevent automating and &lt;a href="https://www.lambdatest.com/blog/web-scraping-with-javascript-and-selenium/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;web scraping&lt;/a&gt;. However, the structure stays the same in terms of hierarchy. By using XPath, you can handle this situation. To manage dynamic material, you can employ a variety of techniques:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;XPath functions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To locate elements with partial attribute values, XPath offers operations like &lt;em&gt;contains(), starts-with(), and ends-with().&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Find a button with a dynamic ID or className
dynamic_id = "xyz-431353"
button = driver.find_element(By.XPATH, f"//button[contains(@id, '{dynamic_id}')]")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Using Axes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use XPath axes such as following-sibling, preceding-sibling, etc., to move to the target element in relation to other already known elements.&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;label_text = "name"
span_element = driver.find_element(By.XPATH, f"//label[text()='{label_text}']/following-sibling::span")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  XPath- Multiple Attribute
&lt;/h2&gt;

&lt;p&gt;By placing the | operator between square brackets [], you may use XPath to pick elements based on some attributes. This enables you to filter elements that fit various criteria simultaneously. To do this, you reference an attribute in the XPath expression by using the @ symbol followed by the attribute name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ARZrHU1AvIuCJs5Jo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ARZrHU1AvIuCJs5Jo.png" width="800" height="133"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Make your data tamper-proof with our &lt;a href="https://www.lambdatest.com/free-online-tools/whirlpool-hash-calculator?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=may_13&amp;amp;utm_term=bh&amp;amp;utm_content=free_online_tools" rel="noopener noreferrer"&gt;Whirlpool Hash Calculator&lt;/a&gt;. Create secure hashes quickly and easily with just a few clicks. Try it out now!&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  Using XPath variables and conditions
&lt;/h2&gt;

&lt;p&gt;XPath supports variables and can be utilized to shorten and reuse your expressions. When utilizing your Python XPath expressions in Selenium, you can pass variables to them.&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium.webdriver.common.by import By

# Define the XPath expression with variables
element_type = "button"
element_text = "Submit"
xpath_expression = f"//{element_type}[text()='{element_text}']"

# Find the element using the XPath expression
element = driver.find_element(By.XPATH, xpath_expression)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You can also use conditions in your XPath expressions using the if statement.&lt;/p&gt;

&lt;p&gt;Code Example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Find elements based on a condition (e.g., containing a specific class)
is_enabled = True
condition = "enabled" if is_enabled else "disabled"
xpath_expression = f"//button[contains(@class, '{condition}')]"
elements = driver.find_elements(By.XPATH, xpath_expression)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Keep in mind that using complicated XPath expressions can make it more difficult to read and maintain your code. When feasible, utilize CSS selectors because they are typically quicker and easier to understand. Use XPath only in circumstances where CSS selectors are insufficient. Additionally, to make locating items in your HTML more dependable and effective, choose to use distinct IDs, classes, or data properties.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In a nutshell, when used with Selenium, XPath is a strong and crucial tool for web scraping and automating online applications. Using an element’s attributes, textual content, or connections to other elements enables developers and testers to find that element in an HTML or XML document. Python XPath offers several methods, tools, and axes for effectively navigating and choosing elements.&lt;/p&gt;

&lt;p&gt;Before using XPath with Selenium, you must install and configure the necessary Python libraries. When everything is set up, you can find DOM items using XPath expressions. &lt;strong&gt;Relative XPath&lt;/strong&gt; allows you to navigate between elements based on their relationships, while &lt;strong&gt;Absolute XPath&lt;/strong&gt; specifies the whole route from the root. XPath is adaptive to changing web page structures because it can handle dynamic material utilizing functions like *contains(), starts-with(), *and more.&lt;/p&gt;

&lt;p&gt;Using XPath, you may extract attribute values and text content to validate and modify data on web pages. You can manage numerous matches and filter outcomes based on particular criteria using XPath. Advanced XPath techniques, such as using variables and conditions and creating custom functions to make XPath statements more dynamic and reusable.&lt;/p&gt;

&lt;p&gt;To sum up, XPath is an excellent option for finding objects on web pages, navigating intricate DOM structures, and effectively handling dynamic material thanks to its variety and potent capabilities. Learning XPath enables developers and testers to create automated scripts that are reliable and maintainable, thereby enhancing the quality and efficiency of web applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions (FAQs)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Does XPath work with HTML in Python?
&lt;/h3&gt;

&lt;p&gt;Yes, Python supports using XPath with HTML. A strong query language used to browse and pick elements from XML or HTML documents is called XPath. It enables you to extract particular information or elements from the text depending on their positioning and characteristics. Using XPath will be fruitful when unique identifiers are not present like ID, or Class.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to check if the XPath element exists in Selenium Python?
&lt;/h3&gt;

&lt;p&gt;The &lt;em&gt;find_elements()/ find_element()&lt;/em&gt; method in Selenium Python can be used to determine whether an element is present based on an XPath expression. A list of web elements that match the provided XPath is returned by the &lt;em&gt;following&lt;/em&gt; method. The element does not exist on the page if the list is empty, which means no elements were detected. By using a simple if else statement or try/catch method you can check for the element.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to find XPath in Selenium Python?
&lt;/h3&gt;

&lt;p&gt;In Selenium Python, determining an element’s XPath is a rather simple process. To find an element’s XPath, you can do so in a number of different ways. Here are three such methods: 1. Using browser developer tools, 2. Using Selenium’s &lt;em&gt;find_elements()/ find_element()&lt;/em&gt;, and 3. Using browser extensions.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>testing</category>
      <category>programming</category>
    </item>
    <item>
      <title>Mastering Web Automation With Python Selenium On Chrome</title>
      <dc:creator>jainishpatel78</dc:creator>
      <pubDate>Thu, 25 Jan 2024 14:54:22 +0000</pubDate>
      <link>https://forem.com/testmuai/mastering-web-automation-with-python-selenium-on-chrome-55ac</link>
      <guid>https://forem.com/testmuai/mastering-web-automation-with-python-selenium-on-chrome-55ac</guid>
      <description>&lt;p&gt;Python is the 4th most preferred programming language according to StackOverflow Survey 2022. It is easy to learn, versatile, and has a large developer community. Python’s open-source nature, high-level syntax, and interpretive capabilities make it an ideal choice.&lt;/p&gt;

&lt;p&gt;With frameworks like Flask and Django for backend development and libraries like &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt; for automation testing, Python offers comprehensive options. As software becomes more complex, manual testing is time-consuming. Python’s support for testing frameworks such as &lt;a href="https://www.lambdatest.com/learning-hub/python-unittest?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Unittest&lt;/a&gt;, &lt;a href="https://www.lambdatest.com/learning-hub/selenium-pytest-tutorial?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;pytest&lt;/a&gt;, Robot, Behave, and Lettuce makes it a popular choice for automation testing.&lt;/p&gt;

&lt;p&gt;In this article, we will go through critical areas to help you run Python Selenium on Chrome for web automation. Firstly, we will start with an installation and setup process for Python and Selenium. Next, we will discuss handling various input types and interacting with warnings, alerts, and pop-ups. At the end of the article, you will be able to create tests for &lt;a href="https://www.lambdatest.com/learning-hub/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;automation testing&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Dive into automation testing using Selenium with Java with this detailed tutorial. &lt;a href="https://www.lambdatest.com/blog/selenium-with-java/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;learn java for automation testing&lt;/a&gt; confidently.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Python Selenium Chrome Driver Setup
&lt;/h2&gt;

&lt;p&gt;Selenium is a library that allows to control web browsers and automate tasks/tests. It provides a way to simulate user interaction with a web page, including filling out forms, clicking buttons/links, navigating through pages/windows, and taking screenshots of the window.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/learning-hub/webdriver?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Selenium WebDriver&lt;/a&gt; uses different WebDrivers, like &lt;strong&gt;&lt;em&gt;ChromeDriver&lt;/em&gt;&lt;/strong&gt; for Chrome, &lt;em&gt;**gecko *&lt;/em&gt;*driver for Firefox, and many others. It gives Selenium WebDriver the ability to control the browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation and setup process of ChromeDriver for different operating systems
&lt;/h2&gt;

&lt;p&gt;There are multiple steps to be taken to install the driver correctly. Let’s look at each step one by one.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check the version of Chrome installed on your device. To check the version, go to the top right corner, select the 3 dots icon -&amp;gt; settings, and then select &lt;strong&gt;About Chrome&lt;/strong&gt; from the left panel.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A1kUIOzWz3RyIyp3M.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A1kUIOzWz3RyIyp3M.png" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Follow this ChromeDriver link to download it on your system.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A3fW_PSXq-QSiN2Dk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A3fW_PSXq-QSiN2Dk.png" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Test your native app and website on real iOS and Android devices hosted on the cloud. LambdaTest is a convenient, cost-effective and centralised solution for running realtime and Automated test on &lt;a href="https://www.lambdatest.com/real-device-cloud?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;device clouds for mobile app testing&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Select the ChromeDriver according to the version of the Chrome browser. After that, we have to choose the operating system on which we desire to run it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AHnL0MqQ8tyJo_qDj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AHnL0MqQ8tyJo_qDj.png" width="800" height="289"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Once the download is complete, extract the &lt;em&gt;chromedriver.exe&lt;/em&gt; by unzipping the folder.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Till this point, the process is the same for all operating systems. Now we will see the setup process for each OS.&lt;/p&gt;

&lt;h3&gt;
  
  
  Windows Operating System
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Copy the path for the file &lt;em&gt;chromedriver.exe such that I am storing the driver in “D:\chromedriver.exe”.&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;2. Right-click&lt;/em&gt; on My Computer and click on &lt;strong&gt;&lt;em&gt;Properties&lt;/em&gt;&lt;/strong&gt; options.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AykEe9_3yj4MLz5ZW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AykEe9_3yj4MLz5ZW.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You will be redirected to a new window that will look similar to the image below; after that, select the &lt;strong&gt;&lt;em&gt;Advanced System Settings&lt;/em&gt;&lt;/strong&gt; option.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AEF-G3mr4ubh32bVq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AEF-G3mr4ubh32bVq.png" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;From the &lt;em&gt;system propertie*s options, select the *&lt;/em&gt;&lt;em&gt;Environment Variables&lt;/em&gt;** button.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A-2F03NNVuF3YGCcL.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A-2F03NNVuF3YGCcL.png" width="404" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Now, from the &lt;em&gt;system variables&lt;/em&gt;, select the path and press the &lt;strong&gt;&lt;em&gt;Edit&lt;/em&gt;&lt;/strong&gt; button.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ADSK6ka94vj6z0C9a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2ADSK6ka94vj6z0C9a.png" width="589" height="651"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Paste the copied path of the file and save the changes by pressing the **OK **button.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A1kBsHyYeBioSdx12.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A1kBsHyYeBioSdx12.png" width="495" height="553"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Test your native app and website on real iOS and Android devices hosted on the cloud. LambdaTest is a convenient, cost-effective and centralised solution for running realtime and &lt;a href="https://www.lambdatest.com/real-device-cloud?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;mobile testing platform with real devices&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  macOS Operating System
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Open the *Finder *and use ⌘+shift+G&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to the following path &lt;em&gt;“/usr/local/bin&lt;/em&gt;”&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add the “chromedriver.exe” file here&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Linux Operating System
&lt;/h3&gt;

&lt;p&gt;If you have already followed the steps to download ChromeDriver, skip steps 1, 2, and 3.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Go to your temp folder:&lt;/p&gt;

&lt;p&gt;cd /tmp/&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Download the latest Linux-based Chromedriver:&lt;/p&gt;

&lt;p&gt;wget &lt;a href="https://chromedriver.storage.googleapis.com/2.37/chromedriver_linux64.zip" rel="noopener noreferrer"&gt;https://chromedriver.storage.googleapis.com/2.37/chromedriver_linux64.zip&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Extract Chromedriver from its archive:&lt;/p&gt;

&lt;p&gt;unzip chromedriver_linux64.zip&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Move Chromedriver to the applications folder:&lt;/p&gt;

&lt;p&gt;sudo mv chromedriver /usr/bin/chromedriver&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you want to explore the way to set the environment variable for the driver using a terminal, follow the documentation by Selenium.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Explore seamless app testing on real mobile devices with our hassle-free solutions. Ensure optimal performance across all &lt;a href="https://www.lambdatest.com/test-on-mobile-devices?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;online device testing&lt;/a&gt; effortlessly.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  How to Check if the Installation is Successful
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Windows OS:&lt;/strong&gt;&lt;br&gt;
Open the command prompt and run the following command:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chromedriver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If the driver is installed correctly, it will open the chromedriver.exe program window.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;macOS or Linux-based OS:&lt;/strong&gt;&lt;br&gt;
Run the following command in the terminal; if installed correctly, it will output the version of the driver.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chromedriver — version or chromedriver -version or chromedriver -v
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Installation of ChromeDriver using webdriver-manager
&lt;/h2&gt;

&lt;p&gt;In the year 2022, Selenium introduces &lt;a href="https://www.lambdatest.com/blog/webdrivermanager-in-selenium/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium Manager&lt;/a&gt;. It is a CLI (Command-Line Interface) program that was created in &lt;em&gt;**Rust *&lt;/em&gt;&lt;em&gt;programming language to support cross-platform execution. When no browser driver is found on the PATH or a third-party driver manager is not utilized, Selenium Manager is transparently triggered by the Selenium bindings. In Python, a module known as *&lt;/em&gt;&lt;em&gt;webdriver-manager&lt;/em&gt;** is used to manage WebDriver binaries for multiple programming languages and browsers. It makes it easier to download and maintain the correct WebDriver executables that are needed for browser automation and to manage WebDriver binaries for browsers like Chrome, Firefox, and Edge, utilize this library.&lt;/p&gt;

&lt;p&gt;You must first install the package before you can use &lt;strong&gt;&lt;em&gt;webdriver-manager&lt;/em&gt;&lt;/strong&gt; in your Python project. The Python package installer pip can be used to accomplish this by using the following command:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install webdriver-manager
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Once the module is installed you can use the &lt;strong&gt;&lt;em&gt;webdriver-manager&lt;/em&gt;&lt;/strong&gt; to initialize the ChromeDriver as shown in the below code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
# 
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.close()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Test your native app and website on real iOS and Android devices hosted on the cloud. LambdaTest is a convenient, cost-effective and centralised solution for running realtime and &lt;a href="https://www.lambdatest.com/real-device-cloud?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;online real device testing&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Opening a Website in Chrome Browser with the help of Python Selenium Script
&lt;/h2&gt;

&lt;p&gt;For this guide, we will use VS Code.&lt;/p&gt;

&lt;p&gt;Before moving forward, the following steps need to be done to run the Python scripts with ease.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a virtual environment
&lt;/h2&gt;

&lt;p&gt;It is not a mandatory step; However, if you want to use different versions of Python on your system for different projects, a virtual environment is a way to do it.&lt;/p&gt;

&lt;p&gt;Let’s discuss how to create an environment using different methods.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using GUI:&lt;/strong&gt;&lt;br&gt;
From VS Code, you can create local environments using virtual environments(venv) or Anaconda(conda) by opening the Command Palette (&lt;em&gt;Windows: Ctrl+Shift+P or macOS: ⇧+⌘+P&lt;/em&gt;), start typing the &lt;strong&gt;&lt;em&gt;Python: Create Environment&lt;/em&gt;&lt;/strong&gt; command to search and then select the command.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2904%2F0%2AnStM6STFRZRgVo1X.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2904%2F0%2AnStM6STFRZRgVo1X.png" width="800" height="116"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you select Venv another menu will open to select the version of Python.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;After that, the environment folder will be created in your workspace, and a notification will display the creation progress in the bottom right corner.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Explore seamless app testing on &lt;a href="https://www.lambdatest.com/test-on-mobile-devices?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;real device testing online free&lt;/a&gt; with our hassle-free solutions. Ensure optimal performance across real-world scenarios effortlessly.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Using Terminal:&lt;/strong&gt;&lt;br&gt;
If you choose to create a virtual environment using this method, use the following command (where .venv is the name of the environment folder):&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Windows
# You can also use `py -3 -m venv .venv`
python -m venv .venv


# macOS/Linux
# You may need to run `sudo apt-get install python3-venv` first on Debian-based OSs
python3 -m venv .venv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Once the virtual environment (.&lt;em&gt;venv&lt;/em&gt;) is created successfully, create a new Python file with the desired name.&lt;/p&gt;

&lt;p&gt;Open a VS Code Terminal and run the following command to install Selenium. The version of Selenium used in this article is *&lt;em&gt;4.9.1 *&lt;/em&gt;. To know more about &lt;a href="https://www.lambdatest.com/blog/selenium-with-python/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Selenium v4&lt;/a&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install selenium
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A03nrWTz9853M4OwE.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A03nrWTz9853M4OwE.png" width="800" height="252"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The required setup is completed. Let’s start with the rollercoaster ride of learning Python Selenium using Chrome.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running Python Selenium using ChromeDriver
&lt;/h2&gt;

&lt;p&gt;To open a website on Chrome Browser, run the block of code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver


driver = webdriver.Chrome()
driver.get("https://google.com")
driver.close()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The import statement is used to get webdriver from the selenium package.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AA9f-I8M_git2CfHI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AA9f-I8M_git2CfHI.png" width="800" height="316"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is used to initialize a new instance of the Chrome WebDriver.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AakSjpxOnKmOhl0GL.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AakSjpxOnKmOhl0GL.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;get():&lt;/em&gt;&lt;/strong&gt; This is a method provided by the WebDriver object, which is used to navigate to a specific URL. It instructs the web browser to open the specified URL.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2324%2F0%2AuVEBG9rftJBv9BSN.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2324%2F0%2AuVEBG9rftJBv9BSN.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;close():&lt;/strong&gt; This method is used to close the current browser window or tab.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2324%2F0%2AB0dOOpA2pVmilO97.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2324%2F0%2AB0dOOpA2pVmilO97.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Test your native app and website on real iOS and Android devices hosted on the cloud. LambdaTest is a convenient, cost-effective and centralised solution for running realtime and Automated &lt;a href="https://www.lambdatest.com/real-device-cloud?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;test on real device&lt;/a&gt; cloud.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Creating Python Selenium Test Cases
&lt;/h2&gt;

&lt;p&gt;In this section, we will see how to control various actions such as navigating to web pages, clicking buttons, and filling out forms which are very commonly used by most websites. For this, we will use amazon.com to run our Selenium scripts.&lt;/p&gt;

&lt;h2&gt;
  
  
  find_element method
&lt;/h2&gt;

&lt;p&gt;To perform various actions, we need to locate the element. A key Selenium feature that enables you to locate web items on a page is the &lt;strong&gt;&lt;em&gt;find_element&lt;/em&gt;&lt;/strong&gt; method. This approach allows you to search for elements using various criteria, including an ID, name, class name, CSS selector, or XPath. During test execution, you can quickly recognize and interact with the desired elements by setting the proper locator strategy.&lt;/p&gt;

&lt;p&gt;Selenium WebDriver offers a range of methods to locate elements on a webpage, providing flexibility and versatility for web automation tasks. These methods enable developers to pinpoint specific elements based on various attributes. Some of the commonly used methods include finding elements by ID, NAME, or XPATH. Additionally, elements can be located by their LINK_TEXT or PARTIAL_LINK_TEXT. Other options include finding elements by their TAG_NAME, by CLASS_NAME, or by using CSS_SELECTOR. By leveraging these various methods, developers can effectively locate and interact with elements on webpages using Selenium with Python. The below table shows the syntax to use different methods.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2Ar441Hwccre9-rxgYuOT7RA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2Ar441Hwccre9-rxgYuOT7RA.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To use &lt;em&gt;**By *&lt;/em&gt;*following import statement needs to be imported:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium.webdriver.common.by import By
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2724%2F0%2AgHJk_jHR01GLbq15.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2724%2F0%2AgHJk_jHR01GLbq15.png" width="800" height="199"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Find element by ID:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
To get a form from the below HTML snippet:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2724%2F0%2AOplcM6lhNEyOUylC.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2724%2F0%2AOplcM6lhNEyOUylC.png" width="800" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Follow the find_element method will be used:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;contact_form = driver.find_element(By.ID, ‘contactForm’)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2724%2F0%2AJSnVpgka26pCnQLo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2724%2F0%2AJSnVpgka26pCnQLo.png" width="800" height="199"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Other methods will work similarly to the ID method.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Experience efficient &lt;a href="https://www.lambdatest.com/android-device-test?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Android device test&lt;/a&gt; with LambdaTest. Test your apps interactively on a variety of Android devices for optimal performance and user experience.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Adding value to the input field
&lt;/h2&gt;

&lt;p&gt;In automation, interacting with input fields is a regular task. &lt;em&gt;Right-click&lt;/em&gt; anywhere on the webpage and choose *&lt;em&gt;inspect **element to view the element’s details. Hover over the element you want to access. After utilizing the find_element function to identify the input element, you may imitate keyboard input by using the *send_keys()&lt;/em&gt; method. You can simulate user input by filling the input field with the desired data by giving the desired text or value as an argument.&lt;/p&gt;

&lt;p&gt;The below code shows how to use the method:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ApKY8jIRCtzI8ArIA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ApKY8jIRCtzI8ArIA.png" width="800" height="477"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The below code is using element’s ID to access it:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;search_for_key = driver.find_element(By.ID, "twotabsearchtextbox")
# Below code will add iPhone 14 in the input field.
search_for_key.send_keys("iphone 14")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3108%2F0%2AIHbtQ36N7br9eP76.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3108%2F0%2AIHbtQ36N7br9eP76.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Triggering Button
&lt;/h2&gt;

&lt;p&gt;Buttons play an important role in web applications, from submitting a form to adding some functionalities, and testing their functionality is vital. For this task, we will use the &lt;em&gt;**Xpath *&lt;/em&gt;&lt;em&gt;to locate the element. To get the path of the element, *right-click&lt;/em&gt; on the element and select &lt;em&gt;copy *from the available options; select copy *&lt;/em&gt;&lt;em&gt;Xpath&lt;/em&gt;**(relative path of the element).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Az6qc3-PYJdM1ZS0N.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Az6qc3-PYJdM1ZS0N.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code to store the element in a variable by XPath will look as below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;search_btn = driver.find_element(By.XPATH, ‘//*[@id=”nav-search-submit-button”]’)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A787dyCFdInTW316b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A787dyCFdInTW316b.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;click()&lt;/strong&gt; method will click the button.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;search_btn.click()search_btn.click()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AarL0GTGtEHwMeAN5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AarL0GTGtEHwMeAN5.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Controlling Dropdown
&lt;/h2&gt;

&lt;p&gt;Select elements or dropdown menus are frequently found in web forms. Consider a situation where you need to control the dropdown menu. As shown below, we need to select &lt;em&gt;Digital Music&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A6z8n_Y8rR9PpbNzt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A6z8n_Y8rR9PpbNzt.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To use Dropdown get the XPath of the option you want to select. In the below code &lt;strong&gt;option[10]&lt;/strong&gt; is assigned to &lt;em&gt;Digital Music.&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select_category_btn = driver.find_element(By.XPATH, ‘//*[@id=”searchDropdownBox”]/option[10]’).click()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After selecting the category below, the piece of code will add the text to the search field.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;search_for_key = driver.find_element(By.ID, "twotabsearchtextbox")
search_for_key.send_keys("iphone 14")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The below code will press the search button to trigger the search.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;search_btn = driver.find_element(By.XPATH, '//*[@id="nav-search-submit-button"]')
search_btn.click()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AlEx5jJTcrvmBqH4n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AlEx5jJTcrvmBqH4n.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To know more about other methods, follow a detailed article for handling &lt;a href="https://www.lambdatest.com/blog/handling-dropdown-in-selenium-webdriver-python/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Dropdowns&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Clearing Edit Box or Text Input
&lt;/h2&gt;

&lt;p&gt;Another important action in Selenium test cases is clearing the text input or edit box’s contents. To clear the content/text of an edit box or text input, you can use the &lt;em&gt;clear()&lt;/em&gt; method provided by the WebElement class.&lt;/p&gt;

&lt;p&gt;Here’s an example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;search_for_key = driver.find_element(By.ID, "twotabsearchtextbox")
# Clear the input field
search_for_key.clear()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A_ziR2BNjbogxLU6s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A_ziR2BNjbogxLU6s.png" width="800" height="187"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Advanced Python Selenium Techniques in Chrome
&lt;/h2&gt;

&lt;p&gt;Selenium offers a large range of advanced techniques that let you manage many different actions encountered while performing web testing as well as complex automation activities. In this section, we will cover advanced Python Selenium techniques in Chrome, including drag-and-drop, moving through history and location, switching between windows and frames, handling popups and alarms, adding cookies, adding waits, and taking screenshots.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Perform manual or automated &lt;a href="https://www.lambdatest.com/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;cross browser device testing&lt;/a&gt; on 3000+ browsers online. Deploy and scale faster with the most powerful cross browser testing tool online.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Drag and Drop
&lt;/h2&gt;

&lt;p&gt;This functionality is commonly found in modern web apps. Selenium also provides this functionality to simulate drag-and-drop actions on web elements. By locating the source and the target element using the &lt;em&gt;find_element&lt;/em&gt; method, you can use the &lt;em&gt;drag_and_dro*p or *drag_and_drop_by_offset&lt;/em&gt; methods to perform drag-and-drop operations, imitating user interactions and validating the expected behavior of the application.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Get the element by its name
source = driver.find_element(By.NAME, "source")
target = driver.find_element(By.NAME, "target")


# initialize the ActionChains
action_chains = ActionChains(driver)


# drag_and_drop method is a part of ActionChains
action_chains.drag_and_drop(source, target).perform()
search_for_key = driver.find_element(By.ID, "twotabsearchtextbox")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2956%2F0%2Akdg4dWHO7YnNmxKq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2956%2F0%2Akdg4dWHO7YnNmxKq.png" width="800" height="313"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For more detailed information on this topic, please refer to our comprehensive blog post on &lt;a href="https://www.lambdatest.com/blog/drag-and-drop-in-selenium-python/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Drag and Drop&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Switching between windows and frames
&lt;/h2&gt;

&lt;p&gt;Most websites nowadays have various iframes and windows which are used to implement various features WebDriver also provides support moving between named windows using the “switch_to.window” method:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# To change the window
driver.switch_to.window("windowName")


# To change the frame
driver.switch_to.frame("frameName")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2100%2F0%2AqNN2k6UMkjlXZUa_.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2100%2F0%2AqNN2k6UMkjlXZUa_.png" width="800" height="332"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Scroll a web application
&lt;/h2&gt;

&lt;p&gt;Most websites have scroll to make it more interactive and engaging. The selenium scroll-down code is shown below. It calls the method &lt;em&gt;execute_script()&lt;/em&gt; with the javascript to scroll to the end of the web page.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
import time


driver = webdriver.Chrome()
driver.get("https://www.amazon.com")


# scroll the website or webpage to the complete body height
driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
time.sleep(3)
driver.close()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Popups and Alerts
&lt;/h2&gt;

&lt;p&gt;Handling popups and alerts are paramount when automating web testing. Popups and alerts can appear during various scenarios, such as confirmation messages, error alerts, warnings, or authentication dialogs.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# import Alert
from selenium.webdriver.common.alert import Alert
driver = webdriver.Chrome()
# create an alert object
alert = Alert(driver)
# To print the alert text
print(alert.text)
# accept the alert
alert.accept()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2604%2F0%2AW2FoFPtOozJfbG8j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2604%2F0%2AW2FoFPtOozJfbG8j.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To delve deeper into this topic, we recommend referring to our comprehensive blog post specifically covering &lt;a href="https://www.lambdatest.com/blog/how-to-handle-javascript-alert-in-selenium-webdriver/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Alert and Popup&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Test your native app and website on real iOS and Android devices hosted on the cloud. LambdaTest is a convenient, cost-effective and centralised solution for running realtime and Automated &lt;a href="https://www.lambdatest.com/real-device-cloud?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;test on real mobile devices&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Navigating through history and location
&lt;/h2&gt;

&lt;p&gt;Selenium enables you to control the browser’s location and navigate its history. To simulate user navigation behaviors, you can use Selenium WebDriver class methods like &lt;em&gt;back(), forward(), and refresh()&lt;/em&gt;. You can test the behavior of web applications using these techniques to navigate to other pages, go back to the previous page, go forward to the next page, or refresh the current page.&lt;/p&gt;

&lt;p&gt;The below code will showcase the usage of the methods:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Get the website URL
driver.get("https://www.google.com")
driver.get("https://wwww.amazon.com/")
 # Step backward in the browser history
driver.back()
 # Step forward in the browser history
driver.forward()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2360%2F0%2AUuCwtbGQ1ryGwZ5F.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2360%2F0%2AUuCwtbGQ1ryGwZ5F.png" width="800" height="393"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Cookies
&lt;/h2&gt;

&lt;p&gt;A cookie is a piece of data from a website stored within a &lt;a href="https://www.lambdatest.com/web-browser-online?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;web browser&lt;/a&gt; that the website can retrieve later. Web applications utilize cookies heavily to store session data and user preferences.&lt;/p&gt;

&lt;p&gt;For adding cookies while running tests, Selenium offers several options. You can define the cookie’s name, value, domain, path, and other parameters using the &lt;em&gt;add_cookie&lt;/em&gt; method, whereas the &lt;em&gt;get_cookies()&lt;/em&gt; method is used to get the values of cookies. You may mimic personalized or logged-in user sessions by adding cookies, allowing for thorough testing of application operation. The cookies will be stored for a particular domain.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Set a valid Domain
driver.get("http://www.example.com")


# Now set the cookie. It is valid for the entire domain
cookie = {'key' : 'value'}
driver.add_cookie(cookie)


# get the value stored cookie
driver.get_cookies()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2832%2F0%2AOzfrVJHkkmYMGoJ4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2832%2F0%2AOzfrVJHkkmYMGoJ4.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To learn more about this topic, we advise you to read our in-depth blog post about &lt;a href="https://www.lambdatest.com/blog/handling-cookies-in-selenium-webdriver/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Handling Cookies&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Waits
&lt;/h2&gt;

&lt;p&gt;One must incorporate waits in order to time test execution with the loading and rendering of the web page. You can tell Selenium to wait until a specific condition is met before moving on to the next phase by using &lt;strong&gt;explicit&lt;/strong&gt; or &lt;strong&gt;implicit&lt;/strong&gt; waits. This increases test stability and dependability by ensuring that the test case waits for particular items to be visible, clickable, or present.&lt;/p&gt;

&lt;p&gt;There are two types of waits &lt;strong&gt;&lt;em&gt;explicit&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;implicit&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explicit waits:&lt;/strong&gt; This piece of code that you declare to wait until a specific event takes place before continuing on the website. The extreme case of this is time.sleep(), which sets the condition to an exact time period to wait.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
try:
   # wait 5 seconds before looking for element
   element = WebDriverWait(driver, 5).until(
       EC.presence_of_element_located((By.ID, "elementName"))
   )
finally:
   # else quit
   driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3100%2F0%2AY13e-hONNqUeDx0p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3100%2F0%2AY13e-hONNqUeDx0p.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implicit waits:&lt;/strong&gt; It tells WebDriver to poll the DOM for a certain amount of time when trying to find any element (or elements) not immediately available. The default setting is 0.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;driver.implicitly_wait(10) # seconds
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2188%2F0%2AAvClyWe6wH4PY55c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2188%2F0%2AAvClyWe6wH4PY55c.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We suggest reading our in-depth blog post on &lt;a href="https://www.lambdatest.com/blog/types-of-waits-in-selenium/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Types of waits&lt;/a&gt; to learn it in depth.&lt;/p&gt;

&lt;h2&gt;
  
  
  Taking Screenshots
&lt;/h2&gt;

&lt;p&gt;It is practical to take screenshots while a test execution for documentation, debugging, and reporting purposes. Selenium offers tools for taking screenshots of a web page whenever you want. You can save the screenshot to a particular location by employing the *save_screenshot *method. By taking screenshots, you may visually check the condition of the website and collect proof of test execution for subsequent investigation.&lt;/p&gt;

&lt;p&gt;To store screenshots &lt;em&gt;pillow&lt;/em&gt; library is required. It can be installed using the following command:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 -m pip install - upgrade pip
python3 -m pip install - upgrade Pillow
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The below code will take a screenshot and open the code is implemented with it open a new window to show the preview of the screenshot.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from PIL import Image
driver = webdriver.Chrome()
driver.get("http://amazon.com")
# Taking the image
driver.save_screenshot("image.png")
# Loading the image
image = Image.open("amazonWebsiteImage.png")
# Showing the image
image.show()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2716%2F0%2A2YDvoRzKCjilvaHh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2716%2F0%2A2YDvoRzKCjilvaHh.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To delve deeper into this topic, we recommend you to explore our extensive blog post covering the topic of &lt;a href="https://www.lambdatest.com/blog/python-selenium-screenshots/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Python Selenium Screenshots&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Want to ensure your website is optimized for mobile traffic? Our comprehensive guide to &lt;a href="https://www.lambdatest.com/blog/mobile-website-testing/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;website testing on different devices&lt;/a&gt; and everything you need to know.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Chrome Options and Preferences
&lt;/h2&gt;

&lt;p&gt;ChromeOptions class has been introduced in the latest/updated version of Selenium. It is helpful to make changes in the Chrome browser, whereas, DesiredCapabilities is an old concept (its usage in Java is deprecated.) to configure or make changes in the browser. Save this answer.&lt;/p&gt;

&lt;p&gt;In Selenium 3, capabilities were defined in a session by using Desired Capabilities classes. As of Selenium 4, you must use the browser options classes. For remote driver sessions, a browser options instance is required as it determines which browser will be used.&lt;/p&gt;

&lt;h2&gt;
  
  
  Page Load Strategy
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://www.lambdatest.com/blog/selenium-page-load-strategy/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;page load strategy&lt;/a&gt; specifies how long the browser should wait for the page to load before continuing. Different page load techniques, including &lt;em&gt;regular, eager, and none&lt;/em&gt;, are offered by Selenium. You can manage how Selenium interacts with the page while running the test by defining the preferred page load strategy in Chrome Options. By selecting the proper page load strategy, you may increase test stability by making sure that your tests wait until all required items have loaded completely.&lt;/p&gt;

&lt;p&gt;Types of loading:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;normal&lt;/em&gt;&lt;/strong&gt; : Used by default, waits for all resources to download&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;eager&lt;/em&gt;&lt;/strong&gt; : DOM access is ready, but other resources like images may still be loading&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;none&lt;/em&gt;&lt;/strong&gt; : Any Does not block WebDriver at all&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The below code will showcase how to incorporate eager technique to load the website.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# initialize options to use it
options = Options()


options.page_load_strategy = 'eager'


# adding options to local chrome driver
driver = webdriver.Chrome(options=options)
driver.get("http://www.google.com")
driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2788%2F0%2AkH85il44CpI-QWHB.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2788%2F0%2AkH85il44CpI-QWHB.png" width="800" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Headless Mode
&lt;/h2&gt;

&lt;p&gt;You can use Chrome’s headless mode to run it without displaying the graphical user interface(GUI). It is helpful for reducing test execution time or for conducting tests in a server environment. You can automate the web without a visible browser window by turning on the headless mode in Chrome Options. When performing tests on distant servers or in a continuous integration (CI) environment, headless mode is especially useful.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
# add_argument parameter tells Chrome browser that it should be run without UI (Headless)
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)
driver.get("http://www.amazon.com")
driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AAcIfwTi63Jb987uv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AAcIfwTi63Jb987uv.png" width="800" height="267"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Proxy Setting
&lt;/h2&gt;

&lt;p&gt;Proxy settings are essential for scenarios where you need to route network traffic through a proxy server. With Selenium, you may modify proxy settings by using Chrome Options. By specifying the proxy server’s address and port, you can route your browser’s requests through the proxy server during test execution. Applications that depend on certain network settings can be tested using this, and it can also be used to simulate various geographic locations.&lt;/p&gt;

&lt;p&gt;With the help of a proxy within tests, we can verify if the website user interface matches the location.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
#proxyserver
py = "128.21.0.0:8080"
#proxy parameter to options
options.add_argument('--proxy-server=%s' % py)
#options to Chrome()
driver = webdriver.Chrome(options=options)
driver.implicitly_wait(0.6)
driver.get("http://www.amazon.com")
driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2788%2F0%2A8bjArkmTvUeKdOkE.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2788%2F0%2A8bjArkmTvUeKdOkE.png" width="800" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Tips and tricks to improve the Chrome browsing experience for web automation
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2048%2F0%2AjdtzOK_gU_NfnexY.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2048%2F0%2AjdtzOK_gU_NfnexY.png" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here are some tips and tricks to enhance the Chrome browsing experience for web automation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use Chrome Developer Tools:&lt;/strong&gt; This tool helps to debug and inspect any website. To access it right-click on a web page and select &lt;strong&gt;Inspect&lt;/strong&gt; or by pressing &lt;em&gt;Ctrl+Shift+I or ⌥ Option + ⌘ Cmd + I.&lt;/em&gt; The Developer Tools allow you to analyze network traffic, manipulate the DOM, and test JavaScript code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Employ User-Agent Switching:&lt;/strong&gt; The User-Agent header identifies the operating system and browser being used. Depending on the user agent, some websites may act differently or restrict access to a particular functionality. You can change the user agent to imitate various devices or browsers to get around this.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Utilize Headless Mode:&lt;/strong&gt; Chrome offers a headless mode, which allows you to run Chrome without a GUI(graphical user interface). It can be advantageous for automation since it reduces the usage of resources and allows you to execute tasks in the background.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Handle Page Load Delays:&lt;/strong&gt; Asynchronous loading of web pages is common, and automation scripts must handle delays effectively. The “Network API,” the powerful API offered by Chrome’s Developer Tools, enables you to keep monitoring network traffic and wait for particular requests to finish before moving further. This might assist in coordinating your automation duties with the manner in which the website loads.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Clear Browser Data:&lt;/strong&gt; To prevent interruption from past sessions when automating activities frequently, it’s crucial to delete browser data including cookies, cache, and local storage. By using the Chrome settings or programming tools for browser automation, you can manually delete this data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;**Optimize Network Performance: **One should take advantage of Chrome’s network-related features to enhance the execution of automation scripts. Using Chrome’s DevTools or automation libraries, you can, for instance, disable images and pointless resources, simulate slow network conditions, or deny particular requests.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Avoid Captchas and IP Blocking:&lt;/strong&gt; To prevent automation, several websites use security measures like Captchas or IP blocking. You can utilize machine learning techniques or third-party captcha-solving services to bypass captchas. Using proxy servers or VPNs, you can rotate IP addresses for IP blocking.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use Page Object Model (POM):&lt;/strong&gt; By portraying web pages as objects, the Page Object Model design pattern aids in the organization and maintenance of automation code. Your automation scripts’ capacity to be reused, read, and maintained is enhanced. Your code will become more modular and easier to maintain if POM is implemented.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Implement Page Interactions:&lt;/strong&gt; Interacting with site elements like buttons, forms, dropdown menus, etc. is common in automation. To find and interact with these items, Chrome automation frameworks like Puppeteer and Selenium WebDriver offer APIs. Your automation capabilities will be improved by effectively understanding and utilizing these APIs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Stay Up-to-date:&lt;/strong&gt; Regular upgrades and new features are introduced as Chrome continues to develop. Keep up with the most recent Chrome upgrades, which may include adjustments to the browser’s functionality, security improvements, and automation-related capabilities. This will enable you to take advantage of new functionality and modify your automation techniques as necessary.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Revolutionize your user experience with &lt;a href="https://www.lambdatest.com/puppeteer-visual-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Puppeteer Visual Testing &lt;/a&gt;— Capturing, Comparing, and Creating Seamless Experiences!&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Integrate Selenium with Python Testing Libraries
&lt;/h2&gt;

&lt;p&gt;Popular Python testing tools like Unittest and Pytest may be simply combined with Selenium to increase the functionality of your test automation system. In this guide, we will explore how to integrate Selenium with these testing libraries, providing a structured and efficient approach to writing and executing tests.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unittest
&lt;/h2&gt;

&lt;p&gt;The base of the Python Unittest library is a third-party package called PyUnit. Based on the well-known JUnit framework, Steve Purcell came up with the idea for PyUnit. It later developed into a recognized Python module starting with version 2.5.&lt;/p&gt;

&lt;p&gt;For a long time, Python’s standard library has included the &lt;em&gt;**unittest *&lt;/em&gt;&lt;em&gt;module. Therefore, installing it using something like pip install unittest is typically not necessary. It works without installation if you simply run **import unittest&lt;/em&gt;* in your Python code.&lt;/p&gt;

&lt;p&gt;To get a better understanding and working of the &lt;em&gt;**unittest *&lt;/em&gt;*module we are going to run the following code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
class SearchText(unittest.TestCase):
   def set_up(self):
       # create a new Chrome session
       self.driver = webdriver.Chrome()
       self.driver.implicitly_wait(10)
       self.driver.maximize_window()
       # navigate to the amazon.com
       self.driver.get("http://www.amazon.com/")


   def test_search_by_text_keyword(self):
       # get the search bar
       self.search_field = self.driver.find_element(By.ID, "twotabsearchtextbox")
       self.search_field.send_keys("iphone 14")
       # enter search keyword and submit
       self.search_btn = self.driver.find_element(By.XPATH, '//*[@id="nav-search-submit-button"]')
       self.search_btn.click()
   def tear_down(self):
       # close the browser window
       self.driver.quit()
if __name__ == '__main__':
   unittest.main()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above code will run from top to bottom. Firstly, the &lt;em&gt;set_up()&lt;/em&gt; method will initialize the driver and opens the mentioned URL. Then &lt;em&gt;test_search_by_text_keyword()&lt;/em&gt; method will be executed where the search box will be selected and added value “iphone 14” and press the search button to search for the item. The test will be ended by closing the browser via &lt;em&gt;tear_down()&lt;/em&gt; method.&lt;/p&gt;

&lt;p&gt;To use the &lt;em&gt;**unittest *&lt;/em&gt;&lt;em&gt;module and write a simple test by inheriting the *&lt;/em&gt;&lt;em&gt;TestCase *&lt;/em&gt;&lt;em&gt;class, you’ll need to import the *&lt;/em&gt;&lt;em&gt;unittest *&lt;/em&gt;&lt;em&gt;module and define a class that inherits the *&lt;/em&gt;&lt;em&gt;TestCase *&lt;/em&gt;*class.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AsbRfSWuIYzZQzw_Y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AsbRfSWuIYzZQzw_Y.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2008%2F0%2Asc13n1APBgTflgDC.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2008%2F0%2Asc13n1APBgTflgDC.png" width="800" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The test case’s entry point is a &lt;strong&gt;&lt;em&gt;set_up()&lt;/em&gt;&lt;/strong&gt; method. It can be used to perform a predetermined set of operations before running a single test or every test included in the class.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2236%2F0%2AxRUrsGoT9EnvcGaw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2236%2F0%2AxRUrsGoT9EnvcGaw.png" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Similar to the &lt;strong&gt;&lt;em&gt;set_up()&lt;/em&gt;&lt;/strong&gt; method, test methods get implemented in the TestCase class.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Test site on mobile easily with LT Browser, free responsive testing tool. Just enter the URL to &lt;a href="https://www.lambdatest.com/test-site-on-mobile?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;test website on all devices&lt;/a&gt; and check if it is compatible across 50+ device viewports.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Pytest
&lt;/h2&gt;

&lt;p&gt;The PyPy project gave rise to the Python testing framework Pytest. It can be used to create a variety of software tests, including functional, end-to-end, unit, and integration tests.&lt;/p&gt;

&lt;p&gt;You must run the following command on the terminal (or command prompt), which makes use of the Python package management (pip), to install &lt;em&gt;pytest&lt;/em&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install pytest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Once the &lt;strong&gt;&lt;em&gt;pytest&lt;/em&gt;&lt;/strong&gt; module is installed successfully. We can run our test using &lt;em&gt;pytest *by the following code and name the file appropriately with the .*py extension:&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Import necessary modules
import pytest


from selenium import webdriver
from selenium.webdriver.common.by import By
# In order for pytest to recognise the test, the test name must begin with test_&amp;lt;name&amp;gt; (for example, test_lambdatest_todo_app).
def test_case():
   driver = webdriver.Chrome()
   driver.maximize_window()
   driver.get("https://www.amazon.com/")
   search_for_key = driver.find_element(By.ID, "twotabsearchtextbox")
   search_for_key.send_keys("iphone 14")
   search_btn = driver.find_element(By.XPATH, '//*[@id="nav-search-submit-button"]')
   search_btn.click()
   driver.close()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Run the Python pytest file using the following command:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pytest &amp;lt;filename&amp;gt;.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Once the test is completed you get information regarding it in the terminal:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AiSe2yfQMI2ql-S_t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AiSe2yfQMI2ql-S_t.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check out our detailed blog on &lt;a href="https://www.lambdatest.com/blog/selenium-python-pytest-testing-tutorial/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Pytest&lt;/a&gt; to know more about it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Challenges of Testing on the Local Grid
&lt;/h2&gt;

&lt;p&gt;Testing on a local grid might provide a variety of difficulties. The lack of readily available physical devices and browsers is one of the major problems. A local grid makes it challenging to test across a variety of device and browser configurations, which could have a negative effect on test coverage as a whole. It also needs a lot of resources, including infrastructure setup, software, and hardware, to maintain and manage. It can take time and money to scale the grid to handle rising test demand.&lt;/p&gt;

&lt;p&gt;In order to address these issues, cloud-based testing grids are required. Cloud-based testing grids provide a large inventory of actual devices and browsers, making them a scalable and adaptable solutions. Testers have access to a variety of setups and can run parallel tests, which speeds up test execution and improves test coverage. Because the testing environment is hosted on the infrastructure of the cloud provider, the cloud-based method eliminates the requirement for managing and maintaining local infrastructure.&lt;/p&gt;

&lt;p&gt;To improve automated testing, a platform like LambdaTest provides cloud-based testing grids. Testers may easily perform Selenium tests in Python on a variety of Chrome browser versions across various operating systems by using LambdaTest. The platform enables testers to build and run automation scripts quickly by providing a seamless interface with well-known test frameworks like Pytest and Unittest.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running Testcase on a cloud-based testing grid
&lt;/h2&gt;

&lt;p&gt;In this section, we are going to run a test on LambdaTest using Pytest as the testing framework. We are going to run our test on WINDOWS 10 operating system with Chrome version 114.0.&lt;/p&gt;

&lt;p&gt;Before Running a Python test on LambdaTest, follow a few simple steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create an account on &lt;a href="https://www.lambdatest.com/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Lambdatest&lt;/a&gt; and complete all the required processes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to the dashboard by clicking the &lt;strong&gt;&lt;em&gt;dashboard&lt;/em&gt;&lt;/strong&gt; button on the top right corner or Follow this &lt;a href="https://accounts.lambdatest.com/dashboard?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;LambdaTest Dashboard&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Get your credentials from the &lt;em&gt;profile icon&lt;/em&gt; located in the top right corner, and then select the &lt;em&gt;profile&lt;/em&gt; option it will redirect to the profile screen. Select &lt;strong&gt;Password &amp;amp; Security&lt;/strong&gt; here; you can find your &lt;em&gt;Username and Access Key&lt;/em&gt; and save it for future use. Or use this &lt;a href="https://accounts.lambdatest.com/security?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Profile link&lt;/a&gt;.&lt;/p&gt;
&lt;h1&gt;
  
  
  Import required modules
&lt;/h1&gt;

&lt;p&gt;import pytest&lt;br&gt;
from selenium import webdriver&lt;br&gt;
from selenium.webdriver.chrome.options import Options as ChromeOptions&lt;br&gt;
from selenium.webdriver.common.by import By&lt;/p&gt;
&lt;h1&gt;
  
  
  username and access_key is important to run your test on LambdaTest
&lt;/h1&gt;

&lt;p&gt;username = ""&lt;br&gt;
access_key = ""&lt;/p&gt;
&lt;h1&gt;
  
  
  Capabilities define the OS, Browser name, and other necessary details
&lt;/h1&gt;

&lt;p&gt;lt_options = {&lt;br&gt;
   "user": username,&lt;br&gt;
   "accessKey": access_key,&lt;br&gt;
   "build": "First build",&lt;br&gt;
   "name": "First Test",&lt;br&gt;
   "platformName": "Windows 10",&lt;br&gt;
   "video": True,&lt;br&gt;
   "w3c": True,  # informing latest Selenium 4 being used&lt;br&gt;
   "browserName": "Chrome",&lt;br&gt;
   "browserVersion": "114.0",&lt;br&gt;
   "selenium_version": "4.8.0"&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;def test_app():&lt;br&gt;
   # To run the test on the platform&lt;br&gt;
   remote_url = "&lt;a class="mentioned-user" href="https://dev.to/hub"&gt;@hub&lt;/a&gt;.lambdatest.com/wd/hub"&amp;gt;http://{}:{}&lt;a class="mentioned-user" href="https://dev.to/hub"&gt;@hub&lt;/a&gt;.lambdatest.com/wd/hub".format(username, access_key)&lt;br&gt;
   browser_options = ChromeOptions()&lt;/p&gt;

&lt;p&gt;# adding the capability to the chrome&lt;br&gt;
   browser_options.set_capability('LT:Options', lt_options)&lt;/p&gt;

&lt;p&gt;# initializing remote server&lt;br&gt;
   driver = webdriver.Remote(command_executor=remote_url, options=browser_options)&lt;br&gt;
   driver.get("&lt;a href="https://www.amazon.com/%22" rel="noopener noreferrer"&gt;https://www.amazon.com/"&lt;/a&gt;)&lt;br&gt;
   search_for_key = driver.find_element(By.ID, "twotabsearchtextbox")&lt;br&gt;
   search_for_key.send_keys("iphone 14")&lt;br&gt;
   search_btn = driver.find_element(By.XPATH, '//*[&lt;a class="mentioned-user" href="https://dev.to/id"&gt;@id&lt;/a&gt;="nav-search-submit-button"]')&lt;br&gt;
   search_btn.click()&lt;br&gt;
   driver.close()&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Understanding the code step by step:&lt;/p&gt;

&lt;p&gt;Add your credentials here as it will help the lambdaTest to run tests on your account:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A9bx06Tfzj0zeAEdF.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A9bx06Tfzj0zeAEdF.png" width="638" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Get your desired capabilities that can be generated from the &lt;a href="https://www.lambdatest.com/capabilities-generator/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;capabilities generator&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2824%2F0%2Ahn7KFr8xilK8gVlJ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2824%2F0%2Ahn7KFr8xilK8gVlJ.png" width="800" height="518"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations on achieving a milestone.&lt;/p&gt;

&lt;p&gt;Find details of your test case under &lt;strong&gt;&lt;em&gt;Automation&amp;gt;Builds&lt;/em&gt;&lt;/strong&gt; click on the &lt;em&gt;First test&lt;/em&gt; to get details of the implementation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AGiTD5M-Ua9ZDYKXi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AGiTD5M-Ua9ZDYKXi.png" width="800" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Explore various available test details to get a better idea of the platform.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ASo_aHkiC9JFm_ia9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ASo_aHkiC9JFm_ia9.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In a nutshell, Python Selenium with Chrome provides a reliable and adaptable approach to performing testing and automating web-based tasks. In this blog, we’ve covered a variety of Python Selenium with Chrome, starting with how to install and configure ChromeDriver on various operating systems. We gained advanced skills, including drag-and-drop, dealing with pop-ups and warnings, and navigating through history and location. We also learned how to open websites, interact with items on web pages, and execute other basic tasks.&lt;/p&gt;

&lt;p&gt;We also go in-depth on some of the extra functionality and personalization choices offered by ChromeOptions, like proxy settings, headless mode, and page load strategies. Additionally, we covered Tips and Tricks to improve Chrome browsing for web automation.&lt;/p&gt;

&lt;p&gt;We explored the integration of Selenium with well-known Python testing frameworks like Unittest and Pytest to broaden the scope of testing. As a result, developers can use these frameworks’ robust functionality and reporting skills in conjunction with Selenium.&lt;/p&gt;

&lt;p&gt;You may streamline your web automation responsibilities, increase testing productivity, and ensure the quality of your online applications by becoming proficient with Python Selenium with Chrome. You now have a good base to build upon as you continue your exploration and use Python Selenium to its best potential in your projects. Happy automating!&lt;/p&gt;

&lt;p&gt;To dive deep into Selenium with Python, follow &lt;a href="https://www.lambdatest.com/learning-hub/python-tutorial?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_25&amp;amp;utm_term=bh&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Python Tutorial with Selenium&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions (FAQs)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What are the significant changes from Selenium 3 to Selenium 4?
&lt;/h3&gt;

&lt;p&gt;Compared to Selenium 3, Selenium 4 brings substantial modifications and enhancements. A new architecture dubbed Selenium Grid 4, stronger support for contemporary web technologies, including the W3C WebDriver Protocol, improved documentation, new APIs for interacting with web elements, and increased debugging abilities are just a few of the essential enhancements.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is the difference between ChromeOptions and Desired Capabilities?
&lt;/h3&gt;

&lt;p&gt;Both ChromeOptions and Desired Capabilities are used in Selenium for customizing the behavior of the Chrome browser.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;ChromeOptions&lt;/strong&gt; is a class specific to the Chrome browser that allows you to set various browser options such as window size, user agent, and proxy settings. It provides a more convenient and readable way to configure Chrome-specific options.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Desired Capabilities&lt;/strong&gt; on the other hand, is a generic concept in Selenium that applies to all browsers. It allows you to set a range of capabilities for browser automation, including browser name, version, platform, and other specific settings. Desired Capabilities can be used to customize the behavior of any browser supported by Selenium, not just Chrome.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What are the Python Selenium Chrome best practices?
&lt;/h3&gt;

&lt;p&gt;For efficient automation, it’s crucial that you stick to recommended practices while utilizing ChromeDriver with Python Selenium. For better speed, use headless mode, use explicit waits rather than static sleep, and keep Chrome and ChromeDriver up to date. For maintainable code, specify window size, avoid pointless interactions, and apply the page object pattern. Utilize effective locators like IDs or CSS_SELECTORS, gently handle exceptions, and properly close the browser and driver. Effectively manage sessions and cookies. By following these tips you can improve the dependability, performance, and maintainability of your Python Selenium Chrome automation scripts.&lt;/p&gt;

</description>
      <category>python</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>How To Setup And Install PhantomJS In Python: All You Need To Know</title>
      <dc:creator>jainishpatel78</dc:creator>
      <pubDate>Mon, 22 Jan 2024 08:32:23 +0000</pubDate>
      <link>https://forem.com/testmuai/how-to-setup-and-install-phantomjs-in-python-all-you-need-to-know-447d</link>
      <guid>https://forem.com/testmuai/how-to-setup-and-install-phantomjs-in-python-all-you-need-to-know-447d</guid>
      <description>&lt;p&gt;Running automation scripts on a normal browser like Chrome, Firefox, or Edge Browser takes too many resources and time as they run test scripts that include all the UI/UX of the website, such as animations and visual effects. So the concept of Headless browsers is introduced. Headless browsers are web browsers that can operate without a graphical user interface (GUI), allowing developers to automate tasks like web scraping and testing. They interact with websites programmatically, processing HTML and executing JavaScript instead of rendering web pages visually. PhantomJS was one of the earliest and simplest headless browsers, offering a JavaScript API for browser manipulation. However, alternatives like Puppeteer (for Node.js) and Selenium WebDriver have gained more traction in recent years.&lt;/p&gt;

&lt;p&gt;In this article, we will cover various aspects of PhantomJS, starting from How to install PhantomJS, practical examples, advantages and disadvantages, and running test scripts with it. At last, we will see a test time execution comparison with PhantomJS and other browsers.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;New to &lt;a href="https://www.lambdatest.com/learning-hub/visual-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_22&amp;amp;utm_term=bh&amp;amp;utm_content=youtube" rel="noopener noreferrer"&gt;visual testing&lt;/a&gt;? Discover the essentials in our comprehensive guide. Learn what is visual testing, why it matters, and how to begin.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What is a Headless Browser?
&lt;/h2&gt;

&lt;p&gt;A &lt;a href="https://www.lambdatest.com/web-browser-online?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_22&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;web browser&lt;/a&gt; that doesn’t use a Graphical User Interface (GUI) is known as a headless browser. An interface for a headless browser differs from that of a standard browser, such as Chrome, Firefox, Microsoft Edge, or Safari, in that it interacts with websites programmatically rather than rendering the content on a visible window. Testers and Developers can use scripts or code to control it, which enables them to automate a variety of web-related tasks like website monitoring, web scraping, and automated testing. PhantomJS, Headless Chrome, and Firefox are a few examples of popular headless browsers.&lt;/p&gt;

&lt;p&gt;Several use cases in web development and testing necessitate headless browsers. Web scraping, the process of taking data from websites for analysis or commercial gain, is a major contributing factor. Large-scale scraping tasks can benefit from headless browsers since they offer a quicker and more effective method of retrieving data programmatically without requiring a visible browser window. Furthermore, automated testing is done with headless browsers to verify the functioning and speed of websites. Testers and Developers can save time and resources by conducting tests in a headless environment while guaranteeing the application operates successfully in various scenarios by executing scripts in headless mode.&lt;/p&gt;

&lt;p&gt;Headless browsers are an invaluable tool for web developers and testers because they provide a solid and adaptable interface for programmatic interaction with web content.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is PhantomJS?
&lt;/h2&gt;

&lt;p&gt;A headless browser called PhantomJS enables programmers to automate interactions with online pages and carry out operations like web scraping, automated testing, and website performance analysis. The WebKit rendering engine, also utilized by Safari browsers, serves as its foundation.&lt;/p&gt;

&lt;p&gt;PhantomJS is suitable for server-side operations and automation activities since it runs in a headless mode or without a graphical user interface(GUI). It offers a JavaScript API that enables programmers to control the content of web pages and script browser behaviors.&lt;/p&gt;

&lt;p&gt;The capability of PhantomJS to process and render web pages with JavaScript is one of its core capabilities. It has a JavaScript engine integrated within it that can run and parse JavaScript code, making it helpful for scraping dynamically created content or interacting with JavaScript-heavy websites.&lt;/p&gt;

&lt;p&gt;It provides a range of features, such as network monitoring, screenshot taking, DOM modification, web page navigation, and form submission. It enables programmed data extraction from web pages and simulation of user interactions.&lt;/p&gt;

&lt;p&gt;It’s crucial to remember that PhantomJS development has been halted since the 5th of August 2017; therefore, it might not be the option for a headless browser that is being maintained or updated the most. Because of that, Selenium 3.8.1 also deprecated PhantomJS from its support and recommends using Chrome and Firefox in headless mode.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo4fuw74i8nflhd97tni7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo4fuw74i8nflhd97tni7.png" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Explore the best 25 &lt;a href="https://www.lambdatest.com/blog/visual-testing-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_22&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;visual testing tools&lt;/a&gt; for a seamless user experience in 2023. Find your perfect tool today with our comprehensive list.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Integration of PhantomJS in Python
&lt;/h2&gt;

&lt;p&gt;The Selenium WebDriver enables smooth integration of PhantomJS in Python. A popular automation system called Selenium offers a Python API for managing web browsers. You can use PhantomJS’s capability for a variety of tasks by combining Selenium and PhantomJS.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2mzt0qxf4uuf32ip3ph8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2mzt0qxf4uuf32ip3ph8.png" width="800" height="310"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The significance of utilizing PhantomJS in Python for activities like website testing, data extraction, and screenshot generation is due to its headless nature and features like network monitoring and JavaScript support:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Website testing:&lt;/strong&gt; Automated website testing is possible with PhantomJS without a visible browser window. It can mimic user interactions, evaluate UI components, test functionality, and record test results.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data extraction:&lt;/strong&gt; Web scraping and data extraction are effective when Python, Selenium, and PhantomJS are used together. It can manage web pages with a lot of JavaScript, dynamically generated content, and intricate DOM structures. Web page navigation, element extraction, and data retrieval are all possible for developers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Creating screenshots:&lt;/strong&gt; PhantomJS can display online pages and take screenshots, making it possible to conduct visual tests, create website thumbnails, or take pictures when web scraping. This can be helpful for visual documentation creation, preview creation, and website appearance monitoring.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can automate web browser interactions, conduct effective testing, retrieve data from websites, and create screenshots headlessly by utilizing the combination of PhantomJS in Python through Selenium.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to install PhantomJS in Python
&lt;/h2&gt;

&lt;p&gt;In this section, we will see the installation process in detail, from setting up Python to PhantomJS WebDriver and Selenium. With that, we will verify it by running a simple script and scraping the website title on which the script runs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation and Environment Setup
&lt;/h2&gt;

&lt;p&gt;The process is quite simple if it follows the chronology. To install PhantomJS in Python, we will see it in a detailed manner, starting from installing Python -&amp;gt; PhantomJS -&amp;gt; Selenium.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installation and Environment Setup
&lt;/h3&gt;

&lt;p&gt;To install Python on your computer, you need to follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Visit the Python official website.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Python provides installers for various platforms, including Windows, macOS, and Linux. It will be selected according to your system automatically on which you are opening the above site.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Download the latest version of Python, it can easily be downloaded by the Download &lt;strong&gt;Python 3.x.x&lt;/strong&gt; button.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run the installer file once it has been downloaded to begin the installation procedure.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Make sure you click the box on the installation wizard that states &lt;strong&gt;&lt;em&gt;Add Python to PATH&lt;/em&gt;&lt;/strong&gt; or &lt;em&gt;**Add Python to environment variables *&lt;/em&gt;*(the words vary depending on the version). This makes sure that Python is accessible from anywhere on your machine.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can change the directory according to your preferences.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click **Install **to begin the process of installation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hold off until the installation is finished. The installer will configure Python on your machine and copy the required files.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You should get a notification indicating a successful installation once the installation is complete.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Open a command prompt or terminal and enter the following command:&lt;/p&gt;

&lt;p&gt;python — version&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Console Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AsVY8hJklELfKc4RI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AsVY8hJklELfKc4RI.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If the above command runs fine, Python is installed successfully.&lt;br&gt;
Now that Python is installed successfully let’s install PhantomJS.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;With LambdaTest’s Smart &lt;a href="https://www.lambdatest.com/smart-visual-ui-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_22&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Visual UI Testing&lt;/a&gt;, ensure visual perfection and deliver seamless websites and web applications 3000+ devices. Try now!&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Installing PhantomJS and setup
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;From the official website, get the PhantomJS binary that matches your operating system.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AN-2uGQYYURHH6KPp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AN-2uGQYYURHH6KPp.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Extract the downloaded archive into the desired location.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can find the &lt;em&gt;phantomjs.exe&lt;/em&gt; file in the bin folder of the extracted folder.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add the location of the &lt;em&gt;phantomjs.exe&lt;/em&gt; file to the Environment Variables.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You are just one step away before using it. To do that, Selenium WebDriver is needed, a part of the Selenium module. So let’s download Selenium.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing Selenium for Python
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Open a &lt;strong&gt;terminal **or **command prompt.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install the Selenium package for Python using the *pip *Python package manager.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Run the following command:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install selenium==3.8.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Console Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2652%2F0%2AYVJnTIPya5Boj_i0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2652%2F0%2AYVJnTIPya5Boj_i0.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: From Selenium 3.8.1 PhantomJS is deprecated. So, to use the PhantomJS it is recommended to use **Selenium 3.8.0 or lower version&lt;/em&gt;&lt;em&gt;. You can also refer to the changelog for more information.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Verifying using a simple Python script with PhantomJS
&lt;/h2&gt;

&lt;p&gt;Here is a little piece of code that shows how to initialize the PhantomJS WebDriver and carry out fundamental operations:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver

# Initialize the PhantomJS WebDriver
driver = webdriver.PhantomJS()
driver.set_window_size(1120, 550)

# Navigate to a web page
driver.get('https://www.google.com')

# Get the page title
print("Page title:", driver.title)

# Take a screenshot of the page
driver.save_screenshot('screenshot.png')

# Close the WebDriver
driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2844%2F0%2AqrQShkvbM8s6ZqxM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2844%2F0%2AqrQShkvbM8s6ZqxM.png" width="800" height="46"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s understand the code Step-by-Step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Import statement to get the webdriver from the selenium module.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AiA4eSYeEz8PiluFG.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AiA4eSYeEz8PiluFG.png" width="800" height="309"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;There are 2 ways to use PhantomJS. You can use it according to your requirements. But, we are using the first way as we already added the Driver to the system’s environment Variables.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3108%2F0%2ABmGK_W5I1z90Dzv4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3108%2F0%2ABmGK_W5I1z90Dzv4.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we are using a headless browser just to confirm that the website is opening this line, we will take the screenshot and store it in the same location where the Python file is.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2144%2F0%2A6du1s4LBZxpUi-oS.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2144%2F0%2A6du1s4LBZxpUi-oS.png" width="800" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Output Screenshot:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2240%2F0%2AQs_4MCzzknG8JVKD.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2240%2F0%2AQs_4MCzzknG8JVKD.png" width="800" height="392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;With LambdaTest’s Smart &lt;a href="https://www.lambdatest.com/smart-visual-ui-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_22&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Automated Visual Testing&lt;/a&gt;, ensure visual perfection and deliver seamless websites and web applications 3000+ devices. Try now!&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Practical Examples of PhantomJS
&lt;/h2&gt;

&lt;p&gt;With the help of the headless web browser PhantomJS, you can &lt;a href="https://www.lambdatest.com/learning-hub/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_22&amp;amp;utm_term=bh&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;automate testing&lt;/a&gt; and carry out numerous &lt;a href="https://www.lambdatest.com/blog/web-scraping-using-selenium-and-python/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_22&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;web scraping&lt;/a&gt; operations. It has a JavaScript API that enables you to manage websites, control the browser, and take screenshots. In this section, we are going to have a glance at both of them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Automation Testing
&lt;/h2&gt;

&lt;p&gt;In automation testing, there are various advantages to using a headless browser like PhantomJS. Without a visual interface, it mimics actual browser behavior, accelerating test performance. It makes it easier to test complicated applications across platforms because it can render and interact with web pages just like a regular browser. Because of its command-line interface and lightweight design to efficiently minimize resource usage and improve performance, testing frameworks may be quickly integrated, resulting in scalable and reliable automation.&lt;/p&gt;

&lt;p&gt;You can use frameworks like Selenium WebDriver to create Python and PhantomJS test cases. In the example, we will search for a query and get the resultant output by printing it in the terminal and taking a screenshot.&lt;/p&gt;

&lt;p&gt;Here is an illustration of how to create a test case in Python and PhantomJS:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
from selenium.webdriver.common.by import By

# Configure the PhantomJS WebDriver
driver = webdriver.PhantomJS()

# Navigate to a web page
driver.get('https://www.lambdatest.com/selenium-playground/table-sort-search-demo')
driver.set_window_size(1440, 550)

# Getting the search bar
search_bar = driver.find_element(By.XPATH,'//*[@id="example_filter"]/label/input')
search_bar.send_keys("London")

# Getting row of the table
rows = driver.find_elements(By.TAG_NAME,"tr")

# Capturing the screenshot of the window 
driver.save_screenshot('table_data.png')

# Printing the rows
for row in rows:
    data = row.text 
    print(data)
driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2692%2F0%2Ab8E4mMYgIRTdNiI0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2692%2F0%2Ab8E4mMYgIRTdNiI0.png" width="800" height="156"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Screenshot of the result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AD_G6wk6WkyscZppK.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AD_G6wk6WkyscZppK.png" width="800" height="421"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AO_PL7XnkRRTX-ziE.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AO_PL7XnkRRTX-ziE.png" width="800" height="502"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Web Scraping
&lt;/h2&gt;

&lt;p&gt;PhantomJS in Python for web scraping has several advantages, including enhanced resource management and faster execution. It can extract data more quickly as it doesn’t need to visually render web pages because it runs in a headless context. Additionally, it enables better resource management because it uses fewer system resources than a full browser with a GUI.&lt;/p&gt;

&lt;p&gt;Python web scraping is effective when done using PhantomJS. You can programmatically control PhantomJS to browse websites, interact with components, and extract data with frameworks like Selenium or Puppeteer. Here is an illustration of how to scrape data of countries and their populations using Python and PhantomJS:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
from selenium.webdriver.common.by import By

# Configure the PhantomJS WebDriver
driver = webdriver.PhantomJS()

# Navigate to a web page
driver.get('https://www.worldometers.info/geography/alphabetical-list-of-countries/')

rows = driver.find_elements(By.TAG_NAME,"tr")

# Initialize a list to store the countries and populations
countries_populations = []

# Iterate over the rows of the table
for row in rows:
    data = row.text.split(" ") # splitting the data 

   # Add the country and population to the list
    countries_populations.append((data))

for countries_population in countries_populations:
    if len(countries_population) == 5:
        print(f"{countries_population[1]}: {countries_population[2]}")

# Close the WebDriver
driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2932%2F0%2Ah0LBtQKgTStauv0L.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2932%2F0%2Ah0LBtQKgTStauv0L.png" width="800" height="392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2At-0LcwUIF5FzDUSD.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2At-0LcwUIF5FzDUSD.png" width="800" height="532"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Discover 31 &lt;a href="https://www.lambdatest.com/blog/automation-testing-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_22&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;top software testing tools&lt;/a&gt; in 2023 for powerful and efficient testing solutions.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Advantages of PhantomJS
&lt;/h2&gt;

&lt;p&gt;There are various benefits of using PhantomJS which are going to cover in this section like Headless nature, Cross-platform compatibility, and many more.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Headless nature:&lt;/strong&gt; PhantomJS is suitable for server-side automation and tasks where a visible browser window is not required due to its headless nature, which functions without a GUI.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Cross-platform compatibility:&lt;/strong&gt; PhantomJS supports Windows, macOS, Linux, and FreeBSD, making it cross-platform compatible and enabling developers to use it across numerous platforms.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Support for JavaScript:&lt;/strong&gt; PhantomJS comes with a JavaScript engine that allows it to handle and run JavaScript scripts on web pages. This makes it practical for scraping web pages that use a lot of JavaScript or dynamic information.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Network monitoring:&lt;/strong&gt; Network traffic analysis is possible with PhantomJS, which is useful for performance evaluation, troubleshooting, or recording HTTP requests and responses for site scraping.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Screenshot capture:&lt;/strong&gt; PhantomJS can render websites and take screenshots, making it handy for visual testing, creating thumbnails for websites, or storing screenshots of websites when scraping.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Disadvantages of PhantomJS
&lt;/h2&gt;

&lt;p&gt;Some drawbacks related to Development discontinued, Performance, limited ecosystem support, and Data technology are going to cover in this section in detail.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Development discontinued:&lt;/strong&gt; PhantomJS’s development has been stopped since 2017; hence there may not be any further security patches or bug fixes. The reason behind its depreciation is that the community needs to maintain it actively. Compatibility problems with more modern web technologies such as HTML5, CSS3, ES6+ JavaScript, and potential security holes also resulted in discontinuation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Performance:&lt;/strong&gt; PhantomJS executed rather quickly, although its page rendering and execution times might be slower than those of other headless browsers like Chrome or Firefox. This may affect the general effectiveness of automated tasks. We are going to see this in the next &lt;a href="https://www.lambdatest.com/blog/phantomjs-python/#Comparing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_22&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;section&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;**Limited ecosystem support: **The ecosystem and community support for PhantomJS may be significantly smaller than those for competing headless browsers. As a result, developers will have less access to tools, guides, and community-driven upgrades.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dated technology:&lt;/strong&gt; PhantomJS may not support more recent web standards, APIs, or functionality because development on it has ceased. This may reduce its ability to work with current websites and web applications.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Comparing Test Time Execution of Different Headless Browsers
&lt;/h2&gt;

&lt;p&gt;There are various options available, like Chrome, Firefox, and Microsoft Edge, which provide a better performance, development tools, and many more. In this section, we will see how to use them and compare the execution speed of the script with the execution speed of PhantomJS.&lt;/p&gt;

&lt;p&gt;For the execution speed comparison, we will run the below test on different browsers.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def scrape_table_data():
    start = time.time()
    driver.get('https://www.lambdatest.com/selenium-playground/table-sort-search-demo')

    search_bar = driver.find_element(By.XPATH, '//*[@id="example_filter"]/label/input')
    search_bar.send_keys("London")

    rows = driver.find_elements(By.TAG_NAME, "tr")

    data_list = []
    for row in rows:
        data = row.text
        data_list.append(data)
    driver.quit()
    end = time.time()

    execution_time = (end - start) * 10 ** 3
    return execution_time
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AcAAEaijgWoWRR1Ll.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AcAAEaijgWoWRR1Ll.png" width="800" height="464"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  PhantomJS
&lt;/h2&gt;

&lt;p&gt;The below code will run the test on PhantomJS and the functions return the execution time which will be printed in milliseconds.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
driver = webdriver.PhantomJS()
execution_time = scrape_table_data()
print(f"Time taken PhantomJS Browser: {execution_time:.03f}ms")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Note: we are using Selenium 3.8.0 to use the PhantomJS browser.&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2688%2F0%2ACoJ_M26ogTbspS1L.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2688%2F0%2ACoJ_M26ogTbspS1L.png" width="800" height="47"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;This article sheds light on the best &lt;a href="https://www.lambdatest.com/blog/cross-browser-testing-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_22&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;cross browser testing tools&lt;/a&gt; to look for in 2023.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Chrome
&lt;/h2&gt;

&lt;p&gt;To run the test on the Chrome browser in the headless mode you are required to add &lt;strong&gt;&lt;em&gt;–headless=new&lt;/em&gt;&lt;/strong&gt; using the &lt;em&gt;add_argument()&lt;/em&gt; method in the Options.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2220%2F0%2A_he5jtCJCM11DLAT.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2220%2F0%2A_he5jtCJCM11DLAT.png" width="800" height="314"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: It is recommended to use the latest version of the Selenium. While executing the code Selenium 3.11.2 is used.&lt;/p&gt;

&lt;p&gt;The below code will run the test on the Chrome browser in headless mode.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
options = ChromeOptions()
options.add_argument('--headless=new')
driver = webdriver.Chrome(options=options)
execution_time = scrape_table_data()
print(f"Time taken Chrome Browser: {execution_time:.03f}ms")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2736%2F0%2AZFcwofTYINUJUzMY.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2736%2F0%2AZFcwofTYINUJUzMY.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Firefox
&lt;/h2&gt;

&lt;p&gt;To run the test on the Firefox browser in the headless mode you are required to add &lt;strong&gt;&lt;em&gt;–headless&lt;/em&gt;&lt;/strong&gt; using the add_argument() method in the Options.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2260%2F0%2AmRe3cB2bRe8E6dXf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2260%2F0%2AmRe3cB2bRe8E6dXf.png" width="800" height="308"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note: It is recommended to use the latest version of the Selenium. While executing the code &lt;strong&gt;&lt;em&gt;Selenium 3.11.2&lt;/em&gt;&lt;/strong&gt; is used.&lt;/p&gt;

&lt;p&gt;The below code will run the test on the Firefox browser in headless mode.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
from selenium.webdriver.firefox.options import Options as FirefoxOptions
options = FirefoxOptions()
options.add_argument('--headless')
driver = webdriver.Firefox(options=options)
execution_time = scrape_table_data()
print(f"Time taken Firefox Browser: {execution_time:.03f}ms")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2700%2F0%2APwjCP0Mbr-OKxdzs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2700%2F0%2APwjCP0Mbr-OKxdzs.png" width="800" height="47"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Discover the top 20 &lt;a href="https://www.lambdatest.com/blog/mobile-app-testing-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_22&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;mobile testing tools&lt;/a&gt; for 2023 in this informative blog post. Stay up-to-date with the latest advancements and choose the perfect tool to meet your app testing requirements.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Microsoft Edge
&lt;/h2&gt;

&lt;p&gt;To run the test on the Microsoft Edge browser in the headless mode you are required to add –headless using the add_argument() method in the Options.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2144%2F0%2AMUSU3rVZ2d-DZbma.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2144%2F0%2AMUSU3rVZ2d-DZbma.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: It is recommended to use the latest version of the Selenium. While executing the code **Selenium 3.11.2 **is used.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The below code will run the test on the Microsoft Edge browser in headless mode.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
from selenium.webdriver.edge.options import Options as EdgeOptions
options = EdgeOptions()
options.add_argument('--headless')
driver = webdriver.Edge(options=options)
execution_time = scrape_table_data()
print(f"Time taken Edge Browser: {execution_time:.03f}ms")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2744%2F0%2AEL56oL-z1f86YUn2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2744%2F0%2AEL56oL-z1f86YUn2.png" width="800" height="48"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AQIBFFwOLz88g_vHutGo4kA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AQIBFFwOLz88g_vHutGo4kA.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the above table, it can be concluded that the Chrome browser executes the test script most efficiently. Though Firefox is not left behind in the race it equally performs well.&lt;/p&gt;

&lt;p&gt;Clone the PhantomJSInPython GitHub repository to run the Python files mentioned in the blog.&lt;/p&gt;

&lt;p&gt;Apart from headless browsers, several cloud grid options offer the functionality to run tests without utilizing the system’s resources. One prominent platform in this domain is &lt;a href="https://www.lambdatest.com/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_22&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;LambdaTest&lt;/a&gt;, an AI-Powered Test Orchestration platform, set up as a cloud infrastructure. It provides a comprehensive suite of features, including &lt;a href="https://www.lambdatest.com/blog/automation-testing-tutorial/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_22&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Automation Testing&lt;/a&gt;, &lt;a href="https://www.youtube.com/watch?v=sOaQ-ttEBzs?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_22&amp;amp;utm_term=bh&amp;amp;utm_content=youtube" rel="noopener noreferrer"&gt;Smart UI Testing&lt;/a&gt;, and Screenshots, all delivered optimally.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Discover 31 top &lt;a href="https://www.lambdatest.com/blog/automation-testing-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_22&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;QA automation tools&lt;/a&gt; in 2023 for powerful and efficient testing solutions.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The main advantages of using Cloud Grid like LambdaTest are as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Ease of Use:&lt;/strong&gt; LambdaTest is simple to use and has a user-friendly interface, making it simple to prepare and run tests without complicated settings or technical expertise.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Cloud Grid architecture:&lt;/strong&gt; LambdaTest offers a cloud-based grid architecture for executing tests across numerous operating systems, browsers, and versions, ensuring thorough test coverage.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;**Scalability: **LambdaTest makes it simple to scale testing efforts by giving users access to a huge variety of real browsers and devices, allowing for parallel testing, and shortening testing times in general.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Optimized Testing:&lt;/strong&gt; By utilizing cloud resources, LambdaTest does not require the installation or upkeep of local browsers, conserving system resources and guaranteeing consistent test findings in various circumstances.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Smart UI Testing:&lt;/strong&gt; A consistent user experience is ensured by LambdaTest’s Smart UI Testing features, which allow for the identification of visual regression problems and design inconsistencies by comparing screenshots of applications across browsers and devices.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Hassle-free Integration:&lt;/strong&gt; Through its API, CLI, and browser extensions, LambdaTest easily interfaces with well-known automation frameworks and CI/CD technologies, making it simple to include in current development and testing workflows.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;PhantomJS is a powerful headless browser that offers a range of capabilities for automation testing and web scraping. Integrating PhantomJS with Python allows developers to harness its functionalities and leverage its potential in their projects.&lt;/p&gt;

&lt;p&gt;We saw How to install PhantomJS in Python in a step-by-step process, including the installation of Python itself, the setup of PhantomJS, and the installation of Selenium for Python. Verifying the installation can be done through a simple Python script that utilizes PhantomJS.&lt;/p&gt;

&lt;p&gt;PhantomJS provides numerous advantages, such as its ability to execute automated tests, scrape websites, and support various programming languages. However, it also has some disadvantages, including its declining support and limited compatibility with newer web technologies.&lt;/p&gt;

&lt;p&gt;Moreover, PhantomJS can be employed in real-world scenarios, such as data extraction and mining, as well as web application testing. These applications showcase the practical use cases and examples of utilizing PhantomJS effectively.&lt;/p&gt;

&lt;p&gt;In conclusion, PhantomJS, when integrated with Python, provides a robust solution for automation testing and web scraping. While it has its advantages and disadvantages, understanding its features and optimizing its performance can lead to successful implementation. By leveraging the capabilities of PhantomJS, developers can enhance their projects and achieve their desired outcomes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions (FAQs)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Is there any community or support available for PhantomJS?
&lt;/h3&gt;

&lt;p&gt;There was support and an active community for PhantomJS. PhantomJS has been deprecated since 2017, and its development has been halted since then. The official PhantomJS website urges users to switch to other programs like Puppeteer or Headless Chrome.&lt;/p&gt;

&lt;h3&gt;
  
  
  Are there any performance considerations when using PhantomJS in Python?
&lt;/h3&gt;

&lt;p&gt;There are a few performance issues to remember when using PhantomJS in Python. PhantomJS executes in a separate process, adding extra overhead. Additionally, there may be some latency while using PhantomJS’s API. Consider alternatives like Puppeteer or Headless Chrome, which offer greater performance and more active development, if performance is a crucial concern.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can PhantomJS be used with many OS systems?
&lt;/h3&gt;

&lt;p&gt;Yes, a variety of operating systems can use PhantomJS. It is intended to work with various operating systems, including Windows, macOS, Linux, and FreeBSD. Depending on their requirements and preferences, customers can use PhantomJS on various operating systems thanks to this cross-platform support.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can I run PhantomJS in parallel or distributed setups?
&lt;/h3&gt;

&lt;p&gt;Yes, PhantomJS may be used in distributed or parallel configurations. You can accomplish this by simultaneously coordinating the execution of several PhantomJS instances deployed across several computers or virtual environments. This enables improved performance and scalability when managing complicated operations or answering numerous requests simultaneously. The use of alternative tools with built-in support for parallel or distributed execution, such as Puppeteer or Headless Chrome, is advised because PhantomJS has been deprecated and replaced.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Looking to manage your tests more efficiently? In this blog, explore a variety of &lt;a href="https://www.lambdatest.com/blog/best-test-management-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=jan_22&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;test management tools&lt;/a&gt; and select the one that suits your team’s needs.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>python</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Dive Into Headless Chrome: Your Key to Efficient Web Development</title>
      <dc:creator>jainishpatel78</dc:creator>
      <pubDate>Mon, 18 Dec 2023 06:41:16 +0000</pubDate>
      <link>https://forem.com/testmuai/dive-into-headless-chrome-your-key-to-efficient-web-development-30g0</link>
      <guid>https://forem.com/testmuai/dive-into-headless-chrome-your-key-to-efficient-web-development-30g0</guid>
      <description>&lt;p&gt;Numerous technologies and tools have been developed as a result of the ever-expanding internet that is intended to make web creation and test automation less complicated. Headless Chrome is one of the most noteworthy tools in this area. This headless version of the popular Google Chrome browser works without a visible window and gives programmers and developers a superb way to interact with web information programmatically.&lt;/p&gt;

&lt;p&gt;In this article, we will explore key topics concerning Headless Chrome, such as detecting the headless mode, taking screenshots of the browser window, working with userAgent, printing DOM, handling cookies, and running tests on Cloud Grid.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Streamline mobile app development across iOS, Android, Windows, and macOS with &lt;a href="https://www.lambdatest.com/support/docs/app-automation-using-app-center/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=support_doc" rel="noopener noreferrer"&gt;Visual Studio App Center&lt;/a&gt;. It’s an all-in-one platform that consolidates services like building, testing, distribution, monitoring, diagnostics, and more into a unified cloud solution. Plus, it simplifies the app upload process, reducing manual effort.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What is Headless Chrome?
&lt;/h2&gt;

&lt;p&gt;The Google Chrome web browser has a headless (or non-graphical) variant called Headless Chrome or Chrome Headless. It was launched with &lt;strong&gt;Chrome 59&lt;/strong&gt;. Running Chrome essentially without Chrome! It enables programmatic and automated usage of Chrome’s features without presenting a Graphical User Interface (GUI). It provides to the command line all of the contemporary web platform capabilities offered by &lt;em&gt;Chromium&lt;/em&gt; and the &lt;em&gt;Blink&lt;/em&gt; rendering engine.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common Use Cases of Headless Chrome?
&lt;/h2&gt;

&lt;p&gt;Headless Chrome is mostly used for data extraction, data scraping, &lt;a href="https://www.lambdatest.com/learning-hub/software-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;software testing&lt;/a&gt;, and web automation. It ensures functioning and spots regressions. In &lt;a href="https://www.lambdatest.com/automation-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;automation testing&lt;/a&gt;, it serves a dual purpose: &lt;em&gt;ensuring the correctness of operations and promptly identifying any regressions&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;It also provides programmers with the means to engage in &lt;a href="https://www.lambdatest.com/blog/web-scraping-with-javascript-and-selenium/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;web scraping,&lt;/a&gt; effectively extracting data from websites. Simultaneously, it simplifies activities like form-filling and the generation of PDFs through &lt;a href="https://www.lambdatest.com/learning-hub/web-automation?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;web automation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.lambdatest.com/online-browser-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Cross browser testing &lt;/a&gt;is made easier by assisting developers to ensure browser compatibility. Remote access to Chrome DevTools allows for debugging. Apart from this, it is used by security experts to examine the security of web apps, and web performance. It is ideally suited for server-side and automated processes because it can carry out these functions without the need for a graphical user interface.&lt;/p&gt;

&lt;h2&gt;
  
  
  Different Versions of Headless Chrome
&lt;/h2&gt;

&lt;p&gt;A new mode was added in Chrome version 96 along with the established &lt;em&gt;**— headless *&lt;/em&gt;&lt;em&gt;option. From versions 96 to 108, this fresh mode was known as *&lt;/em&gt;&lt;em&gt;— headless=chrome&lt;/em&gt;&lt;strong&gt;, and from versions 109 onwards, it was known as ***— headless=new&lt;/strong&gt;*. Users have complete access to all browser features in this mode, including the ability to use extensions. This improvement goes above and beyond what the standard headless mode can do, giving users and developers additional freedom to use Chrome’s robust features while operating in a headless environment.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Discover 31 &lt;a href="https://www.lambdatest.com/blog/automation-testing-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Top Software Testing Tools&lt;/a&gt; in 2023 for powerful and efficient testing solutions.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Working of Chrome — headless=new Mode
&lt;/h2&gt;

&lt;p&gt;Before we delve into understanding &lt;strong&gt;— — *headless=new&lt;/strong&gt;* mode, it’s essential to grasp the working of &lt;strong&gt;&lt;em&gt;— headless&lt;/em&gt;&lt;/strong&gt;. From the implementation of Headless Chrome, it can easily be misinterpreted that the Headless Chrome and regular Chrome browser are the same. Interestingly, this was only partially true.&lt;/p&gt;

&lt;p&gt;The codebase of Headless Chrome didn’t share any of the Chrome browser code located in &lt;strong&gt;&lt;em&gt;//chrome&lt;/em&gt;&lt;/strong&gt;. However, Headless mode introduced some unique bugs and features that are not found in regular Chrome. This sometimes creates a scenario where a test case is working correctly in one mode but fails in the other. It also slowed automation testing performance involving browser extensions or other browser-level functions without dedicated Headless support.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AL_MK9P3IsD11zgCs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AL_MK9P3IsD11zgCs.png" width="628" height="671"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In 2021, the Chrome development team finally integrated the Headless and Headful modes in version 112. In this mode, Chrome starts but doesn’t display any platform windows. However, all existing functionalities and future updates are available with no restrictions.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Run Headless Chrome in Selenium?
&lt;/h2&gt;

&lt;p&gt;In this section, we are going to cover the installation process for Chrome Headless.&lt;/p&gt;

&lt;p&gt;There are a few prerequisites for using Headless Chrome:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The latest version of Python 3.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Chrome Browser.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Newer version of &lt;a href="https://www.lambdatest.com/learning-hub/selenium-4?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Selenium 4&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can follow a detailed blog on &lt;a href="https://www.lambdatest.com/blog/python-selenium-with-chrome/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Python Selenium On Chrome&lt;/a&gt; if you are facing difficulties in installing any of the prerequisites.&lt;/p&gt;

&lt;p&gt;Headless Chrome is enabled using &lt;em&gt;ChromeOptions()&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AxDB0FYucN_xBY_qa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AxDB0FYucN_xBY_qa.png" width="800" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Discover the game-changing power of 31 leading &lt;a href="https://www.lambdatest.com/blog/automation-testing-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;QA automation tools&lt;/a&gt; in 2023 — optimize your testing for unparalleled efficiency and effectiveness.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Basic Headless Chrome Techniques
&lt;/h2&gt;

&lt;p&gt;This section covers some basic functionalities like detecting Headless Chrome, getting the title of the website, taking screenshots, and many more.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Detect Headless Chrome in Selenium
&lt;/h2&gt;

&lt;p&gt;You can use &lt;strong&gt;userAgent&lt;/strong&gt; to detect whether Chrome is running in Headless mode or not. A web browser or other client software transmits a &lt;em&gt;userAgent&lt;/em&gt; string to a web server with each request to identify itself and its capabilities. The user agent string includes details that can be used to determine the client, such as the operating system, device type, and browser version.&lt;/p&gt;

&lt;p&gt;The below code shows the use of &lt;strong&gt;userAgent&lt;/strong&gt; for detection.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions

options = ChromeOptions()
options.add_argument("--headless=new")

driver = webdriver.Chrome(options=options)

isHeadless = driver.execute_script("return navigator.userAgent;")
substr = "HeadlessChrome"

print(isHeadless)
if substr.lower() in isHeadless.lower():
    print("Headless Chrome detected")
else:
    print("Non-Headless Chrome detected")

driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2612%2F0%2A86y1lMpGKjDSdzxQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2612%2F0%2A86y1lMpGKjDSdzxQ.png" width="800" height="109"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting the Title of the Website
&lt;/h2&gt;

&lt;p&gt;The title of the website can be used to verify that the website is accessible to users and also verify that the website is loading the correct page.&lt;/p&gt;

&lt;p&gt;The below code shows how to get the &lt;strong&gt;title&lt;/strong&gt; and check it using assertion.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions

# Create a new ChromeOptions object.
options = ChromeOptions()

# Set the headless mode.
options.add_argument("--headless=new")

# Create a new ChromeDriver object, passing in the ChromeOptions object.
driver = webdriver.Chrome(options=options)

# Navigate to the desired website.
driver.get("https://ecommerce-playground.lambdatest.io/")

# Print the title of the page.
print("Title of the page is: " + driver.title)

# Assert that the title of the page contains the text "Your Store".
assert "Your Store" in driver.title

# Close the browser.
driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A9AONFBpz9J2M2cqt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2A9AONFBpz9J2M2cqt.png" width="800" height="66"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;title&lt;/em&gt; method is used to get the title of the website. &lt;strong&gt;Assert&lt;/strong&gt; is used for verifying or validating the website title. &lt;em&gt;Learn &lt;a href="https://www.lambdatest.com/blog/assert-and-verify-in-selenium-webdriver/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;How To Use Assert and Verify in Selenium WebDriver&lt;/a&gt; for accurate validation of website titles.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2376%2F0%2AFTqPsjspdnBpgEsc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2376%2F0%2AFTqPsjspdnBpgEsc.png" width="800" height="293"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Taking a Screenshot of a Window
&lt;/h2&gt;

&lt;p&gt;The screenshot ensures that while performing automation tests, we can have a visual record of the page’s state in case of any errors.&lt;/p&gt;

&lt;p&gt;The below code shows how to use the &lt;em&gt;save_screenshot()&lt;/em&gt; method.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions

options = ChromeOptions()
options.add_argument("--headless=new")

driver = webdriver.Chrome(options=options)

driver.get("https://ecommerce-playground.lambdatest.io/")

# Save a screenshot of the current page to the file "screenshot.png".
driver.save_screenshot("screenshot.png")
driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Taken Screenshot:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AsWc_7S22zpvsvmgT.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AsWc_7S22zpvsvmgT.png" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Selenium by default provides a method to record screenshots via the &lt;em&gt;save_screenshot()&lt;/em&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2144%2F0%2A5_7yzXt1Q5AN_ZyH.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2144%2F0%2A5_7yzXt1Q5AN_ZyH.png" width="800" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Advanced Headless Chrome Techniques
&lt;/h2&gt;

&lt;p&gt;For developers, testers, and QAs, advanced techniques in Headless Chrome open up a world of customization possibilities. We’ll look at some of the methods in this section, like customizing userAgent, handling cookies, printing &lt;a href="https://www.lambdatest.com/blog/document-object-model/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;Document Object Model&lt;/a&gt; (DOM), generating PDFs, and working with ActionChains.&lt;/p&gt;

&lt;h2&gt;
  
  
  Customizing userAgent
&lt;/h2&gt;

&lt;p&gt;Customizing the userAgent serves multiple purposes, such as debugging web applications, testing web applications across various browser environments, enabling web scraping without triggering anti-scraping measures, and accessing websites that may be restricted based on geographical location or device types.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions

# Define your custom user agent
custom_user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/116.0.0.0 Safari/537.36"

options = ChromeOptions()
options.add_argument("--headless=new")
options.add_argument(f"--user-agent={custom_user_agent}")  # Set custom user agent

driver = webdriver.Chrome(options=options)

user_agent = driver.execute_script("return navigator.userAgent;")
# Print the user agent string.
print(user_agent)
driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2620%2F0%2AMoDcA2m_TZLq6kXh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2620%2F0%2AMoDcA2m_TZLq6kXh.png" width="800" height="152"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;This guide offers a complete overview of the various &lt;a href="https://www.lambdatest.com/learning-hub/web-automation-tools?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;web automation tools&lt;/a&gt; available and how to choose the right tool for your specific testing needs.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Handling Authentication and Cookies
&lt;/h2&gt;

&lt;p&gt;While performing &lt;a href="https://www.lambdatest.com/learning-hub/web-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;web testing&lt;/a&gt;, it becomes crucial to handle and use cookies related to authentication or maintaining session data. Selenium provides various functions like &lt;strong&gt;&lt;em&gt;add_cookie(), get_cookie(), get_cookies()&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;delete_cookie()&lt;/em&gt;&lt;/strong&gt; to work with cookies.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions

options = ChromeOptions()
options.add_argument("--headless=new")
driver = webdriver.Chrome(options=options)

driver.get("https://ecommerce-playground.lambdatest.io/")

driver.add_cookie({"name": "rohan", "value": "user1234"})
print(driver.get_cookie("rohan"))
driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2972%2F0%2AjcjclQgjO2cxLI8n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2972%2F0%2AjcjclQgjO2cxLI8n.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Capturing Screenshots
&lt;/h2&gt;

&lt;p&gt;Taking snapshots of different HTML elements like div, span, a, and many more while performing tests helps to figure out issues related to UI and rendering data.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions

options = ChromeOptions()
options.add_argument("--headless=new")
driver = webdriver.Chrome(options=options)

driver.get("https://ecommerce-playground.lambdatest.io/")

element = driver.find_element(By.XPATH,'//*[@id="mz-carousel-213240"]/div/div[1]/a/img')

element.screenshot('screenshot.png')

driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Taken Screenshot:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2368%2F0%2A5L7k3KjUrncPEClm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2368%2F0%2A5L7k3KjUrncPEClm.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Printing the DOM
&lt;/h2&gt;

&lt;p&gt;Printing Document Object Model (DOM) helps evaluate a web page’s structure, which is essential for debugging and guaranteeing data consistency. You can inspect the page’s structure and spot any irregularities in the data retrieving process by printing the DOM.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.common.by import By
import time
options = ChromeOptions()
options.add_argument("--headless=new")
driver = webdriver.Chrome(options=options)

driver.get("https://ecommerce-playground.lambdatest.io/")

print(driver.page_source)

# or

html = driver.execute_script("return document.documentElement.outerHTML;")

# Save the DOM to a file.
with open("dom.html", "w") as f:
    f.write(html)
driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2576%2F0%2AJ-dUQoToVcPD0AyH.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2576%2F0%2AJ-dUQoToVcPD0AyH.png" width="800" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;File Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Ayv7or4BWucBcMa6D.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2Ayv7or4BWucBcMa6D.png" width="800" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Looking to manage your tests more efficiently? In this blog, explore a variety of &lt;a href="https://www.lambdatest.com/blog/best-test-management-tools/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=blog" rel="noopener noreferrer"&gt;test management tools&lt;/a&gt; and select the one that suits your team’s needs.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Generating PDFs
&lt;/h2&gt;

&lt;p&gt;Generating PDFs is a handy method for archiving web content and verifying that web pages are printable. There are various ways to generate a PDF:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using the Selenium&lt;/strong&gt; &lt;em&gt;print_page()&lt;/em&gt; &lt;strong&gt;method&lt;/strong&gt;:&lt;br&gt;
The &lt;em&gt;print_page()&lt;/em&gt; prints the current page within the browser. Read more about it from the &lt;a href="https://www.selenium.dev/documentation/webdriver/interactions/windows/#print-page" rel="noopener noreferrer"&gt;official docs of selenium&lt;/a&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.common.print_page_options import PrintOptions
import base64
print_options = PrintOptions()
print_options.page_ranges = ['1-2']

options = ChromeOptions()
options.add_argument('--headless=new')

driver = webdriver.Chrome(options=options)
driver.get("https://ecommerce-playground.lambdatest.io/")

base64code = driver.print_page(print_options)
# decode the base64 string to bytes
pdf_bytes = base64.b64decode(base64code)

# write the bytes to a file with a .pdf extension
with open('output.pdf', 'wb') as f:
    f.write(pdf_bytes)
driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AIPtPYynw4ZRBiP4s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2AIPtPYynw4ZRBiP4s.png" width="446" height="304"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A5ypg5OfPyuhOlyOa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A5ypg5OfPyuhOlyOa.png" width="800" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Chrome DevTools Protocols:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Chrome DevTools Protocols (CDP commands) allow you to interact with and control Google Chrome or Chromium-based browsers programmatically. You can use CDP commands to perform various tasks, such as navigating to web pages, inspecting the DOM, capturing screenshots, and more. Here’s an overview of how to use CDP commands with the help of the &lt;em&gt;execute_cdp_cmd() method&lt;/em&gt; provided by Selenium:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
import base64


options = ChromeOptions()
options.add_argument('--headless=new')

driver = webdriver.Chrome(options=options)
driver.get("https://ecommerce-playground.lambdatest.io/")

# Execute the CDP command to print the page to PDF.
pdf_data = driver.execute_cdp_cmd("Page.printToPDF", {"path": 'html-page.pdf', "format": 'A4'})

# Decode the PDF data and write it to a file.
with open('cdp_output.pdf', 'wb') as f:
    f.write(base64.b64decode(pdf_data['data']))

driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Taken Screenshot:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AZ2oVdA3YnDdwbhNz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AZ2oVdA3YnDdwbhNz.png" width="800" height="379"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Capture, Create, and Recreate Actions
&lt;/h2&gt;

&lt;p&gt;Automating user interactions with Headless Chrome allows you to create &lt;a href="https://www.lambdatest.com/learning-hub/react-end-to-end-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;end-to-end test scripts&lt;/a&gt;, replicating real user behavior.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Capture:&lt;/strong&gt; Use &lt;em&gt;ActionChains&lt;/em&gt; to record user actions like mouse movements and clicks; call the &lt;em&gt;perform()&lt;/em&gt; method to execute them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Create:&lt;/strong&gt; Make a new &lt;em&gt;ActionChains&lt;/em&gt; object, add user actions, and call the &lt;em&gt;perform()&lt;/em&gt; method to execute them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Recreate:&lt;/strong&gt; To repeat captured user actions, create new &lt;em&gt;ActionChains&lt;/em&gt;, add recorded actions, and call &lt;em&gt;perform()&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;from selenium import webdriver&lt;br&gt;
from selenium.webdriver.chrome.options import Options as ChromeOptions&lt;br&gt;
from selenium.webdriver.common.by import By&lt;br&gt;
from selenium.webdriver.common.action_chains import ActionChains&lt;/p&gt;

&lt;p&gt;options = ChromeOptions()&lt;br&gt;
options.add_argument('--headless=new')&lt;/p&gt;

&lt;p&gt;driver = webdriver.Chrome(options=options)&lt;/p&gt;

&lt;p&gt;driver.get("&lt;a href="https://ecommerce-playground.lambdatest.io/%22" rel="noopener noreferrer"&gt;https://ecommerce-playground.lambdatest.io/"&lt;/a&gt;)&lt;/p&gt;
&lt;h1&gt;
  
  
  Set the window size to 1200x760
&lt;/h1&gt;

&lt;p&gt;driver.set_window_size(1200, 760)&lt;/p&gt;
&lt;h1&gt;
  
  
  Find the element to interact with
&lt;/h1&gt;

&lt;p&gt;element = driver.find_element(By.CSS_SELECTOR, ".active &amp;gt; .d-block &amp;gt; .d-block")&lt;/p&gt;
&lt;h1&gt;
  
  
  Create a new ActionChains object
&lt;/h1&gt;

&lt;p&gt;actions = ActionChains(driver)&lt;/p&gt;
&lt;h1&gt;
  
  
  Move the mouse to the element and click and hold
&lt;/h1&gt;

&lt;p&gt;actions.move_to_element(element).click_and_hold().perform()&lt;/p&gt;

&lt;p&gt;driver.save_screenshot('action1.png')&lt;/p&gt;
&lt;h1&gt;
  
  
  Release the mouse click
&lt;/h1&gt;

&lt;p&gt;actions.move_to_element(element).release().perform()&lt;/p&gt;
&lt;h1&gt;
  
  
  Click on the dropdown toggle
&lt;/h1&gt;

&lt;p&gt;driver.find_element(By.CSS_SELECTOR, ".dropdown-toggle &amp;gt; .info:nth-child(2) &amp;gt; .title").click()&lt;/p&gt;

&lt;p&gt;driver.save_screenshot('action2.png')&lt;/p&gt;

&lt;p&gt;driver.quit()&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Action1 Image Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A3roESzrudvybb0LI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A3roESzrudvybb0LI.png" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Action 2 Image Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ABYSCZ4pmH_aoWhOH.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ABYSCZ4pmH_aoWhOH.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Retrieving Performance Logs from Chrome Headless
&lt;/h2&gt;

&lt;p&gt;It can be retrieved using &lt;em&gt;execute_cdp_cmd()&lt;/em&gt; in Selenium. Performance logs are essential because they provide information about how a web application is being used, and help locate and fix performance issues, resulting in faster loading of web applications and ultimately better user experiences.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions

options = ChromeOptions()
options.add_argument('--headless=new')

driver = webdriver.Chrome(options=options)
driver.get("https://ecommerce-playground.lambdatest.io/")

driver.execute_cdp_cmd('Performance.enable', {})
t = driver.execute_cdp_cmd('Performance.getMetrics', {})
print(t)

driver.quit()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Console Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2APhfh2xhSLI35AtQh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2APhfh2xhSLI35AtQh.png" width="800" height="172"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Debug in Headless Chrome?
&lt;/h2&gt;

&lt;p&gt;The DevTools protocol is activated when Chrome is run with the &lt;strong&gt;&lt;em&gt;— remote-debugging-port=9222&lt;/em&gt;&lt;/strong&gt; option. DevTools protocol allows Chrome and the headless browser to communicate with one another. Tools like Sublime, Visual Studio Code, and Node.js also use this protocol for remote debugging.&lt;/p&gt;

&lt;p&gt;Since there isn’t a visible browser window in headless mode, you may see if it’s functioning by using another web browser and going to &lt;a href="http://localhost:9222." rel="noopener noreferrer"&gt;&lt;strong&gt;&lt;em&gt;http://localhost:9222&lt;/em&gt;&lt;/strong&gt;.&lt;/a&gt; This will provide a list of pages that you may examine to see how the headless browser renders different pages.&lt;/p&gt;

&lt;p&gt;You can analyze, debug, and modify the web page using the same DevTools interface seen on this page, just as you would with a standard browser. This website also functions as a helpful debugging tool for viewing all the underlying commands that the DevTools protocol is delivering to the browser if you’re utilizing headless mode in your code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ARpBWxUQ1tlkb-YIf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2ARpBWxUQ1tlkb-YIf.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Headless Chrome on Cloud Grid
&lt;/h2&gt;

&lt;p&gt;When it comes to running a series of test scripts to guarantee website compatibility across multiple versions of Chrome, a problem arises. In our current environment, where Chrome version 118 is the only version available, the implementation of test scripts for Chrome version 114 would lead to code errors. This is where the use of Cloud Grid comes into play.&lt;/p&gt;

&lt;p&gt;A cloud grid gives users access to various real and virtual machines, each with its configuration, operating system, and web browser. This guarantees accurate testing in various situations, an essential component of reliable software development. Additionally, Cloud providers typically offer high availability and redundancy, reducing the likelihood of downtime during testing.&lt;/p&gt;

&lt;p&gt;LambdaTest is a &lt;a href="https://www.lambdatest.com/selenium?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Selenium&lt;/a&gt; based cloud Grid and &lt;a href="https://www.lambdatest.com/online-browser-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;cross browser testing&lt;/a&gt; platform for web and mobile applications. It supports tests across 3000+ combinations of browsers, real devices, and OS to offer &lt;a href="https://www.lambdatest.com/learning-hub/cross-browser-compatibility?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;cross-browser compatibility&lt;/a&gt;. This empowers you to establish testing procedures that not only meet your specific requirements but also expand in scale as your project evolves.&lt;/p&gt;

&lt;p&gt;In this section, we will run a test on LambdaTest. We will run our test on the &lt;strong&gt;macOS Sonoma&lt;/strong&gt; operating system with &lt;strong&gt;Chrome version 118.0&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Before Running a Python test on LambdaTest, follow the simple steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create an account on &lt;a href="https://www.lambdatest.com/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;LambdaTest&lt;/a&gt; and complete all the required processes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to the dashboard by clicking the &lt;strong&gt;&lt;em&gt;dashboard&lt;/em&gt;&lt;/strong&gt; button on the top right corner or Follow this &lt;a href="https://accounts.lambdatest.com/dashboard?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;LambdaTest Dashboard&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Get your credentials from the &lt;em&gt;profile&lt;/em&gt; icon located in the top right corner, and then select the &lt;em&gt;profile&lt;/em&gt; option it will redirect to the profile screen. Select &lt;strong&gt;Password &amp;amp; Security&lt;/strong&gt; here; you can find your &lt;em&gt;Username and Access Key&lt;/em&gt; and save it for future use. Or use this &lt;a href="https://accounts.lambdatest.com/security?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;Profile link&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The below test uses the same code as the &lt;strong&gt;&lt;em&gt;Capture, Create, and Recreate Actions&lt;/em&gt;&lt;/strong&gt; section.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

username = "username"
access_key = "access_key"


# Capabilities define the OS, Browser name, and other necessary details
lt_options = {
    "user": username,
    "accessKey": access_key,
    "build": "Build for Headless Chrome",
    "name": "Test 1",
    "platformName": "macOS Sonoma",
    "headless": True, # Headless Chrome
    "video": True,
    "w3c": True,  
    "browserName": "Chrome",
    "browserVersion": "118.0",
}

def test_for_headless_on_cloud_gird():
    # To run the test on the platform
    remote_url = "@hub.lambdatest.com/wd/hub"&amp;gt;http://{}:{}@hub.lambdatest.com/wd/hub".format(username, access_key)
    browser_options = ChromeOptions()

    # Adding the capability to the chrome
    browser_options.set_capability('LT:Options', lt_options)

    driver = webdriver.Remote(command_executor=remote_url, options=browser_options)
    driver.get("https://ecommerce-playground.lambdatest.io/")

    element = driver.find_element(By.CSS_SELECTOR, ".active &amp;gt; .d-block &amp;gt; .d-block")

    actions = ActionChains(driver)

    actions.move_to_element(element).click_and_hold().perform()

    actions.move_to_element(element).release().perform()

    driver.find_element(By.CSS_SELECTOR, ".dropdown-toggle &amp;gt; .info:nth-child(2) &amp;gt; .title").click()

    driver.quit()

if __name__ == "__main__":
    test_for_headless_on_cloud_gird()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Understanding the code step by step:&lt;/p&gt;

&lt;p&gt;Add your credentials here as it will help the lambdaTest to run tests on your account:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Azefp6RoFs1YtjZ7J.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F0%2Azefp6RoFs1YtjZ7J.png" width="782" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Get your desired capabilities that can be generated from the &lt;a href="https://www.lambdatest.com/capabilities-generator/?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=webpage" rel="noopener noreferrer"&gt;capabilities generator&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2216%2F0%2Az4QMaSW3ugf9gEWX.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2216%2F0%2Az4QMaSW3ugf9gEWX.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Find details of your &lt;a href="https://www.lambdatest.com/learning-hub/test-case?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;test case&lt;/a&gt; under &lt;strong&gt;&lt;em&gt;Automation&amp;gt;Builds&lt;/em&gt;&lt;/strong&gt; click on the First test to get details of the implementation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AcU-_zMQn1vlxtkZl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2AcU-_zMQn1vlxtkZl.png" width="800" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Explore various available test details to get a better idea of the platform.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A7BfjFPXr59NBPrm9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F3200%2F0%2A7BfjFPXr59NBPrm9.png" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;In this tutorial, learn what is &lt;a href="https://www.lambdatest.com/learning-hub/regression-testing?utm_source=devto&amp;amp;utm_medium=organic&amp;amp;utm_campaign=dec_18&amp;amp;utm_term=bh&amp;amp;utm_content=learning_hub" rel="noopener noreferrer"&gt;Regression testing&lt;/a&gt;, its importance, types, and how to perform it.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Headless Chrome is a powerful instrument for web scraping and automation testing. It comes in a variety of configurations that are tailored to different use cases, and Selenium integration is like the cherry on the cake.&lt;/p&gt;

&lt;p&gt;Essential functionalities are provided by simple methods, including detecting Headless Chrome, getting web page title, and taking screenshots. Customizing userAgent, managing cookies for authentication or managing sessions, generating website PDFs, and obtaining performance logs all present a multitude of opportunities for more complex use cases in test automation.&lt;/p&gt;

&lt;p&gt;Headless Chrome may simplify your workflows and assist you in achieving your objectives, regardless of whether you are a Developer, Tester, or QA. When successfully utilized, its adaptability and capabilities can greatly improve your web automation initiatives.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently Asked Questions (FAQs)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why to use Headless Chrome?
&lt;/h3&gt;

&lt;p&gt;Headless Chrome is lightweight, it uses fewer resources, making it perfect for server-side operations.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to detect Headless Chrome?
&lt;/h3&gt;

&lt;p&gt;One common method to identify Headless Chrome is by examining its &lt;em&gt;userAgent&lt;/em&gt; string. Headless Chrome &lt;em&gt;userAgent&lt;/em&gt; strings typically include the word “Headless” and may also mention the Chrome version. For &lt;em&gt;example HeadlessChrome/XXX.0.0.0&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to run Chrome Headless in Selenium?
&lt;/h3&gt;

&lt;p&gt;You just need two things installed on your machine Chrome browser and Selenium WebDriver. After that you use the headless chrome by the “–headless=new” as an argument in chrome &lt;em&gt;options&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How fast is Chrome Headless?
&lt;/h3&gt;

&lt;p&gt;Since it doesn’t have to show the content visually, Chrome Headless is often &lt;em&gt;1.5x to 2x&lt;/em&gt; faster than a standard, real Chrome browser. However, speed can vary according to the unique use case and the effectiveness of the scripts or tools being used with Chrome Headless.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does Headless Chrome use regular browser cookies?
&lt;/h3&gt;

&lt;p&gt;Yes, just like Chrome’s standard version, Headless Chrome uses conventional browser cookies. This enables a flawless browsing experience while using automated scripts or performing web testing because when you interact with a website using Headless Chrome, it may save and transfer cookies to retain your session and user-specific data.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
