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

Pubblicato il: 10 dicembre 2018
sul canale di: Ankpro Training
1,517
10

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

((JavascriptExecutor)driver).executeScript("history.go(0)");

To Handle Checkbox
To make the checkbox has checked
((IJavaScriptExecutor)driver).ExecuteScript("document.querySelectorAll('input[value = read]')[0].click()");

To make the checkbox has unchecked
((IJavaScriptExecutor)driver).ExecuteScript("document.querySelectorAll('input[value = read]')[0].click()");

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

((JavascriptExecutor)driver).executeScript("return document.documentElement.innerText;").toString();

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

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

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

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

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

((JavascriptExecutor)driver).executeScript("window.scrollBy(0,500)");

To scroll the page vertically till the end we use following code
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,document.body.scrollHeight)");

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

(JavascriptExecutor)driver).executeScript("window.location='http://uitestpractice.com'");

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

((JavascriptExecutor)driver).executeScript("return window.innerHeight;").toString();
((JavascriptExecutor)driver).executeScript("return window.innerWidth;").toString();

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

((JavascriptExecutor)driver).executeScript("document.getElementById('Email').value='abc@abc.com';");

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();
driver.get("http://ankpro.com");

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

driver.get("http://uitestpractice.com/Students/Form");
//((IJavaScriptExecutor)driver).ExecuteScript("document.getElementByValue('dance').checked=true;");
((JavascriptExecutor)driver).executeScript("document.querySelectorAll('input[value = read]')[0].click()");

//String sText = ((JavascriptExecutor)driver).executeScript("return document.documentElement.innerText;").toString();
//System.out.println(sText);

//String sText1 = ((JavascriptExecutor)driver).executeScript("return document.title;").toString();
//System.out.println(sText1);

//String Text = ((JavascriptExecutor)driver).executeScript("return document.domain;").toString();
//System.out.println(Text);

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

driver.quit();
}


In questa pagina del sito puoi guardare il video online Selenium with Java 51 - Execute JavaScript code from Selenium | How to use JavaScriptExecutor? della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Ankpro Training 10 dicembre 2018, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 1,517 volte e gli è piaciuto 10 spettatori. Buona visione!