Lightning talk #13 – Using Selenium for web automation

Selenium is a web automation tool that allows control of the browser. Combined with python, we can do great stuff in only a few lines.

Let’s take some examples.

For example, this one will simply open firefox and go to facebook.com, and then close itself.

C++

1

2

3

4

5

from selenium import webdriver

driver=webdriver.Firefox()

driver.get("https://facebook.com")

driver.close()

If we want to write some data into a webpage field, we just need to find the element we want to write into and pass it some keys.

C++

1

2

3

4

5

6

7

8

9

10

from selenium import webdriver

driver=webdriver.Chrome()

driver.get("https://facebook.com")

driver.find_element_by_id("email").send_keys(self.username)

driver.find_element_by_id("pass").send_keys(self.password)

driver.find_element_by_id("loginbutton").click()

driver.close()

Complete example

Let’s say we want to login on facebook – what do we usually do? We open a browser, go to facebook.com, write the credentials, and then press ok. Those are exactly the steps for doing that with selenium.

This will open firefox and login on facebook. For that, we will need geckodriver (to control firefox), or chromedriver (to control google chrome).

We can also further write a shell script to open that python script for us, with some set credentials.

C++

1

python Facebook.py-u'user'-p'pass'

find_element_by_

What else we can do with selenium? Well, the biggest feature is that we can find elements in the webpage. How we can do that?

find_element_by_id

C++

1

2

3

4

5

6

7

8

9

<html>

<body>

<form id="loginForm">

<input name="username"type="text"/>

<input name="password"type="password"/>

<input name="continue"type="submit"value="Login"/>

</form>

</body>

</html>

C++

1

login_form=driver.find_element_by_id('loginForm')

find_element_by_name

C++

1

2

3

4

5

6

7

8

9

10

<html>

<body>

<form id="loginForm">

<input name="username"type="text"/>

<input name="password"type="password"/>

<input name="continue"type="submit"value="Login"/>

<input name="continue"type="button"value="Clear"/>

</form>

</body>

</html>

C++

1

2

3

4

5

6

username=driver.find_element_by_name('username')

password=driver.find_element_by_name('password')

# this will give the 'login' buttton as it occurs before the 'clear' button

continue=driver.find_element_by_name('continue')

find_element_by_link_text

C++

1

2

3

4

5

6

7

<html>

<body>

<p>Are you sure you want toproceed?</p>

<ahref="continue.html">Continue</a>

<ahref="cancel.html">Cancel</a>

</body>

</html>

C++

1

2

continue_link=driver.find_element_by_link_text('Continue')

continue_link=driver.find_element_by_partial_link_text('Conti')

find_element_by_tag_name

C++

1

2

3

4

5

6

<html>

<body>

<h1>Welcome</h1>

<p>Site content goes here</p>

</body>

</html>

C++

1

heading1=driver_find_element_by_tag_name('h1')

find_element_by_class_name

C++

1

2

3

4

5

<html>

<body>

<pclass="content">Site content goes here</p>

</body>

</html>

C++

1

content=driver.find_element_by_class_name('content')

find_element_by_css_selector

C++

1

2

3

4

5

<html>

<body>

<pclass="content">Site content goes here</p>

</body>

</html>

C++

1

content=driver.find_element_by_css_selector('p.content')

Anything else?

What else you can do with selenium other than finding elements? Well, you can also wait until elements are visible before getting them (wait for page to load), you can manipulate text fields like any other human (write some text, write letter by letter, with random delay between characters, for example), you can press keys and key combinations, handle popups, close tabs, add new tabs, execute javascript, drag and drop events, and so much more. Prety much anything that you can do manually.

Newsletter

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
More information about cookies can be found here: Cookie policy

Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.

Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.