Interesting Automation with Selenium using Python

#python
#selenium
Interesting Automation with Selenium using Python

Selenium refers to a set of tools that are widely used in the testing community when it comes to cross-browser testing. Selenium is adaptable with many programming languages: C#, Java, JavaScript, Ruby, Python, PHP. But no worry we are not going to use these programming languages in this section. We use python for completing some fascinating tasks with the selenium web driver. Now you are thinking of what is Selenium web driver.                                                                                                There are many components related to selenium and their uses also are different. But here we gonna use the web driver of selenium. Selenium WebDriver is a web framework that allows you to execute cross-browser testing. it is used for automating web-based application testing to confirm that it executes predictably.

You can also learn in details about the Selenium WebDriver Framework Architecture😀😀 .

Okay !!! Let's jump into our motive to use selenium in the first hand.

🔨Setup the Environment:

Step-> 1   Make sure that you have installed the python in your system. because we are using python to automate our tasks with selenium and also pip install in your system.
step->2  Open your command line tool(depend upon which operating system you use) then type this command : pip install selenium
selenium package install
As you automate the tasks through browsers only so for that Selenium requires a driver to interface with the chosen browser. Here I am using chrome browser . You can download the driver depends upon your chrome version only -> Chrome Driver

Get a move on!!!  We completed our setup with selenium.

🥇First Task(Sign in into GitHub)

task 1(sign in to GitHub)
from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("https://github.com/")

def sign_in():
    signIn = driver.find_element_by_xpath("//a[@class= 'HeaderMenu-link flex-shrink-0 no-underline mr-3']")
    signIn.click()

def fill_form():
    username = driver.find_element_by_xpath("//input[@id='login_field']")
    password = driver.find_element_by_xpath("//input[@id='password']")
    username.send_keys("your github linked mail or username")
    password.send_keys("your github password")
    submit = driver.find_element_by_xpath("//input[@class='btn btn-primary btn-block']")
    submit.click()

sign_in()
fill_form()

Don' t worry I will give you a brief understanding of these. Let's understand step by step.

➡ first, we have to import a webdriver from selenium because we are required for testing web applications across different browsers using different programming languages.

webdriver.ChromeOptions is used for customizing the ChromeDriver session. By default when selenium opens up any browser, it opens up without any extension enable or history or cookies, etc.

add_argument("--incognito") is automatically accessible to the incognito mode of any browser you used. You can also add any argument.

driver.get(url)  .get() method is used to open an URL and it will wait till the whole page gets loaded.

Sign_in() function which is a custom function to sign in into github. You can name it in your choice. After that find_element_by_xpath – This method returns all the elements with matching xpath in the argument to form a list. It shall return an empty list if no element has the matching xpath. and then .click()
method is used to emulates a click operation for a link, button, checkbox or radio button.

Fill_form() is an anonymous function where we have to give the details of username or email and the password for signin to GigHub. .find_element_by_xpath is discussed above mentioned and by using  .sendkeys() method we can enter the field values of forms.

➡ And at last we call the function to perform the execution.

then execute by type python file_name.py in the command line. After that you can see the magic of selenium.
You can also do this type of amazing work using selenium. Try yourself to create a account in GitHub.
💡 Note : selectors like Id and class may change due to the update of the platform and the technologies. So you have to keep an eye upon these and for that you can use the developer mode of the browsers.

                                 

🥈Second Task (Convert a PDF file to XLSX)

task 2(convert .pdf to .xlsx)
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")

driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("https://smallpdf.com/pdf-to-excel")
def upload_pdf():
    pdf = driver.find_element_by_id("__picker-input")
    pdf.send_keys("your pdf file path")

def select_option():
    radio = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='sc-6ytb27-1 hVWJHb']")))
    radio.click()
    convert = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='sc-1mvwhop-0 gHRELX']")))
    convert.click()

def download():
    dwn = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='lqkt1b-0 fdItuc l3tlg0-1 gcPfnF']")))
    dwn.click()

upload_pdf()
select_option()
download()

This job is for converting an pdf to excel file using smallpdf website(you can use any other website for converting , but I like much this😁).

Some import Header and methods are already discussed. Here we are going to cover remaining .

WebDriverWait is used to wait for a page load to complete. Actually, it uses a timeout. It waits for an element to show on the page, import By is for selecting the Selector as you wish(it may be ID, CLASS, XPATH, etc.).The webdriverWait class along with expected_conditions is used to create an explicit wait. The webdriverWait class can call the ExpectedCondition after every 500ms by default for checking if the condition is met or not. These are the part of explicit wait in Selenium. you can click here to know more about the wait functionalities.

upload_pdf() function:  Above we considered few methods, which are also included. You can also send your local file as value using send_keys().

select_option() function: After uploading our pdf file we have to convert the file to an excel file. So webDriverWait is used to wait for some time when met the condition. in the until method, the condition is passed. If the condition pass successfully it can use click() to go forward.

download() function: is to download the converted .xlsx file. It also follow the above methods.

➡ At last we call our function to perform the task

If you wanted to convert multiple file in same process then you could update the code to the following.   ⬇👇⬇

from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import os.path

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")


path = "path of pdf folder"
num_files = os.listdir(path)

for file in num_files:
    driver = webdriver.Chrome(chrome_options=chrome_options)
    driver.get("https://smallpdf.com/pdf-to-excel")
    def upload_pdf():
        pdf = driver.find_element_by_id("__picker-input")
        pdf.send_keys(path+file)

    def select_option():
        radio = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='sc-6ytb27-1 hVWJHb']")))
        radio.click()
        convert = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='sc-1mvwhop-0 gHRELX']")))
        convert.click()

    def download():
        dwn = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='lqkt1b-0 fdItuc l3tlg0-1 gcPfnF']")))
        dwn.click()

    upload_pdf()
    select_option()
    time.sleep(6)
    download()
    time.sleep(6)
    driver.close()

Here we just using the for loop to perform our task again and again.            Include os.path module to interact with our file system, os.listdir() returns the list of entries for the given directory. That's it what we have to know in this code and time.sleep() is used to add delay in the execution of a program.

Okay!!!  It's complete now 🏆

                             

This is it, for now, let me know if I left any concept in these above codes, you can put your comment below. Also, you can connect through my LinkedIn. I wish you understood the uses of selenium through the above two tasks.

Tweet a thanks to the author