Como Fazer uma Automação Web Utilizando o Python e o Selenium

Como Fazer uma Automação Web Utilizando o Python e o Selenium

How to Automate Tasks on the Internet Using Python and Selenium

Introduction to Automation

  • The video aims to teach viewers how to create automations on the internet using Python and Selenium, which can perform tasks like downloading files, filling out forms, and logging in.

Overview of Selenium

  • Selenium is a tool within Python that allows users to control their web browser directly, replicating mouse and keyboard actions through code.
  • This automation saves time by eliminating repetitive manual processes.

Setting Up the Environment

  • The presenter uses Jupyter Notebook for demonstrations and recommends it for beginners due to its user-friendly interface. A link for installation will be provided.
  • Users are free to use other code editors (like VS Code), but they must install necessary libraries as per their editor's requirements.

Initial Configurations

  • To control a web browser with Selenium, two configurations are needed: installing Selenium itself and setting up WebDriver.
  • The first step involves installing Selenium via Anaconda Prompt using the command pip install selenium.

Understanding WebDriver

  • After installing Selenium, users need to install WebDriver Manager, which facilitates browser control by linking Python with the installed browser (e.g., Chrome or Firefox).
  • WebDriver acts as a bridge between your script and the web browser, allowing automated interactions.

Browser Recommendations

  • It is recommended to use Google Chrome or Firefox for better compatibility with most websites. Chrome is preferred due to its widespread usage and good interaction capabilities.

Conclusion of Setup Process

  • Once initial configurations are completed, they only need to be done once. The video will proceed with demonstrating a simple automation process using these tools.

Introduction to Automation with Selenium

Overview of the Course

  • The speaker emphasizes that the goal is not to create a massive automation project but to learn how to build automation using Selenium.
  • A simple example will be demonstrated, focusing on filling out forms and clicking buttons on a webpage for a mini-course registration.
  • Participants will have access to a free mini-course during the session, which involves filling in personal details like name and email.

Steps for Automation

  • The process includes entering a website, filling in fields (name and email), and submitting the form through button clicks.
  • The initial step involves importing Selenium into the codebase, similar to previous installations discussed.

Setting Up Selenium

Importing Necessary Libraries

  • Users must import libraries such as webdriver from Selenium after installation on their computers.
  • The speaker demonstrates how to set up the code environment by creating an instance of a web browser using webdriver.

Choosing Browsers

  • Google Chrome or Firefox are recommended browsers for running Selenium automations due to better compatibility.
  • If using Firefox, users need to adjust their code accordingly by specifying Firefox instead of Chrome.

WebDriver Management

Manual vs. Automatic Setup

  • For successful execution of automation scripts, users must manually download WebDriver corresponding to their browser version (e.g., ChromeDriver for Chrome).
  • This manual setup can lead to issues when browsers update; thus, maintaining compatibility becomes challenging.

Automating Driver Management

  • To simplify this process, the speaker introduces webdriver-manager, which automatically manages driver versions based on installed browser versions.
  • This tool helps avoid manual downloads and ensures that scripts run smoothly even after browser updates.

Understanding WebDriver Manager for Selenium

Introduction to WebDriver Manager

  • The process of using webdriver-manager for managing downloads is standard and consists of three lines of code that are essential for creating a browser instance with Selenium.
  • It is recommended to save these lines in a dedicated folder on your computer, allowing easy access whenever you need to create new automation scripts.

Importing Necessary Libraries

  • When importing libraries, ensure the correct syntax: use from selenium.webdriver import Chrome if using Google Chrome.
  • If Firefox is being used instead, replace Chrome with Firefox, ensuring proper case sensitivity in Python.

Setting Up the Service

  • Import the service from Selenium's WebDriver: from selenium.webdriver.chrome.service import Service. This service will execute the necessary commands.
  • The line of code initializes the Chrome Driver Manager, which identifies the current version of Google Chrome and fetches the corresponding driver.

