Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Learning Selenium Testing Tools - Third Edition
Learning Selenium Testing Tools - Third Edition

Learning Selenium Testing Tools - Third Edition: Leverage the power of Selenium to build your own real-time test cases from scratch , Third Edition

eBook
€20.98 €29.99
Paperback
€36.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Learning Selenium Testing Tools - Third Edition

Chapter 2. Locators

Locators allow us to find elements on a page that can be used in our tests. In the last chapter, we managed to work against a page that had decent locators. In HTML, it is seen as a good practice to make sure that every element you need to interact with has an ID attribute and a name attribute. Unfortunately, following best practices can be extremely difficult, especially when building HTML dynamically on the server before sending it back to the browser.

In this chapter, we will cover the following topics:

  • Locate elements by ID
  • Locate elements by name
  • Locate elements by link
  • Locate elements by XPath
  • Locate elements by CSS
  • Locate elements by DOM

So, let's get on with it.

Note

Before starting this chapter, we should begin by making sure that we have all the relevant applications installed. While these are not foolproof, they will give us an idea of how to construct the locator for our tests.

The following are the browser add-ons that will help us inspect and locate...

Locating elements by ID

On web applications today, elements should have an ID attribute for all their controls on the page. A control is an element that we can interact with and is not static text. This allows Selenium to find the unique item, since IDs are unique, and then complete the action that it needs to do against that element. ID locators usually have the highest speed, especially when compared to XPath.

Finding IDs of elements on the page with Firebug

In this section, we will find a web button with an ID that is on the page. You will need to have Firebug installed for this. We will look at how to find the ID of an element using Firefox:

  1. Navigate to http://book.theautomatedtester.co.uk/chapter2 and click on the Firebug icon present in the Firefox browser toolbar shown in the following screenshot, or start Firebug by pressing the F12 key:
    Finding IDs of elements on the page with Firebug
  2. Click on the Select Element icon in Firebug . Finding IDs of elements on the page with Firebug
  3. Move your mouse over the element that you wish to have a look at.
  4. Move your mouse over different elements...

Moving elements on the page

As I just mentioned, Selenium, when using the value of the ID attribute, can find elements on a page even if they are moved. Click on the button with the text Random on the Chapter 2 page of the site (you can do this manually), and then run the script that we created earlier. You will see that your test executes successfully.

Finding elements by name

Elements do not necessarily have ID attributes on all of them. Elements can have names that we can use to locate them. In the Target textbox, this would look like name=Element. Try the following example to see how it works:

  1. Open Selenium IDE.
  2. Navigate to http://book.theautomatedtester.co.uk/chapter2 and click on the Firebug icon.
  3. Find any element that you want to interact with and, in the Target textbox of Selenium IDE, place the value of its name attribute. For example, use but2, as in the following screenshot, against http://book.theautomatedtester.co.uk/chapter2:
    Finding elements by name
  4. Type the click command into the Command select box.
  5. Play...

Using direct XPath in your test

As I mentioned in the first part of this section, having // as the start of your XPath is seen as a greedy query since it will parse the entire DOM until it finds the element that you want to find. If you want to work against an element that is always in a certain place, you can use a more direct XPath.

Finding elements by direct XPath

Instead of using //, you can use a single /, but you will need to make sure that the first node in your query is HTML. Let's see an example of this:

  1. Open Selenium IDE.
  2. Navigate to http://book.theautomatedtester.co.uk/chapter2.
  3. Type xpath=/html/body/div[2]/div[3]/input into the Target input of Selenium IDE.
  4. Click on the Find button.

The previous locator will find the same element as before. This type of XPath query will find the element fractionally quicker, but if your UI is going to change, it may fail if the element is moved into a different area of the page. One thing to really note is that XPath locators can be extremely...

Leveraging the XPath axis with elements

