Selenium with C# 51 - IJavaScriptExecutor interface | How to execute JavaScript from Selenium

preview_player
Показать описание
Javascript executor
IJavascript Interface
Alert using JavascriptExecutor
Refresh the page using JavascriptExecutor
Handle checkbox using JavascriptExecutor
InnerText using 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
Type text of JavascriptExecutor
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 IJavaScriptExecutor?
 
IJavaScriptExecutor is an Interface that helps to execute JavaScript through Selenium Webdriver.

IJavaScriptExecutor 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
(IJavaScriptExecutor)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
((IJavaScriptExecutor)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 IJavaScriptExecutor
What is Javascript
What is IJavascriptExecutor
How to create alert using IJavaScriptExecutor
How to refresh the page using IJavaScriptExecutor
How to handle the checkbox using IJavaScriptExecutor
How to get the InnerText of javascript Executor
How to get the title of a page using IJavaScriptExecutor
How to get a domain of a page using IJavaScriptExecutor
How to get a URL of a page using IJavaScriptExecutor
How to scroll a page using IJavascriptExecutor
How to navigate to other page using IJavascriptExecutor
How to get the height and width of a page using IJavascript Executor
How to type text into text box using IJavaScriptExecutor

Code :
[TestMethod]
public void JavaScriptDemo()
{
IWebDriver driver = new FirefoxDriver();

//((IJavaScriptExecutor)driver).ExecuteScript("prompt('Hello')");
//((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", "element");

//Console.WriteLine(sText);

//Console.WriteLine(sText1);

//Console.WriteLine(Text);

Thread.Sleep(2000);
driver.Quit();
}
Рекомендации по теме
Комментарии
Автор

Thank you for excellent explanation . Very useful video.

sv
Автор

tank yo for The :). I could not understand one work.

wasimmazahereh
Автор

So thanks for I just I have found the scrolldown code in your video:)and it worked:)

ugurerol
Автор

Thank you! This video really helped a lot!

mahadevg
Автор

How can I perform my action(scroll down and click) on particular div of my webpage. I tried lot, doesn't helped me.
Please help me resolve my issue.

mohamedmoussaoui
Автор

Hello, thank you for the video.
Can you pls tell how to use the javascript executor to accept a pop up alert ? or a pop up confirmation? Thanks.

adebowaledollars
Автор

Hello Ankpro Technologies,
How can we scroll down and scroll up by using the c#, Could you please reply me.

nareshk
Автор

How can i get text of a disabled input field

shruthidonthiri
Автор

Can you please make a tutorial on selenium using javascript???

srikanthappari