A Note Before Interactions
Slow Network Connections are evil for Selenium
Since right the after the statement to load the page the element is not displayed, because the page might not have been loaded fully depending upon the network connection speed. If this is the case then the best way to deal with this issue is to wait for some time so that the page loading is completed and the element is properly displayed and is ready to be interacted.
Also sometimes even if the element is present in the page but to make it visible the browser might take some time or the element’s visiblity might depend on some event on some other element, modal boxes are the best examples of this case. If the element is not visible the driver throws the ElementNotVisibleException. The reason why the WebDriver throws this exception is because from a user’s perspective an element cannot be interacted with if it is not visible to the user.
Here by visibility we mean that the CSS “display” property is set to be “none” not the CSS visibility property. For this all we need to do is add an explicit wait condition for the element to be visible.
Click is probably the most common action performed by a user. It simulates the same click event that triggers when a user of the web application clicks on any element.
We have already seen several previous examples similar to code below:
WebElement element = driver.findElement(By.id("some_id")); element.click();
This method enters a string of text into input fields or text area. Below is an example:
WebElement element = driver.findElement(By.id("input_field_id")); element.sendKeys("Some Text");
The submit() method is used for form elements like an input box. When a submit() method is called on an element the driver traverse upward in the HTML and tries to find a form element and triggers submit on the form. If the form element is not found it throws the “NoSuchElementException“. Below is an example:
WebElement element = driver.findElement(By.id("input_field_id")); element.submit();
As the name suggests the getText() method returns the text content from the specified element.
WebElement element = driver.findElement(By.id("input_field_id")); String content = element.getText();
Drop Down/Select Options
Selenium provides a class for the select element and three different methods to choose the option.
Select select = new Select(driver.findElement(By.id("element_id"))); //Method 1 select.selectByIndex(1); //0 based indexes //Method 2 select.selectByValue("option_value"); //Method 3 select.selectByVisibleText("text_on_option");
Consider following HTML:
<a href="www.example.com" onclick="doSomeAction();">Link Text</a>
In the above code, since it is an anchor tag, the click event does not fire hence the method the doSomeAction() does not get executed, based on different browsers event handling mechanisms.
Selenium selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.example.com"); selenium.fireEvent("link=Link Text", "click");
The AdvancedUserInteractions API provides support for more advanced user interaction. It can be a drag and drop event or selecting(clicking) multiple elements while holding the Control key. Below are examples of drag and drop event and the Clicking multiple elements while holding the Control key.
Drag and Drop
To enable an element to be dragged and dropped on some other element the Selenium API provides the dragAndDrop() method from the Actions class.
WebElement elementToBeDragged = driver.findElement(By.id("id1")); WebElement targetElement = driver.findElement(By.id("id2")); (new Actions(driver)).dragAndDrop(elementToBeDragged, targetElement).perform();
Simulating Sequence Of Events
The Actions class can be used to simulate multiple events at a time(for example, selecting multiple files by clicking, while holding the CTRL key). The code below uses multiple events to simulates the same action.
Actions builder = new Actions(driver); builder.keyDown(Keys.CONTROL) .click(someElement) .click(someOtherElement) .keyUp(Keys.CONTROL); Action selectMultiple = builder.build(); selectMultiple.perform();
Below is a list of the actions supported by the API.
- ButtonReleaseAction – Releasing a held mouse button
- ClickAction – Equivalent to WebElement.click()
- ClickAndHoldAction – Holding down the left mouse button
- ContextClickAction – Clicking the mouse button that (usually) brings up the contextual menu
- DoubleClickAction – double-clicking an element
- KeyDownAction – Holding down a modifier key
- KeyUpAction – Releasing a modifier key
- MoveMouseAction – Moving the mouse from its current location to another element
- MoveToOffsetAction – Moving the mouse to an offset from an element (The offset could be negative and the element could be the same element that the mouse has just moved to)
- SendKeysAction – Equivalent to WebElement.sendKey(…)
We are ready to take a look at the Selenium Explicit and Implicit Waits. Let us know any issues regarding the above topic.