Home Python Selenium 4 With Python – A Step by Step Information

Selenium 4 With Python – A Step by Step Information

0
Selenium 4 With Python – A Step by Step Information

[ad_1]

Selenium 4 has been round now for fairly a little bit of time. It got here out with many new options, some you may immediately use whereas some will profit you within the background. Regardless of it isn’t new anymore however nonetheless, not a number of stuff you’ll find to find out about it virtually. That’s the place this tutorial goes that can assist you by offering a step-by-step information to arrange Selenium 4 with Python.

TOC – Selenium 4 With Python

Python at all times offers a seamless expertise when used with Selenium. With this new launch, the combination goes to get higher. Let’s rapidly study what’s new inside Selenium 4.

What’s New in Selenium 4

Let’s first rapidly undergo the listing of Selenium 4 options:

1. From an automation testing standpoint, essentially the most helpful characteristic is the assist for relative locators in Selenium 4. One of many major benefits of that is to make sure that your scripts don’t fail due to a reproduction ingredient. With the assistance of those relative locators, you may differentiate it from a possible duplicate ingredient. Furthermore, you need to use this characteristic to get extra dependable take a look at outcomes.

2. With the brand new model, you may obtain and set up the proper internet driver with one line of code. Although, doing it isn’t as straightforward because it sounds. 🙂

3. Selenium Grid is extra succesful now. It will probably itself register and re-register nodes and has self-healing capability. It will probably select a brand new chief if the principle hub fails. The communication between its parts is now over HTTPS and it might implement authentication and authorization for controlling entry to assets.

4. The brand new Selenium now makes use of built-in Chrome’s headless renderer which hastens take a look at execution. It exhibits internet pages precisely as they need to.

5. Earlier Selenium was utilizing the JSON Wire Protocol, now it has turn out to be W3C compliant. For you, it means: You may write checks as soon as and run them in every single place. You may run the identical checks in additional browsers as your scripts are suitable. Furthermore, it ensures the checks are future-ready as any new browser is extra doubtless to make use of the W3C guidelines.

Arrange Selenium 4 with Python on Ubuntu 22.04

Right here’s the step-by-step information on organising Selenium 4 on Ubuntu 22.04. The next are the main points of the goal OS, we are going to use to arrange Selenium:

$ cat /and many others/os-release | grep -E '^(NAME|VERSION_ID|VERSION|ID|CODENAME|UBUNTU_CODENAME)'

AME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.3 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
UBUNTU_CODENAME=jammy

Step1. Sync System Packages

Firstly, open the console and take this secure step to sync your put in packages:

$ sudo apt replace; apt improve

Step2. Get the Helper Instruments

Set up the helper instruments we’ll want through the setup.

$ sudo apt set up unzip wget

Step3. Obtain the Newest and Steady ChromeDriver

Since we’ll run our checks within the Chrome browser, so we have to get the suitable model of its internet driver. For this, let’s first test the model of Chrome operating on our system:

$ google-chrome --version 
Google Chrome 121.0.6167.160

Now we all know the model of our browser. The following factor is to get the net driver which is as near the browser model as attainable.

There are two methods to do it. The primary one is to comply with the beneath steps to obtain it manually.

One other approach to set up the bundle utilizing the pip command. Examine the steps beneath:

$ pip set up chromedriver-py # Set up the default model
$ pip set up chromedriver-py==121.0.6167.85 # Set up a selected model
To set up Selenium with Python, install chrome driver using pip

Step4. Extract ChromeDriver

When you have downloaded the net driver through the hyperlink, then unzip the downloaded file and replica it as per the beneath directions.

$ sudo unzip chromedriver_linux64.zip -d /decide/chromedriver

Step5. Set Env Vars

Add the trail to the chrome driver dir to your system env variables:

echo 'export PATH="/decide/chromedriver:$PATH"' >> ~/.bashrc
supply ~/.bashrc

Step6. Set up Selenium Libraries

Use pip to put in the required Selenium libraries:

$ pip set up selenium webdriver-manager

After this test the set up:

$ pip listing | grep  selenium
selenium                 4.17.2

Step7. Confirm Selenium Model

Open a Python interpreter and check out importing the libraries:

import selenium
print(selenium.__version__)

Save the file as “check_version.y”. This could print the put in Selenium model.

$ python3 check_version.py 
4.17.2