As we have seen, XPath is normally only used if the element we need to interact with is not accessible by normal means. In this section of the chapter, we will have a look at leveraging the XPath axis in our queries to find the element that we wish to interact with. An example that I have used in the real world was to find a table cell that had specific text, then traverse the tree backwards to find the edit button so that I can click on it. This may seem laborious just to click on an edit button, but it is extremely common according to the Selenium users forum on Google Groups.

Using the XPath axis

In the first example, we found a button and then its sibling. In this example, the query that we will generate is equivalent to xpath=//div[@class='leftdiv']/input[2].

  1. We will start by finding the first element for our query, which is //input[@value='Button with ID']. Place this element into the Selenium IDE Target textbox and see which...

CSS selectors

We saw in the previous section that XPath selectors can offer your tests a lot of flexibility to find elements on the page. Here, we will find the elements using CSS selectors (selectors are patterns used to select the elements you want to style.)

Note

It must be noted that Selenium IDE and Selenium RC use Sizzle, the framework used for selectors in jQuery, to find elements on the page. Not all of these can be translated to work in Selenium WebDriver.

Finding elements by CSS

We discussed that finding elements by XPath can be an extremely costly exercise. A way around this is to use CSS selectors to find the objects that you need. Selenium is compatible with CSS 1.0, CSS 2.0, and CSS 3.0 selectors. There are a number of items that are supported, such as namespace in CSS 3.0, and some pseudo classes and pseudo elements.

The syntax of your locator will look like css=cssSelector. Let's create our first selector to find an element on our page:

  1. Open Selenium IDE.
  2. Navigate to http...

Locating elements by ID


On web applications today, elements should have an ID attribute for all their controls on the page. A control is an element that we can interact with and is not static text. This allows Selenium to find the unique item, since IDs are unique, and then complete the action that it needs to do against that element. ID locators usually have the highest speed, especially when compared to XPath.

Finding IDs of elements on the page with Firebug

In this section, we will find a web button with an ID that is on the page. You will need to have Firebug installed for this. We will look at how to find the ID of an element using Firefox:

  1. Navigate to http://book.theautomatedtester.co.uk/chapter2 and click on the Firebug icon present in the Firefox browser toolbar shown in the following screenshot, or start Firebug by pressing the F12 key:

  2. Click on the Select Element icon in Firebug .
  3. Move your mouse over the element that you wish to have a look at.

  4. Move your mouse over different elements...

Moving elements on the page


As I just mentioned, Selenium, when using the value of the ID attribute, can find elements on a page even if they are moved. Click on the button with the text Random on the Chapter 2 page of the site (you can do this manually), and then run the script that we created earlier. You will see that your test executes successfully.

Finding elements by name

Elements do not necessarily have ID attributes on all of them. Elements can have names that we can use to locate them. In the Target textbox, this would look like name=Element. Try the following example to see how it works:

  1. Open Selenium IDE.

  2. Navigate to http://book.theautomatedtester.co.uk/chapter2 and click on the Firebug icon.

  3. Find any element that you want to interact with and, in the Target textbox of Selenium IDE, place the value of its name attribute. For example, use but2, as in the following screenshot, against http://book.theautomatedtester.co.uk/chapter2:

  4. Type the click command into the Command select box.

  5. Play...

Using direct XPath in your test


As I mentioned in the first part of this section, having // as the start of your XPath is seen as a greedy query since it will parse the entire DOM until it finds the element that you want to find. If you want to work against an element that is always in a certain place, you can use a more direct XPath.

Finding elements by direct XPath

Instead of using //, you can use a single /, but you will need to make sure that the first node in your query is HTML. Let's see an example of this:

  1. Open Selenium IDE.

  2. Navigate to http://book.theautomatedtester.co.uk/chapter2.

  3. Type xpath=/html/body/div[2]/div[3]/input into the Target input of Selenium IDE.

  4. Click on the Find button.

The previous locator will find the same element as before. This type of XPath query will find the element fractionally quicker, but if your UI is going to change, it may fail if the element is moved into a different area of the page. One thing to really note is that XPath locators can be extremely fragile...

Left arrow icon Right arrow icon