Configuring Browser Instance

  • You must specify that your browser instance will utilize this service rather than a manually downloaded driver.
  • Create a variable for the service and pass it into your browser configuration to ensure compatibility with your installed version of Chrome.

Running Your First Automation Script

  • Upon executing your script, it should automatically download the appropriate version of Chrome Driver based on your installed Google Chrome version.
  • A new browser window will open indicating that it is controlled by automated testing software (Selenium).

Navigating to Specific URLs

  • After setting up your browser instance successfully, you can navigate to any specific link on the internet using simple commands.
  • Use methods like .get() on your browser object to direct it towards desired web pages such as Google or Facebook.

Example Code Structure

  • You can structure all commands within one cell or separate them; both approaches work effectively in scripting environments.
  • To navigate to a page, call browser.get("URL"), where "URL" is replaced with the target website address.

How to Access and Automate Web Pages Using Selenium

Understanding Link Access

  • The process of accessing a specific webpage involves copying the complete link, which must start with "https" and be fully qualified.
  • It's essential that the entire link is enclosed in quotation marks for proper functionality when using it in code.

Running Code Segments

  • The speaker demonstrates how to execute code segments separately using Jupyter, allowing for incremental testing and debugging.
  • Asterisks indicate successful execution of code segments; caution is advised against running multiple codes simultaneously as they may interfere with each other.

Navigating with Selenium

  • Upon executing the navigation command, Selenium automatically opens the specified link and waits for it to load completely before proceeding.
  • The next step involves filling out forms on the webpage, such as entering a name and email address.

Identifying Elements on a Page

  • To interact with web elements (like text fields or buttons), one must use an identifier known as an "XPath," which allows Selenium to locate these elements effectively.
  • Commands in Selenium typically begin with navegador. (the Portuguese word for "browser"), indicating actions performed on the browser instance created by the user.

Utilizing Documentation for Element Identification

  • The speaker emphasizes consulting official documentation for guidance on finding elements within a webpage using Selenium commands.
  • Understanding element identification requires familiarity with various attributes like ID, class name, or tag name that can serve as references.

Expert Methodology in Element Selection

  • Each web element has unique identifiers that can be used to select them programmatically; this includes IDs or classes associated with those elements.
  • The method of selecting an element through its XPath requires two pieces of information: how to identify it and what specific attribute will be used.

How to Inspect Elements in Your Browser

Introduction to Element Inspection

  • The speaker introduces the concept of inspecting elements within a web browser, emphasizing the importance of understanding how to interact with webpage elements.
  • Instructions are given on maximizing the browser window and using the right-click context menu to access the "Inspect" feature.

Selecting Elements

  • The process of selecting specific elements is explained; users should hover over desired fields (e.g., first name input) and click to highlight them in blue.
  • After selecting an element, users can right-click on it and choose "Copy" followed by "Copy Selector," allowing them to obtain a unique identifier for that element.

Using Selectors in Code

  • The speaker advises using single quotes around selectors when coding, as double quotes may appear within the selector itself, potentially causing confusion.
  • A warning is issued against downloading pre-written code from external sources, as changes in web technologies could render such code obsolete or incompatible.

Understanding Element Methods

  • The speaker emphasizes learning processes rather than merely copying code. This knowledge will be beneficial when working with different websites.
  • An overview of methods used in this lesson is provided: click() for clicking buttons/links and sendKeys() for entering text into fields.

Practical Application: Filling Out Forms

  • Demonstration begins on filling out a form field programmatically. The speaker shows how to write code that interacts with selected elements.
  • A line of code is presented that utilizes previously copied selectors to fill out a first name field automatically.

Continuing with Additional Fields

  • After successfully entering data into one field, the speaker transitions to filling out an email field using similar techniques demonstrated earlier.
  • The process involves repeating previous steps: identifying the email input field through inspection and copying its selector for use in code.

Final Steps and Conclusion

  • Users are guided through writing additional lines of code for other form fields while maintaining clarity by keeping everything visible on-screen.
  • Emphasis is placed on ensuring correct interaction with each element by utilizing appropriate methods like sendKeys() for entering text into designated fields.