Check with a Easy Selenium Python Script

Let’s create a pattern Selenium 4 Python script to check your setup:

from selenium import webdriver as wd

dr = wd.Chrome()
dr.get("https://www.google.com")
dr.stop()

Save the script as primary.py and run it:

$ python primary.py

If all goes effectively, your browser ought to open and change to Google.

Demo Script to Search String in Google

Let’s now try one other script that opens Google search and discover a key phrase.

from selenium import webdriver as dr
from selenium.webdriver.widespread.by import By
from selenium.webdriver.assist.ui import WebDriverWait as wt
from selenium.webdriver.assist import expected_conditions as EC
from chromedriver_py import binary_path as bin

svc = dr.ChromeService(executable_path=bin)
wd = dr.Chrome(service=svc)

# Change together with your desired URL
url = "https://www.google.com/"

# Navigate to the URL
wd.get(url)

# Discover the search bar ingredient utilizing its title attr
search_bar = wt(wd, 10).till(
    EC.visibility_of_element_located((By.NAME, "q"))
)

# Kind "Selenium take a look at" into the search bar
search_bar.send_keys("Selenium take a look at")

# Discover the search button utilizing its title attribute
search_btn = wt(wd, 10).till(
    EC.element_to_be_clickable((By.NAME, "btnK"))
)

# Click on the search button
search_btn.click on()

# Non-compulsory: Anticipate a selected ingredient on the subsequent web page if wanted
# For instance, anticipate the search outcomes to load
# wt(wd, 10).till(EC.presence_of_element_located((By.ID, "search-results")))

# Print the web page title
title = wd.title
print(f"Web page title: {title}")

# Shut the browser window
wd.stop()

Save the above Selenium 4 Python script as gsearch.py. Run it utilizing the next command. It ought to seek for “Selenium take a look at” and print the web page title of the search outcomes web page.

$ python3 gsearch.py
Web page title: Selenium take a look at - Google Search

Python Script to Show Selenium 4 Relative Locators

As we all know Selenium 4 added new relative locators like above, beneath, to_right_of, to_left_of, and close to, so let’s get to know how one can use them with Python.

This instance is partially reserving a flight from the Google flight web page. It finds and fills sure fields on the internet web page utilizing the relative locators.

from selenium import webdriver as wd
from selenium.webdriver.widespread.keys import Keys
from selenium.webdriver.widespread.by import By
from selenium.webdriver.assist.relative_locator import *
from time import sleep

# Change together with your desired departure and arrival airports
src_city = "Lucknow"
dstn_city = "Australia"

# Provoke Chrome session
dr = wd.Chrome()

# Navigate to Google Flights
dr.get("https://www.google.com/journey/flights")

# Anticipate web page to load (modify timeout if wanted)
dr.implicitly_wait(10)

elem_label = dr.find_element(By.XPATH, "//div[text()='Flights']")
print("elem_label = ", elem_label)
print()

# Discover search kind subject beneath "Flight" label
src = dr.find_element(
    with_tag_name("enter").beneath(elem_label)
)
print("src= ", src)
print()

dstn = dr.find_element(
    with_tag_name("enter").to_right_of(src)
)
print("dstn= ", dstn)
print()

# Enter departure and arrival airports
src.clear()
src.send_keys(src_city)
sleep(1)

ele1 = dr.find_element(with_tag_name("div").beneath(src))
print("ele1= ", ele1)
print()

ele1.click on()

dstn.clear()
dstn.send_keys(dstn_city)
sleep(1)

ele2 = dr.find_element(with_tag_name("div").beneath(dstn))
print("ele2= ", ele2)
print()

ele2.click on()

# ... (proceed together with your reserving course of)

sleep(5)
dr.stop()

Further Notes

  • If you happen to arrange the Chrome driver manually, then you might want to specify its path in your script. Nevertheless, you received’t want it when you’ve got up to date the bashrc file with the driving force path.
  • You need to use another driver like GeckoDriver (for Firefox) by following comparable steps and downloading the respective driver.
  • To arrange a number of variations of Selenium, use a device  virtualenv to create project-specific Python environments and dependencies.

Moral Issues

  • Pay attention to and respect web site phrases of service when utilizing automation instruments.
  • Keep away from actions which may overload servers or violate moral tips.

We hope this turns into your go-to information to arrange Selenium 4 in your Ubuntu system!

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here