Description

If you are a software developer with a basic knowledge of testing and are interested in automated testing using Selenium, this is the book for you. No prior knowledge of Selenium is required.

Who is this book for?

If you are a software developer with a basic knowledge of testing and are interested in automated testing using Selenium, this is the book for you. No prior knowledge of Selenium is required.

What you will learn

  • Understand designing and implementing the automation framework
  • Understand and implement AJAX in your web pages
  • Set up Selenium WebDriver in both IntelliJ and Eclipse
  • Build test suites in Selenium using PageObjects
  • Get to know about WebElement handling with Selenium WebDriver
  • Install Selenium WebDriver for mobile devices
  • Understand and learn testing in Selenium Grid
Estimated delivery fee Deliver to Germany

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Feb 24, 2015
Length: 318 pages
Edition : 3rd
Language : English
ISBN-13 : 9781784396497
Category :
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Germany

Premium delivery 7 - 10 business days

€17.95
(Includes tracking information)

Product Details

Publication date : Feb 24, 2015
Length: 318 pages
Edition : 3rd
Language : English
ISBN-13 : 9781784396497
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 88.97
Learning Selenium Testing Tools - Third Edition
€36.99
Selenium Design Patterns and Best Practices
€26.99
Selenium Essentials
€24.99
Total 88.97 Stars icon
Banner background image

Table of Contents

16 Chapters
1. Getting Started with Selenium IDE Chevron down icon Chevron up icon
2. Locators Chevron down icon Chevron up icon
3. Overview of the Selenium WebDriver Chevron down icon Chevron up icon
4. Finding Elements Chevron down icon Chevron up icon
5. Design Patterns Chevron down icon Chevron up icon
6. Working with WebDriver Chevron down icon Chevron up icon
7. Automation Framework Development and Building Utilities Chevron down icon Chevron up icon
8. Mobile Devices Chevron down icon Chevron up icon
9. Getting Started with the Selenium Grid Chevron down icon Chevron up icon
10. Advanced User Interactions Chevron down icon Chevron up icon
11. Working with HTML5 Chevron down icon Chevron up icon
12. Advanced Topics Chevron down icon Chevron up icon
13. Migrating from Remote Control to WebDriver Chevron down icon Chevron up icon
A. Automation Prerequisites for Selenium Automation Chevron down icon Chevron up icon
B. Answers for Self-test Questions Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.4
(11 Ratings)
5 star 63.6%
4 star 9.1%
3 star 27.3%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Amazon Customer Feb 19, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Clearly written with good examples. Was able to get going quickly.
Amazon Verified review Amazon
Bhushan Jun 03, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Perfect book for fresher’s and new learnerWell summarized each and every feature
Amazon Verified review Amazon
Naveen Bharadwaj May 22, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I just started my career in automation team and selenium was a complete rocket science for me. The book authored by Raghavendra Prasad MG has been a great guide to me the whole time. Things are explained from scratch and any person who knows or understands English can learn selenium if he/she practices what's given in the book. Of-course, the person reading the book must have that interest or the need to learn selenium. To finalize, this is an extremely well written book with screenshots at every stage explaining how to go about with things. The book is a very good deal for the price it is being sold at, because what one can make after learning selenium is no-where close to the cost at what the book is sold.
Amazon Verified review Amazon
Mike Ludemann Jul 18, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Alle wichtigen Information und mehr sind vorhanden.
Amazon Verified review Amazon
buya Jun 28, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Ich habe gleich zwei Bücher gekauft für die Automatiserung mit dem Selenium Framework.Dieses Buch bietet einen guten Leitfaden.Im Vergleich zu dem anderen Buch welches ich mir auch über Amazon erworben habe, ist dieses Buch besser in der Praxis einsetzbar. Ich habe dieses Buch als Grundlage für das Selbstudium gekauft und bin recht zufrieden damit.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact [email protected] with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at [email protected] using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on [email protected] with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on [email protected] within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on [email protected] who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on [email protected] within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela