Showing posts with label JavascriptExecutor. Show all posts
Showing posts with label JavascriptExecutor. Show all posts

Execute JavaScript in selenium to perform operations on HTML elements

Now we know how to identify html elements using only selenium or execute javascript in selenium,

Now let's see how we can perform actions on different web elements like
textbox - how we can enter text
Button - click
How to get attribute of a html element...

For input buttons
//js.executeScript("return document.getElementsByName('commit')[0].click();")
//or
//element = (WebElement) js.executeScript("return document.getElementsByName('commit')[0];");
//((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
//or
//element.click();

Execute javaScript (locators) in selenium to identify HTML element

We have seen how to identify web elements using selenium locators...there are situations where selenium alone can't able to identify web elements, there we can execute javaScript commands in selenium...

JavaScript has it's own locators to identify web elements:

getElementById - Returns single element that matches the ID.

Play with scroll bars on a page in selenium and javaScript

There are some situations in which we want to scroll to a particular element or scroll up or down across the browser.

Selenium do not have it's own methods to perform this action, so we can use javascript to play on scroll lbars.

Let's see some of the ways:

All about JavaScriptExecutor in Selenium

As per the selenium official link on Selenium RC, core selenium is basically a javascript program (set of functions) which interprets and executes selenium commands using browser's built in javascript interpreter.

Sometimes we observe web controls doesn't react well to selenium commands, there we can directly use / execute javascript commands using webdriver,