Зміст курсу
Introduction to QA Automation Testing
Introduction to QA Automation Testing
Selenium - Interacting with Web Elements I
Summary
This chapter will acquaint you with some hands-on knowledge of interacting with elements on a webpage.
The first step of interacting with an element is locating that element and storing it as a reference. Similar to how we first create a variable to store an object before performing actions on it.
The main method of finding or locating an element on a web page is findElement
. This element expects an argument of type By.locator()
where By
is a mechanism for locating elements and it supports various locator
strategies for-example By.id
, By.name
, By.xpath
etc.
> Locator: `By.id`: Search elements by their ID;
> Locator: `By.name`: Search elements by their name;
> Locator: `By.className`: Search elements by their class name;
> Locator: `By.css`: Search elements by their css selector;
> Locator: `By.linkText`: Search element which matches a specific text;
> Locator: `By.xpath`: Search elements by XPATH;
There might be cases where the script fails to locate the element, even though the element is present on the page. This usually happens when the script tries to interact with the element instantly after the page is loaded. An error in such cases indicates that the element is not yet loaded when the line is executed. To solve this problem , there is a very useful wait
function, which is very common in any kind of selenium script.
Following is the syntax of the wait
method:
-
`condition`: A condition that is checked periodically until it's met. It can either be a built-in condition from Selenium's `until` module or a custom function.
-
`timeout`: The maximum amount of time (in milliseconds) the WebDriver will wait for the condition to be true.
-
`message`: (Optional) A custom error message that is displayed if the wait times out.
Following is a basic example of the wait
method:
The until
module provides us with some functions which can be used for specifying an element related condition. Following are some common functions provided by the until module:
until.elementLocated(By.locator())
: Until the target element is found.until.elementIsVisible()
: Until the element is visible. It waits till the specified element disappears.until.urlIs(url)
: Until the url matches the specified URL
There is an additional method called findElements
having exactly the same syntax as findElement
. It simply returns an array of all the elements which match the required parameter.
Now that we know how to find or locate elements, we should also be able to interact with them. The available methods for interaction primarily depend upon the type of elements.
For-example, if we have located a button element, we can simulate a click on that button using the click
method. Of course the click method will be available as an attribute of the located element.
On the other hand, if we have a search box , or any kind of text box, we can input text into it using the sendKeys
method.
Дякуємо за ваш відгук!