Selenium with Java 51 - Execute JavaScript code from Selenium | How to use JavaScriptExecutor?

preview_player
Показать описание
Javascript executor
Javascript Interface
Alert using Javascript
Refresh the page using Javascript
Handle checkbox using Javascript
InnerText of JavascriptExecutor
Get the Title of a page using JavascriptExecutor
Get a domain of a page using JavascriptExecutor
Get a URL of a page using JavascriptExecutor
How to scroll a page using JavascriptExecutor
How to navigate to other page using JavascriptExecutor
How to get the type text of Javascript Executor
How to get height and width of a page using JavascriptExecutor

What is JavaScript
JavaScript is the preferred language inside the browser to interact with HTML dom.
This means that a Browser has JavaScript implementation in it and understands the JavaScript commands.

What is JavaScriptExecutor
 
JavaScriptExecutor is an Interface that helps to execute JavaScript through Selenium Webdriver.

JavaScriptExecutor provides two methods "executescript" & "executeAsyncScript“ to run JavaScript on the selected window or current page.

Alert Pop Window
We can create the alert pop-up using allowing code

(JavascriptExecutor)driver).executeScript(“alert('Hello')");

To Refresh The Page
We can refresh the page by using following code

To Handle Checkbox
To make the checkbox has checked

To make the checkbox has unchecked

To Get The Inner Text
We can the inner text of page by using following code

To Get The Title Of Page
We can get title of page by using following code

To Get The Domain Of Page
We can get Domain of page by using following code

To Get The URL Of Page
We can get URL of page by using following code

((JavascriptExecutor)driver).executeScript("return document.URL;").toString();

To Scroll The Page
To scroll the page vertically for 500px we use the following code

To scroll the page vertically till the end we use following code

To navigate other page
We can navigate to other page by using following code

To Get The Height And Width of page
We can height and width of a page by using following code

To Type The Text
We can type the text into a text box by using following code

Possible Interview Questions on javascript executor
What is Javascript
What is JavascriptExecutor
How to create alert using JavascriptExecutor
How to refresh the page using JavascriptExecutor
How to handle the checkbox using JavascriptExecutor
How to get the InnerText of Javascript Executor
How to get the title of a page using JavascriptExecutor
How to get a domain of a page using JavascriptExecutor
How to get a URL of a page using JavascriptExecutor
How to scroll a page using JavascriptExecutor
How to navigate to other page using JavascriptExecutor
How to get the type text of Javascript Executor
How to get height and width of a page using JavascriptExecutor

Code :

@Test
public void JavaScriptDemo() throws InterruptedException
{
WebDriver driver = new FirefoxDriver();

//((JavascriptExecutor)driver).executeScript("alert('Hello')");
//refresh the browser window using javascript


//String Text = ((JavascriptExecutor)driver).executeScript("return document.URL;").toString();

}
Рекомендации по теме
Комментарии
Автор

thank u !!! :) i've been searching for a hour to find that guerySelectorAll func. to how to use it. Thank u very much! :)

zhanmr.