{"id":26419,"date":"2022-01-29T13:08:43","date_gmt":"2022-01-29T13:08:43","guid":{"rendered":"https:\/\/www.askpython.com\/?p=26419"},"modified":"2022-01-29T13:18:46","modified_gmt":"2022-01-29T13:18:46","slug":"selenium-introduction-and-setup","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/selenium-introduction-and-setup","title":{"rendered":"Python Selenium Introduction and Setup"},"content":{"rendered":"\n<p>The purpose of this tutorial is to introduce you to Selenium and show the process of installing selenium and webdriver for browser automation. In this article, we\u2019ve assumed that you already have python installed on your machine.<\/p>\n\n\n\n<p>It&#8217;s an important point to note that Browser automation and Web Scrapping are completely <strong>White Hat<\/strong> in their approach and the Web Browsers themselves support it officially, provide web drivers for automation and testing, just the browser window opened using code is labeled as &#8220;this browser is being controlled by automated test software&#8221;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-selenium\">What is Selenium?<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.selenium.dev\/\" target=\"_blank\" rel=\"noopener\">Selenium<\/a> is an open-source project which provides a wide range of tools to automate the web browser. It is also used to create web scrapers to obtain(scrape) desired data from web pages.<\/p>\n\n\n\n<p>Some tasks that can be done using Python Selenium are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Automating Browser tasks such as login, meeting join, scrolling, surfing, etc.<\/li><li>Getting Data from Website\/webpage in text, excel file, code, etc.<\/li><\/ul>\n\n\n\n<p>A crucial component in browser automation is a Webdriver. A <strong>Webdriver<\/strong> is a collection of APIs which makes interaction with the browser easy. Combining&nbsp;Selenium and webdriver make it very easy to automate boring tasks of the web.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"installing-selenium\">Installing Selenium<\/h2>\n\n\n\n<p>To start with setting up our computer for browser automation and web scraping, we need to start with the installation of some tools and libraries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-install-selenium\">1. Install Selenium<\/h3>\n\n\n\n<p>First we\u2019ll install the selenium package using <a href=\"https:\/\/www.askpython.com\/python-modules\/python-pip\" data-type=\"post\" data-id=\"3848\">pip<\/a>. It is very easy to install any python package using the <strong>pip install package_name<\/strong> command. <\/p>\n\n\n\n<p>Open the command prompt of your computer and enter the below command. You can also run the command in the terminal of your system or IDE.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npip install selenium\n<\/pre><\/div>\n\n\n<p>It will install the latest version of selenium on our machine.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-install-the-selenium-webdriver\">2. Install the Selenium Webdriver<\/h3>\n\n\n\n<p>Our second task, in setting up is to install the webdriver as per our browser, which we intend to use to automate.<\/p>\n\n\n\n<p>While installing the webdriver we need to make sure that it has the <strong>same version<\/strong> as our Web Browser. Every browser has its own webdriver which is maintained by its parent companies.<\/p>\n\n\n\n<p>Below are the links for downloading webdriver of the popular Web Browers &#8211; Mozilla Firefox, Google Chrome, and Microsoft Edge respectively.<\/p>\n\n\n\n<p>To download Mozilla Firefox Webdriver: <a href=\"https:\/\/github.com\/mozilla\/geckodriver\/releases\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a><br>To download Google Chrome Webdriver: <a href=\"https:\/\/chromedriver.chromium.org\/downloads\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a><br>To download Microsoft Webdriver: <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\">here<\/a><\/p>\n\n\n\n<p>After downloading selenium and the required webdriver, you are ready to write python scripts to automate the web browser.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-importing-selenium-in-python\">3. Importing Selenium in Python<\/h3>\n\n\n\n<p>As we have downloaded the required tools and libraries, as a final step we need to import the required as follow:<\/p>\n\n\n\n<p><strong>Note<\/strong>: We need to pass the location (as saved on our computer) of the installed web driver file to the webdriver method.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom selenium import webdriver\nfrom selenium.webdriver.common.keys import Keys\n\ndriver = webdriver.Chrome(&#039;C:\/\/software\/chromedriver.exe&#039;)\n<\/pre><\/div>\n\n\n<p><strong>Tip<\/strong>: Instead, of passing the location as a parameter every time, in every code, we can skip this step in a smart way by declaring (saving) the location of the downloaded web driver file as an environment variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"recommended-reads-for-selenium\">Recommended Reads for Selenium<\/h2>\n\n\n\n<p>Finally, we are done with the setup and you may start web scraping and automating your web browser tasks by following our tutorials on Python Selenium &#8211;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.askpython.com\/python-modules\/open-webpage-urls-selenium\">Open Web Page URLs using Python Selenium<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python-modules\/fetch-website-data-selenium\" data-type=\"post\" data-id=\"26338\">Fetch data from a web page<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python-modules\/fetch-stock-market-data-selenium\" data-type=\"post\" data-id=\"26340\">Fetch Stock Market data using Selenium<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The purpose of this tutorial is to introduce you to Selenium and show the process of installing selenium and webdriver for browser automation. In this article, we\u2019ve assumed that you already have python installed on your machine. It&#8217;s an important point to note that Browser automation and Web Scrapping are completely White Hat in their [&hellip;]<\/p>\n","protected":false},"author":38,"featured_media":26424,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-26419","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\/26419","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=26419"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/26419\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/26424"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=26419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=26419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=26419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}