How to Automate Web Tasks Using Python

Introduction to the Automation Process

  • The speaker introduces a method for testing code, emphasizing that participants should register with their email to access a free mini-course.
  • Demonstrates filling out an online form by entering an email address, highlighting the importance of correct input.

Interacting with Web Elements

  • Discusses the process of clicking a button on a webpage, explaining that finding and interacting with elements follows a standard procedure.
  • The speaker shows how to select the button element using browser tools and copy its XPath for use in code.

Executing Code and Navigating Pages

  • The speaker writes code to find the button element using its XPath, preparing to automate clicking it.
  • After executing the script, it successfully fills in the email and clicks "access," leading to redirection to another page.

Handling Course Access

  • Upon successful registration, users are directed to the mini-course page. If an unregistered email is used, they are prompted to check their inbox for confirmation.
  • The speaker mentions different automation processes available within the course content.

Running Multiple Cells in Code

  • To ensure functionality, all cells of code are executed at once. This can be done through specific commands or by consolidating code into one cell.
  • A new browser window opens as part of running the automation script successfully without issues.

Troubleshooting Errors

  • An error message appears related to webdriverexception, which is identified as a minor bug linked with ChromeDriver version compatibility.
  • The speaker reassures viewers that this issue does not affect overall functionality and suggests future updates may resolve it. Encourages feedback on learning more about Selenium and Python automation.
Video description

Quer saber mais sobre o nosso Curso Completo de Python? Clique no link abaixo para garantir sua vaga na próxima turma: https://pages.hashtagtreinamentos.com/esperapythonimpressionador?origemurl=hashtag_yt_org_listaesperapython_8AMNaVt0z_M PARA BAIXAR O MINICURSO GRATUITO DE PYTHON: https://pages.hashtagtreinamentos.com/inscricao-minicurso-python-automacao-org?origemurl=hashtag_yt_org_minipython_8AMNaVt0z_M ----------------------------------------------------------------------- ► Arquivos Utilizados no Vídeo: https://pages.hashtagtreinamentos.com/arquivo-python-1jGh7kZSxQLoznA_GgwnWa8f7LEl3kKCZ?origemurl=hashtag_yt_org_planilhapyt_8AMNaVt0z_M ► Vídeo de Instalação do Jupyter: https://youtu.be/_eK0z5QbpKA ----------------------------------------------------------------------- Caso prefira o vídeo em formato de texto: https://www.hashtagtreinamentos.com/automacao-web-com-selenium-em-python ----------------------------------------------------------------------- Fala Impressionadores! Na aula de hoje eu vou te ensinar a fazer uma automação web com Python! A ideia é te mostrar como fazer automações na internet com Selenium para que você possa automatizar suas tarefas chatas e repetitivas que precisa fazer. Podem ser tarefas do trabalho, tarefas pessoas, busca de informações, preenchimento de formulários... Com isso vai poder facilitar sua vida, pois o Selenium é responsável por controlar o navegador, então ele vai executar as ações como se fosse você. Já imaginou criar uma automação com Selenium e otimizar seus trabalhos, ser mais eficiente no trabalho e até ter mais tempo para outras atividades? Então vem comigo que eu vou te ensinar automação web utilizando Python! ----------------------------------------------------------------------- Hashtag Programação ► Inscreva-se em nosso canal: http://bit.ly/3c0LJQi ► Ative as notificações (clica no sininho)! ► Curta o nosso vídeo! ----------------------------------------------------------------------- Redes Sociais ► Blog: https://bit.ly/2MRUZs0 ► YouTube: http://bit.ly/3c0LJQi ► Instagram: https://bit.ly/3o6dw42 ► Facebook: http://bit.ly/3qGtaF2 Aqui nos vídeos do canal da Hashtag Programação ensinamos diversas dicas de Python para que você consiga se desenvolver nessa linguagem de programação! ----------------------------------------------------------------------- #python #hashtagprogramacao #cienciadedados