Python — Selenium All Notes with Projects

Selenium is an open-source web-based automation tool to test your web application. We will examine Selenium down to the finest details with some projects.

Setup

Before writing a single line of Python, youe need a Selenium Driver for your favorite web browser. The download links for the drivers are available here: Chrome, Edge, Firefox, and Safari. Follow the link for the browser of your choice and download the driver for the compatible version. We will use the Chromedriver. You can find for chrome compatible version is here chrome://settings/help

You have to install Selenium on a your Python environment. Easiest way using pip or whatever you like. We will use pip install selenium.

pip install selenium

— — — — — — — — — — — — — — — — — — — — — — — — — —

Project-1)

In this example, We will open https://www.python.org/ web page and then search “python jobs” automatically using the Chrome driver.

We we’ll try to figure out our first test project using Selenium and Python step by step. Here is the code snapshot

Explanation of the code

lines[1-3]: import the webdriver and Keys classes from Selenium and import time class. The webdriver class will connect you to a browser’s instance. The Keys class lets you emulate the stroke of keyboard keys, including special keys like “Shift” and “Return”. The time class will be used to hold for a few seconds web pagesline[5]: Create an instance of Chrome with the path of the driver that you downloaded before. We assume that the driver is in the same directory as the Python script that you will execute. This will open an instance of Chrome locally. This command lets you perform tests on it until you use the close() method to end the connection to the browser.line[6]: get() method of the driver to load a website. The .get() method not only starts loading a website but also waits for it to render completely before moving on to the next step.line[7]: Once the page loads successfully, you can use the .title attribute to access the textual title of the webpage. And output is Welcome to Python.orgline[8]: wait page 3 secondsline[10]:Let us submit a query in the search bar. First, select the element from the HTML DOM and enter a value into it and submit the form by emulating the Return key press. You can select the element using its CSS class, ID, its name attribute, or even the tag name. If you check the source of the query search bar, you notice that the name attribute of this DOM element is “q”. Therefore, you can use the find_element_by_name() method as follows to select the element.
line[11]:Once the DOM element is selected, you first need to clear its contents using the clear() method.line[12]: Enter a string as its value using the send_keys() method.line[13]:Finally, emulate the press of the Return key using Keys.RETURN.line[15]:You notice in the window that these actions trigger a change in the URL with the search results in the window. To confirm the current URL of the window, you can use the following command. And output is https://www.python.org/search/?q=python+jobs&submit=line[17]:To close the current session, use the close() method. It also disconnects the link with the browser.

— — — — — — — — — — — — — — — — — — — — — — — — — —

Project-2) Click Recaptcha

We want to login Gardrops , But Gardrops want to be sure yor are a bot or not That is why it uses reCATPCHA, In this example we will click to I am not A bot checkbox . Like below picture.

ReCAPTCHA is an IFrame. An IFrame (Inline Frame) is an HTML element that allows rendering a document within another HTML document on a webpage. We prefer to use IFrames when we aspire to host content from an external source on our webpage.

If a webpage contains multiple IFrames, then we will need to switch between them. Selenium Python API provides “switch_to.iframe (self, frame_reference)” method to move to a particular IFrame.

Here is the code explanation

lines[1-8]: You allready know this code lines on Project-1line[11]: We will find all iframe on this web page, using find_elements_by_tag_name("iframe") medhodline[12]: switch to first frame using switch_to_frame(frames[0]) methodline[14]: Find and click "recaptcha-checkbox-border" class using find_element_by_class_name().click() methodlines[16-17]: You allready know this code lines on Project-1

— — — — — — — — — — — — — — — — — — — — — — — — — —

Project-3) Selenium WebDriver and Execute JavaScript

You can basically execute Javascript codes with the Selenium WebDriver using execute_script()medthod. Every kind of Javascript codes you can execute just calling the method execute_script() .

Now we will call execute_script() method and will show the alert box in the webpage.

Here is the code explanation

lines[1-5]: You allready know this code lines on Project-1
line[7]: define variable a called js with javascript code
line[8]: call execute_script() method to execute java scriptcode

Some execute_script() method examples with javascript codes

userName=driver.find_element_by_xpath("//button[@name='username']")
driver.execute_script("arguments[0].click();", userName)
#find username button and click with javascript
userName= driver.find_element_by_xpath("//button[@name='username']")
password= driver.find_element_by_xpath("//button[
@name='password']")
driver.execute_script("arguments[0].click();arguments[1].click();", userName, password)

#you can have more than one JavaScript action in your statement
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
#To scroll the browser window with javascript
print(driver.execute_script('return document.getElementById("fsr").innerText'))
#return documan text

— — — — — — — — — — — — — — — — — — — — — — — — — —

Project-4) Get HTML Source of WebElement

If we need to get the source of the content of the element we can use innerHTMLattribute. Here is the innerHTML element output example.

<td>1</td><td>2</td>

If we need to get the source with the current element we can use outerHTMLattribute. Here is the outerHTML element output example.

<tr id="myPython"><td>1</td><td>2</td></tr>
lines[1-4]: You allready know this code lines on Project-1
line[6]:get the source of the contents of the elements in web page
line[9]:get the source of the elements in web page

— — — — — — — — — — — — — — — — — — — — — — — — — —

Project-5) Save and Load Cookies

You can get cookies from web page using get_cookies() method and you can add cookies back using add_cookie() method

lines[1-5]: You allready know this code lines on Project-1
line[2]:Pickling is a way to convert a python object (list, dict, etc.) into a character stream. That is why we immort pickle module
line[7]:You can save the current cookies as a Python object using pickle. We will use get_cookies() method to get cookiesline[9]:We will get cookies from pickle file and add them to web page using add_cookie() medhod

I am an computer engineer. I am interested technology since 2010.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store