The Beginner’s Guide to Selenium With Python | by Steffy Lo | Feb, 2022

A step-by-step tutorial to get began with Selenium

Steffy Lo
Picture by Clément Hélardot on Unsplash

By its formal definition, “Selenium is a set of instruments for automating internet browsers”, and that’s actually what it’s. Whereas I believe some individuals may need misunderstood it as a testing device as I did at first, I’ve realized to see that Selenium is extra of a general-purpose device that can be utilized for browser testing as an alternative. In truth, for any repetitive web-based activity that you end up doing, you need to make Selenium do it for you!

In truth, I’ve created a challenge myself, known as Readminder, that helps me automate the method of looking out the most recent chapters and/or chapters the place I left off for my favorite internet comics and ship me an electronic mail reminder for them. Test it out here!

Earlier than we get began, there are three essential steps in organising Selenium:

1) Install the Selenium library to your desired programming language

Selenium at present helps as much as 6 completely different programming languages: Java, Python, C# Ruby, JavaScript, and Kotlin. I’ve chosen Python right here as it’s the language identified for automation/scripting and it’s undeniably my favorite language 🙂

pip set up selenium

2) Download an identical model to your browser driver

I’m selecting Chrome right here because it’s the most well-liked internet browser. First, ensure you are downloading the motive force for the best model by going to chome://model

Right here, you may see that the model I’ve is 97.0.4692.71, yours will most likely be completely different. Don’t be concerned if there is no precise matching model listed. Select the model closest to it. Possible the one to decide on is the place the final 2 digits will not match and that’s advantageous.

3) Arrange the mandatory path for the drivers

Relying in your system, it’s going to look barely completely different on the way you add the situation of your drivers to your PATH setting variable.

Bash

echo 'export PATH=$PATH:/path/to/driver' >> ~/.bashrc
supply ~/.bashrc

Zsh

echo 'export PATH=$PATH:/path/to/driver' >> ~/.zshrc
supply ~/.zshrc

Home windows

setx PATH "%PATH%;C:pathtodriver"

As soon as the above steps are carried out, you are actually prepared to start out automating your browser!

Let’s take a look on the 8 elementary steps or actions you may take when automating the browser with Selenium.

driver = webdriver.Chrome()
driver.get("http://www.google.com")

The commonest internet browser motion you may take is navigating to an internet web page. Within the above instance, we’re opening an internet web page with the URL http://www.google.com. Another helpful internet browser actions embrace:

Different Browser Navigations

driver.again()    # Clicks the browser’s again button
driver.ahead() # Clicks the browser’s ahead button
driver.refresh() # Refreshes the present browser's web page

Add or Delete Cookie

Cookies are helpful for storing and loading consumer data. So as to add a cookie to the present searching context, we are able to use the add_cookie operate which accepts a cookie serializable JSON object.

driver.add_cookie("identify": "key", "worth": "worth")

The table beneath describes the fields of a serialized cookie and whether or not it’s non-compulsory when supplied so as to add a cookie.

Working with Home windows and Tabs

Each window and tab has what it is known as window deal with, which is used to uniquely establish it. To get the window deal with of the present window or tab, we are able to use the next property:

driver.current_window_handle

Once we create a brand new tab or window, the main target shall be shifted onto the brand new window or tab on display screen

driver.switch_to.new_window('tab')    # Change to a brand new tab
driver.switch_to.new_window('window') # Change to a brand new window

To modify again to our authentic window, we are able to merely use a variable to retailer our authentic saved window deal with and change again to it.

original_window = driver.current_window_handle
...
some window/tab switching logic...
...
driver.switch_to.window(original_window)

Lastly, to shut the present window or tab, we are able to name:

driver.shut()
driver.title          # get title
driver.current_url # get present URL

There are a bunch of forms of details about the browser you may request moreover the title and present URL, together with the next:

Cookies

If we had added a cookie earlier than, we are able to get its particulars by its identify by means of the get_cookie operate like so:

driver.get_cookie("foo")    # Get particulars of a cookie named 'foo'

Window Dimension

dimension = driver.get_window_size()
width = dimension.get("width")
top = dimension.get("top")

Window Positions

place = driver.get_window_position()
x1 = place.get('x')
y1 = place.get('y')

Usually, we wish to set up a ready technique to work together with components within the when it has been loaded and able to be interacted. Let’s take a look at the two forms of ready technique.

Implicit Wait

driver.implicitly_wait(10)

The best technique is the implicit wait technique. It tells the WebDriver to ballot or repeatedly test the DOM for a specified period of time when looking for ingredient(s) and if they’re discovered earlier than this specified period of time, then we proceed with out ready out the total length (10 seconds in our instance above). This ready technique is generally used to make it possible for we wait till the ingredient is prepared if they aren’t instantly obtainable.

