{"id":27218,"date":"2022-01-31T15:13:40","date_gmt":"2022-01-31T15:13:40","guid":{"rendered":"https:\/\/www.askpython.com\/?p=27218"},"modified":"2023-02-16T19:56:44","modified_gmt":"2023-02-16T19:56:44","slug":"introduction-browser-drivers-selenium","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/introduction-browser-drivers-selenium","title":{"rendered":"Introduction to Browser Drivers in Selenium"},"content":{"rendered":"\n<p>Drivers in Selenium are a vital rather a must for Browser automation. Selenium library in Python or in any other programming language uses the particular Browser&#8217;s driver to control browsers with their actions, add functionality to it, and in all manipulate data (webpage) in it.<\/p>\n\n\n\n<p>Contrary to the most common way of declaring a Browser&#8217;s Driver which is most common, but often may result in incompatibility issues. There are multiple ways (not so common) to install, initialize web drivers in your Python code.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong><em>Recommended read: <a href=\"https:\/\/www.askpython.com\/python-modules\/selenium-introduction-and-setup\" data-type=\"post\" data-id=\"26419\">Python Selenium Introduction and Setup<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"downloading-the-drivers\">Downloading the Drivers<\/h2>\n\n\n\n<p>The foremost point of error in Selenium occurs when the browser&#8217;s driver&#8217;s version doesn&#8217;t match with the browser&#8217;s version, for which compatibility issue arises. So, to start with, you must always use the driver with the same version as that of the Web Browser(Google Chrome, Mozilla Firefox, Apple Safari, or any other) which you intend to use for automation.<\/p>\n\n\n\n<p>Downloading and installing Web Drivers is completely safe because these drivers are maintained by the Official Browser Companies and updated accordingly, for testing and automation purposes of their browser and webpages.<\/p>\n\n\n\n<p>Check with your browser which you intend to automate for its version and download the driver from the below references accordingly:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th class=\"has-text-align-center\" data-align=\"center\">Web Browser<\/th><th class=\"has-text-align-center\" data-align=\"center\">Driver Download Reference<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">Google Chrome\/Chromium<\/td><td class=\"has-text-align-center\" data-align=\"center\"><a href=\"https:\/\/chromedriver.storage.googleapis.com\/index.html\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/chromedriver.storage.googleapis.com\/index.html\" rel=\"noreferrer noopener nofollow\">Download<\/a><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Mozilla Firefox<\/td><td class=\"has-text-align-center\" data-align=\"center\"><a href=\"https:\/\/github.com\/mozilla\/geckodriver\/releases\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/github.com\/mozilla\/geckodriver\/releases\" rel=\"noreferrer noopener nofollow\">Download<\/a><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Microsoft Edge<\/td><td class=\"has-text-align-center\" data-align=\"center\"><a href=\"https:\/\/developer.microsoft.com\/en-us\/microsoft-edge\/tools\/webdriver\/\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/developer.microsoft.com\/en-us\/microsoft-edge\/tools\/webdriver\/\" rel=\"noreferrer noopener nofollow\">Download<\/a><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">Apple Safari<\/td><td class=\"has-text-align-center\" data-align=\"center\">Already built-in<\/td><\/tr><\/tbody><\/table><figcaption>Driver Download Links<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"ways-to-use-drivers\">Ways to use Drivers<\/h2>\n\n\n\n<p>Let&#8217;s now look at how we can use the selenium web drivers. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-entering-the-direct-location\">1. Entering the Direct Location<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>The most popular method to use a web driver in Python Selenium code for browser automation. The reason for its popularity is the direct approach it uses, to define a driver in a script.<\/li><li>This method requires manual downloading of the web driver.<\/li><\/ul>\n\n\n\n<p><strong>Advantage<\/strong>: No need to care about lengthy setup or figuring out of Environment variable<br><strong>Disadvantage<\/strong>: Makes the code less flexible and subject to manual changes for every update<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndriver = webdriver.Chrome(&#039;path\/to\/chromedriver&quot;) \ndriver = webdriver.Chrome(&#039;C:\/\/software\/chromedriver.exe&#039;) #driver located at the specified location\ndriver = webdriver.Chrome(&#039;chromedriver.exe&#039;)  #driver located in the same directory as of the python script file\n\n#other way\n\nservice = Service(executable_path=&quot;\/path\/to\/chromedriver&quot;)\ndriver = webdriver.Chrome(service=service)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"2-environment-variable\">2. Environment Variable<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>If you have tried the above-mentioned method, then you may have noticed how tiring it is to note\/remember the location of your installed web driver in some directory of your computer.<\/li><li>To tackle for this problem, we define the location or path of our driver in Environment Variable, once and for all. After successful definition, we do not require specifying the path location in our code, and we can code effortlessly.<\/li><li>This method also requires manual installation of the driver, as you might have known this method is used for the issue of path variable.<\/li><\/ul>\n\n\n\n<p><strong>Advantage<\/strong>: No hassle of specifying the path to the driver<br><strong>Disadvantage<\/strong>: No update functionality<\/p>\n\n\n\n<p>To set the Environment Path Variable, go to your command prompt and type in the following command:In place of &#8220;C:\\WebDriver\\bin&#8221; in the below command, use the path (install location) of the driver.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsetx PATH &quot;%PATH%;C:\\WebDriver\\bin&quot;\n<\/pre><\/div>\n\n\n<p>If you find this way difficult, then you may search for Environment Variable in your Windows Start Menu and click open -&#8220;<strong>Edit the system environment variables<\/strong>&#8220;. After that, from the pop-up window select &#8220;<strong>Environment Variables<\/strong>&#8221; to open another pop-up window.<\/p>\n\n\n\n<p>From the <strong>System Variables<\/strong> option, select open <strong>Path <\/strong>and now click on <strong>New <\/strong>to introduce a new path variable. Paste the location of your web driver in it, then OK, OK, and finally again OK, in all windows.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/Environment-Variable-1024x626.png\" alt=\"Environment Variable\" class=\"wp-image-27225\" width=\"768\" height=\"470\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/Environment-Variable-1024x626.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/Environment-Variable-300x184.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/Environment-Variable-768x470.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/Environment-Variable.png 1249w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><figcaption>Environment Variable<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-driver-manager\">3. Driver Manager<\/h3>\n\n\n\n<p>The last and probably the most useful method to use is the Web Driver in your Python code. On selecting, automatic updating in Web Browser, the device only updates the browser and not the installed driver, in this case upon execution of python script the code gives error for not equal versions of the browser and driver.<\/p>\n\n\n\n<p><strong>Advantage<\/strong>: No version compatibility issues and help easily switch between multiple browsers<br><strong>Disadvantage<\/strong>: Maybe a bit difficult to set up for beginners<\/p>\n\n\n\n<p><strong>Install the driver manager<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\npip install webdriver-manager\n<\/pre><\/div>\n\n\n<p><strong>Import the manager<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom selenium import webdriver\nfrom webdriver_manager.chrome import ChromeDriverManager            # --&gt; for Chrome\n\n#from webdriver_manager.firefox import GeckoDriverManager               # --&gt; for Firefox\n\n\n<\/pre><\/div>\n\n\n<p>Now as we have installed and imported the manager, we use it in our code like &#8211; <\/p>\n\n\n\n<p>Use the <code><strong>install()<\/strong><\/code>&nbsp;method to get the location used by the manager and pass it into a loc_service class. This method informs about the location of the installed web driver by itself.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndriver = webdriver.Chrome(ChromeDriverManager().install())   # --&gt; for Chrome\n\n#driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())     #--&gt; for Firefox\n<\/pre><\/div>\n\n\n<p>For any other browser, you may check the official <a href=\"https:\/\/github.com\/SergeyPirogov\/webdriver_manager\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">GitHub repository<\/a> of the driver manager software.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>That&#8217;s it for the tutorial. Contrary to the popular methods of using the web drivers in Selenium, in this tutorial you learned about the other and maybe advantageous methods to perform the same.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Drivers in Selenium are a vital rather a must for Browser automation. Selenium library in Python or in any other programming language uses the particular Browser&#8217;s driver to control browsers with their actions, add functionality to it, and in all manipulate data (webpage) in it. Contrary to the most common way of declaring a Browser&#8217;s [&hellip;]<\/p>\n","protected":false},"author":38,"featured_media":27229,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-27218","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/27218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=27218"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/27218\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/27229"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=27218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=27218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=27218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}