Your ultimate guide

Write a First Automation Program using Python with Selenium



Create a New Python file:
    1) Right-click on your project folder, select New and then click on Python File. 
        
    2) Give the "file name", and select "Python file" then press ENTER.
        
    3) Then your Python file will be created to write the script.

    Note:
        If you want to create a folder in your project, Right-click on your project folder, select New and then click on Directory. Give the Folder name and then press ENTER.


To Launch Web Browser:
    1) First we need to import 'webdriver' from the 'selenium' package.
    2) and we need to import 'Service' from the 'selenium.webdriver.chrome service'
        Example:
            from selenium import webdriver
            from selenium.webdriver.chrome.service import Service
    3) Based on which browser we are going to launch that browser script we need to write
            a) For Chrome Browser:
                    driver = webdriver.Chrome(service=Service("path\to\chromedrive.exe"))
            a) For Firefox Browser:
                    driver = webdriver.Firefox(service=Service("path\to\geckodrive.exe"))
            a) For Edge Browser:
                    driver = webdriver.Edge(service=Service("path\to\msedgedriverdrive.exe"))

    Note: Here path means, the location of already downloaded(and extracted) Browser specific web drivers in your computer.
            For Example, if the chromedriver.exe file is located in the "C:\Driver\" folder then the path will be: "C:\Driver\chromedriver.exe"


To Launch any website (eg: google):
    1) get(String Url) function, is used to open the given URL in the browser.
            Syntax: driver.get("Url")
            Example: driver.get("https://www.google.com")



Example Program to launch Google in Chrome Browser:

    1) from selenium import webdriver
    2) from selenium.webdriver.chrome.service import Service
    3) driver = webdriver.Chrome(service=Service("C:\Driver\chromedriver.exe"))
    4) driver.get("https://www.google.com")
    5) driver.close()

    Output:
        


Note:
    The majority of Selenium scripts are consistent across all the Selenium versions, but there may be occasional changes required depending on the specific Selenium version being used.
    For Example:
- To Launch the Web Browser in Selenium 3 the script is:
    from selenium import webdriver

    driver = webdriver.Chrome(execuutable_path="path\to\chromedriver.exe")

- To Launch the Web Browser in Selenium 4 the script is:
    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service

    driver = webdriver.Chrome(service = Service("path\to\chromedriver.exe"))

No comments:

Post a Comment