Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. Selenium Tutorial covers all topics such as – WebDriver, WebElement, Unit Testing with selenium. This Python Selenium Tutorial covers Selenium from basics to advanced and professional uses. Show
Web scraping menggunakan Python adalah cara mengumpulkan data dari web, dan cukup mudah, karena sudah tersedia library yang mendukung. Modul Pendukung Web Scraping
Selenium sebenarnya hanya berfungsi untuk otomasi browsers, namun dengan fungsi ini banyak hal yang bisa dilakukan. Umumnya digunakan untuk kebutuhan otomasi testing web apps. Namun tidak hanya itu, pengumpulan data dari web juga bisa dilakukan secara otomatis dengan bantuan selenium. BeautifulSoup adalah library yang mempermudah dalam melakukan ekstraksi informasi dari web pages. Memudahkan programmer Python dalam melakukan iterasi, pencarian dan modifikasi tree dari content web pages. ChromeDriver adalah standalone server yang mengimplementasikan standard W3C webdriver. WebDriver sendiri adalah open source tool untuk melakukan otomasi testing, dengan kapabilitas seperti navigasi, user input, javascript dan lainnya. Berikut contoh penggunaan modul diatas (selenium dan chromedriver). Program akan membuka halaman google.com dan menekan tombol I’m Feeling Lucky kemudian melakukan print screen, hasilnya akan disimpan ke local file. Before going further in this tutorial, first, we will understand some essential points which will help us to implement test scripts in Python. What is Selenium?Selenium is an open-source testing tool, which means it can be downloaded from the internet without spending anything. Selenium is a functional testing tool and also compatible with non-functional testing tools as well. For further information about Selenium, refer the below link: https://www.javatpoint.com/selenium-tutorial Why we use Selenium for automation testing?Selenium is one of the most popular automation testing tools. Here automation testing is a process of converting any manual test case into the test scripts using automation tools such as Selenium. Following are some aspects that, why we prefer Selenium for automation testing:
What is Python?
For further details about Python language, refer the below link: https://www.javatpoint.com/Python-tutorial Why is Python useful for automation testing?
Selenium with PythonThere are two ways to run a Selenium test scripts with the help of Python:
Let us see how we configure Selenium with the help of Python programing language: Configure Selenium using PythonThere are following steps to configure Selenium using Python:
Download and install Python for WindowsIn this section, we will see how we download and install the Python for Windows platform. Download the Python To download the latest version of Python for Windows Platforms, refer the below link: https://www.Python.org/downloads/
Install the Python After downloading the Python for Windows-64 bit, we will be ready to install the Python. To install the Python, follow the below process:
After that, we will check whether Python is installed successfully and working fine or not. So for this, we will open our command prompt, and type the command as Python and press the Enter key, and it will open the Python interpreter shell where we can implement the Python program as we can see in the below image: Installing the Selenium libraries in PythonOnce we successfully install the Python in our operation system, we will install the Selenium libraries. For this, we will execute the following command in our command prompt: And, this command will successfully install the latest Selenium package i.e., Selenium -3.141.0 added to the libraries as we can see in the below image: After that executing the above command, it will create the Selenium folder automatically having all the Selenium libraries as we can see in the below screenshot: Download and install PyCharmOnce we successfully install the Selenium libraries into Python, we are ready to download Python IDE that is PyCharm. To download the PyCharm, follow the below process:
Create a new project and write the Selenium test scriptOnce we successfully install the PyCharm, we will open the PyCharm IDE for creating a new project. Create a New Project in PyCharm Follow the below process, to create a new project in PyCharm:
Adding Selenium Test Scripts For adding the Selenium test scripts in the PyCharm, follow the below process:
Write the Selenium test script For our testing purpose, we will first go to the Google Home page and search javatpoint from there. We are creating our sample test script step by step to give you a complete understanding of how we write a Selenium test script in Python programming language. For this, follow the below steps: StepsActionsInputExpected Result1.Import WebDriver from selenium.The WebDriver should be imported.2.Open the Google Chrome browser.The Google Chrome browser should be opened.3.Maximize the browser window.The browser window should be maximized.4.Navigate to the Google home page.https://www.google.com/The Google home page must be displayed.5.Identify the Google search text box and pass the value.javatpointThe value should be entered in the search text box.6.Click on the Google search button.The Google search button should be clicked.7.Close the Browser.The Browser should be closed.Step1 In the first step, we will type the following statement to import the web driver: Step2 After that, we will open the Google Chrome browser. As we can see in the below screenshot, we have multiple types of browsers options available, and we can select any browser from the list like Chrome, Edge, firefox, Internet Explorer, opera, safari, etc. Following are the sample code for opening the Google Chrome browser: Step3 In the next step, we will be maximizing our browser window size, and the sample code is as below: Step4 Then, we will navigate to the given URL. The sample code is as below: Note: As we know that Python is a very easy language to write code because we don't have to write multiple statements like as we did it java. Or if we want to comment out something, we just simply put a hash[#] in our statements, or we can directly press Ctrl+ Forward slash [/] from our keyboard.Step5 In this step, we are trying to locate the Google search text box with the help of its Name attribute value.
Here the sample code: Step6 Once we identify the Google search text box, and we will identify the Google Search button. So for this, follow the below process:
And, the sample code is as following: Step7 In the last step, we are closing the browser. And, the sample code for closing the browser is as follows: Our final test script will look like this, after completing all the above steps: Note: |