Express Wait

from selenium.webdriver.help import expected_conditions as EC
wait = WebDriverWait(driver, timeout=10)
ingredient = wait.till(EC.element_to_be_clickable((By.ID, 'someid')))

The express wait technique is available in once you want extra flexibility in defining precisely how lengthy you need the WebDriver to attend. This wait technique permits your code to halt program execution, or freeze the thread, till the situation you specify resolves. This implies earlier than the timeout (10 seconds in our instance above) has elapsed, it’s going to hold making an attempt and ready on the situation till it returns a true worth.

Notice that utilizing implicit and express ready methods in conjunction may cause unpredictable wait occasions as implicit waits don’t have a predefined wait time. For instance, setting an implicit wait of 10 seconds and an express wait of 15 seconds might trigger a timeout to happen after 20 seconds as an alternative of 25.

from selenium.webdriver.widespread.by import By
search_box = driver.find_element(By.NAME, "q")
search_button = driver.find_element(By.NAME, "btnK")

Earlier than interacting with a component, we now have to first discover the ingredient. This search is finished by means of specifying and filtering the specified components’ attribute. Within the above instance, we’re filtering by the identify attribute. Different attributes obtainable for the By class are as follows:

ID = "id"
XPATH = "xpath"
LINK_TEXT = "hyperlink textual content"
PARTIAL_LINK_TEXT = "partial hyperlink textual content"
NAME = "identify"
TAG_NAME = "tag identify"
CLASS_NAME = "class identify"
CSS_SELECTOR = "css selector"

There’s additionally the find_elements() methodology if you wish to discover a couple of ingredient by the desired attribute. This methodology will return the record of components discovered.

from selenium.webdriver.widespread.keys import Keys
search_box.clear()
search_box.send_keys("Selenium" + Keys.ENTER)
search_button.click on()

There are 4 fundamental forms of instructions that may be executed on a component.

send_keys() — Varieties the supplied keys into an editable ingredient.

click on() — A click on command is executed on the middle of the ingredient.

clear() — Resets the content material of a component.

choose — There’s a Choose class to take actions on <choose> components simpler. First, you create a Choose object utilizing a WebElement that references a <choose> ingredient.

from selenium.webdriver.help.choose import Choose
select_element = driver.find_element(By.ID, 'selectElementID')
select_object = Choose(select_element)

Then use one of many following methods to pick out an choice:

1. Choose an <choice> primarily based upon the <choose> ingredient’s inner index

select_object.select_by_index(1)

2. Choose an <choice> primarily based upon its worth attribute

select_object.select_by_value('worth')

3. Choose an <choice> primarily based upon its textual content

select_object.select_by_visible_text('textual content')

Or if you happen to’re simply concerned about what <choice> components the <choose> ingredient comprises and which <choice> components are chosen:

all_available_options = select_object.choices
all_selected_options = select_object.all_selected_options
driver.find_element(By.NAME, "q").get_attribute("worth")

Parts comprise data that we are able to extract from. Discover within the above instance how we straight find the ingredient and requesting the worth attribute from it.

Usually occasions, we have to relocate the ingredient earlier than requesting ingredient data because the DOM has modified since we first find it. Now, there are 4 widespread classes from which we are able to question ingredient data:

1. Attributes and Properties

Utilizing get_attribute and get_property strategies, we are able to fetch our desired attributes and properties from a component.

link_text.get_attribute("href")
img.get_attribute("src")
search_input.get_property("worth")

2. Textual content Content material

textual content = driver.find_element(By.CSS_SELECTOR, "h1").textual content

3. CSS Worth

cssValue = driver.find_element(By.LINK_TEXT, "See Extra").value_of_css_property('coloration')

4. States

isDisplayed = driver.find_element(By.ID, 'textLbl').is_displayed()

is_displayed checks if the ingredient is seen or not.

isSelected = driver.find_element(By.CSS_SELECTOR, "enter[type='checkbox']").is_selected()

is_selected checks if the ingredient is chosen or not.

isEnabled = driver.find_element(By.NAME, 'btnK').is_enabled()

is_enabled checks if the ingredient is enabled or disabled. These strategies returns a boolean worth. True if the test passes and False in any other case.

driver.give up()

This quits the browser by default and the automation for this session ends.

And that’s it! Utilizing these 8 easy steps, you need to be capable of automate absolutely anything in your browser with Selenium in Python!

Hope this information has been useful and as all the time, thanks for studying!

More Posts