{"id":221286,"date":"2022-06-22T18:15:16","date_gmt":"2022-06-22T18:15:16","guid":{"rendered":"https:\/\/www.softwaretestinghelp.com\/?page_id=221286"},"modified":"2025-04-01T09:46:59","modified_gmt":"2025-04-01T09:46:59","slug":"javascriptexecutor-in-selenium","status":"publish","type":"page","link":"https:\/\/www.softwaretestinghelp.com\/javascriptexecutor-in-selenium\/","title":{"rendered":"How to use JavaScriptExecutor in Selenium: A Complete Guide"},"content":{"rendered":"<p><strong>In this hands-on tutorial you will learn what is JavaScriptExecutor, how to use JavaScriptExecutor in Selenium WebDriver, its types, syntax, and usage scenarios with programming code examples.<\/strong><\/p>\n<p><strong><span lang=\"EN-GB\">What is Selenium?<\/span><\/strong><\/p>\n<p>Selenium is an open-source web automation testing tool used to validate web applications across different browsers, such as Google Chrome, Microsoft Edge, Firefox, Safari, etc.<\/p>\n<p>Selenium also supports different platforms like Windows, macOS, and Linux.<\/p>\n<p><strong><em> <\/em><\/strong><\/p>\n<p><strong>Why JavaScriptExecutor?<\/strong><\/p>\n<p>In certain cases, selenium drivers can fail to handle a small number of web elements, such as when opening a webpage, and if in that place an unpredicted popup window arises, that will stop the selenium driver from finding the specified elements and this will cause inaccurate results.<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/JavaScriptExecutor-in-Selenium.png\"><img decoding=\"async\" class=\"alignnone wp-image-221398 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/JavaScriptExecutor-in-Selenium.png\" alt=\"JavaScriptExecutor in Selenium\" width=\"700\" height=\"394\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/JavaScriptExecutor-in-Selenium.png 700w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/JavaScriptExecutor-in-Selenium-300x169.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<p>Here comes the usage of JavaScriptExecutor for handling these types of web elements.<\/p>\n<h2>What is JavaScriptExecutor<\/h2>\n<p>JavaScriptExecutor simply acts like a terminal that is used to implement the JavaScript code through the Selenium driver. Generally, the selenium webdriver uses locators like XPath, id, CSS selector, etc.<\/p>\n<p>In case these locators failed to work, we can use JavaScriptExector. It does not need any extensions, we only need to import the package \u201c<strong>org.openqa.selenium.JavascriptExecutor\u201d<\/strong> in the script to use JavaScriptExecutor.<\/p>\n<h2>Types of JavaScriptExecutor<\/h2>\n<p>JavaScriptExecutor provides two types of methods that can handle any type of interaction using JavaScript in Selenium.<\/p>\n<ol>\n<li>ExecuteScript<\/li>\n<li>ExecuteAsyncScript<\/li>\n<\/ol>\n<p><strong>#1) ExecuteScript<\/strong><\/p>\n<p>The ExecuteScript is used only to execute JS in the subject of the presently accessed webpage in Selenium. This script runs as an unnamed function and the script can return different types of values.<\/p>\n<p><strong>#2) ExecuteAsyncScript<\/strong><\/p>\n<p>The ExecuteAsync Script method is used to execute the asynchronous JS in the present webpage. The major difference in the ExecuteAsync script is that the webpage continues parsing even while the Async script runs in the background, which improves the overall performance of the application.<\/p>\n<p>The ExecuteAsyncScript is mostly used when the sleep function has to be performed in the browser under test.<\/p>\n<p>Both methods return some value and store them in a variable.<\/p>\n<p><strong>The datatypes returned are:<\/strong><\/p>\n<ul>\n<li>WebElement<\/li>\n<li>Long<\/li>\n<li>String<\/li>\n<li>Boolean<\/li>\n<li>List<\/li>\n<\/ul>\n<h2>Get Started with JavaScriptExecutor<\/h2>\n<p><strong>To use JavaScriptExecutor in Selenium, we need to follow these three steps:<\/strong><\/p>\n<p><strong>#1) Import the package<\/strong><\/p>\n<p>First, we have to import the package.<\/p>\n<p><em>import org.openqa.selenium.JavascriptExecutor;<\/em><\/p>\n<p><strong>#2) Create a reference<\/strong><\/p>\n<p>For using JavaScriptExecutor, we have to create an object for the imported package.<\/p>\n<p><em>JavascriptExecutor obj = (JavascriptExecutor) driver<\/em><strong>;<\/strong><\/p>\n<p><strong>#3) Call the JavascriptExecutor method<\/strong><\/p>\n<p>After we created the object for JavascriptExecutor, Now call JavaScriptExecutor method by as follows:<\/p>\n<p><em>obj.executeScript(script, args);<\/em><\/p>\n<h2>Basic Syntax for JavaScriptExecutor<\/h2>\n<p>JavascriptExecutor obj = (JavascriptExecutor) driver;<br \/>\nobj.executeScript(script, args);<\/p>\n<h3>Some of the Scenarios for Using JSExecutor<\/h3>\n<p>Let\u2019s discuss some of the scenarios we could use the JavaScriptExecutor Interface for Selenium.<\/p>\n<p><strong>#1) To click the Button<\/strong><\/p>\n<p>js.executeScript(&#8220;document.getElementById(&#8216;enter element id&#8217;).click();&#8221;);<\/p>\n<p><strong>#2) To send a text without using the sendkeys() method<\/strong><\/p>\n<p>js.executeScript(\u201cdocument.getElementByID(\u2018element id \u2019).value = \u2018xyz\u2019;\u201d);<\/p>\n<p><strong>#3) To generate an alert pop-up window<\/strong><\/p>\n<p>js.executeScript(&#8220;alert(&#8216;Welcome To Sotware testing help&#8217;);&#8221;);<\/p>\n<p><strong>#4) To get InnerText of a Webpage<\/strong><\/p>\n<p>string Text = js.executeScript(\u201creturn document.documentElement.innerText;\u201d).toString();<\/p>\n<p><strong>#5) To refresh the browser window<\/strong><\/p>\n<p>js.executeScript(\u201clocation.reload()\u201d)<\/p>\n<p><strong>#6) To get the Title of the webpage<\/strong><\/p>\n<p>String titleText = js.executeScript(&#8220;return document.title;&#8221;).toString();<br \/>\nSystem.out.println(titleText);<\/p>\n<p><strong>#7) To scroll the webpage<\/strong><\/p>\n<ul>\n<li>To scroll the page vertically for 500 px:<br \/>\njs.executeScript(\u201cwindow.scrollBy(0,500)\u201d);<\/li>\n<li>To scroll the page vertically till the end:<br \/>\njs.executeScript(\u201cwindow.scrollBy(0,document.body.scrollHeight)\u201d);<\/li>\n<\/ul>\n<h3>JavaScriptExecutor in Selenium WebDriver Example<\/h3>\n<p>In this section, we will discuss writing the code and running a test scenario by using Java e and Selenium web driver.<\/p>\n<p><strong>Pre-requisites:<\/strong><\/p>\n<p><strong>For executing the code, make sure that your system has the following:<\/strong><\/p>\n<p><strong>#1) Selenium<\/strong><br \/>\nDownload Selenium&#8217;s latest stable version <a href=\"https:\/\/www.selenium.dev\/downloads\/\" target=\"_blank\" rel=\"noopener nofollow\">here.<\/a><\/p>\n<p><strong>#2) Java SDK<\/strong><br \/>\nDownload the Java Software Development Kit (JDK) from <a href=\"https:\/\/www.oracle.com\/java\/technologies\/downloads\/\" target=\"_blank\" rel=\"noopener nofollow\">here.<\/a><\/p>\n<p><strong>#3) Eclipse IDE for Java Developers<\/strong><br \/>\nDownload the Eclipse IDE from <a href=\"https:\/\/www.eclipse.org\/downloads\/\" target=\"_blank\" rel=\"noopener nofollow\">here.<\/a><\/p>\n<p><strong>#4) Chrome Driver<\/strong><br \/>\nDownload the Chrome web driver according to your Chrome version from the site <a href=\"https:\/\/sites.google.com\/chromium.org\/driver\/downloads\" target=\"_blank\" rel=\"noopener nofollow\">here.<\/a><\/p>\n<p><strong>Suggested Reading =&gt;&gt; <a href=\"https:\/\/www.softwaretestinghelp.com\/chromedriver-selenium\/\">ChromeDriver Selenium Tutorial<\/a><\/strong><\/p>\n<h3>Example of ExecuteAsyncScript<\/h3>\n<p>Let\u2019s write a simple code to illustrate an example using executeAsyncScript, where we will perform the following operations:<\/p>\n<ul>\n<li>Launch the browser.<\/li>\n<li>Open site <strong>\u201chttps:\/\/www.softwaretestinghelp.com\/\u201d.<\/strong><\/li>\n<li>The application waits for 5 sec to perform further action.<\/li>\n<\/ul>\n<p><strong>Code<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">import org.openqa.selenium.JavascriptExecutor;\r\nimport org.openqa.selenium.chrome.ChromeDriver;\r\nimport org.testng.annotations.Test;\r\n\r\n\r\npublic class ExecuteAsyncScript{\r\n   @Test\r\n   public void Softwaretestinghelp(){\r\n   System.setProperty(&quot;webdriver.chrome.driver&quot;,&quot;C:\\\\Users\\\\ADMIN\\\\Documents\\\\chromedriver.exe&quot;);\r\nChromeDriver driver = new ChromeDriver();\r\n\r\n\/\/Creating the object for JavascriptExecutor \r\nJavascriptExecutor obj = (JavascriptExecutor)driver;\r\n\r\n\/\/ Maximize the browser\r\ndriver.manage().window().maximize();\r\n\r\n\/\/ Launch Website\r\ndriver.get(&quot;https:\/\/www.softwaretestinghelp.com\/&quot;);\r\n\r\n\/\/Declare and set the start time \r\nlong start_time = System.currentTimeMillis();\r\n\r\nobj.executeAsyncScript(&quot;window.setTimeout(arguments&#x5B;arguments.length - 1], 5000);&quot;); \r\nSystem.out.println(&quot;Passed time: &quot; + (System.currentTimeMillis() - start_time)); \r\n\r\n}\r\n}<\/pre>\n<p><strong>Code Explanation<\/strong><\/p>\n<p><strong>Step1:<\/strong> Set the path of the Chrome web driver and instantiate the driver object.<br \/>\n<strong>Step2:<\/strong> Create the reference for the JavaScriptExecutor.<br \/>\n<strong>Step3:<\/strong> Maximize the browser.<br \/>\n<strong>Step4<\/strong>: Open the URL.<br \/>\n<strong>Step5<\/strong>: Get the starting time of the execution before the script waits for 5 seconds.<br \/>\n<strong>Step6:<\/strong> Then use the use executeAsyncScript() to wait 5 seconds.<br \/>\n<strong>Step7<\/strong>: Then, get the current time.<br \/>\n<strong>Step8:<\/strong> Subtract (current time \u2013 start time) which gives the passed time<br \/>\n<strong>Step9<\/strong>: Verify the output should display more than 5000 milliseconds.<\/p>\n<p><strong>Output<\/strong><\/p>\n<p><strong>Passed time: 5256<\/strong><\/p>\n<p>PASSED: Softwaretestinghelp<\/p>\n<p>===============================================<\/p>\n<p>Default test<\/p>\n<p>Tests run: 1, Failures: 0, Skips: 0<\/p>\n<p><strong>Image for output:<\/strong><\/p>\n<h3><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/AsyncScript.png\"><img decoding=\"async\" class=\"alignnone wp-image-221362 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/AsyncScript.png\" alt=\"AsyncScript\" width=\"600\" height=\"466\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/AsyncScript.png 600w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/AsyncScript-300x233.png 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><\/h3>\n<h3>Example of ExecuteScript<\/h3>\n<p><strong>We will use the following examples in the ExecuteScript:<\/strong><\/p>\n<ul>\n<li>Generate an Alert window using JavaScriptExecutor.<\/li>\n<li>Scroll Down using JavaScriptExecutor.\n<ul>\n<li>Scrolling by defining pixels.<\/li>\n<li>Scrolling up to the bottom of the page.<\/li>\n<\/ul>\n<\/li>\n<li>Capture Data and Navigate to different pages using JavaScriptExecutor.<\/li>\n<\/ul>\n<p>Let\u2019s get started with the first example<\/p>\n<h4>Example #1: Generate Alert Window Using JavaScriptExecutor<\/h4>\n<p>In this example, we will navigate to the \u201csoftwaretestinghelp\u201d website and create an alert using ExecuteScript.<\/p>\n<p><strong>Steps:<\/strong><\/p>\n<ul>\n<li>Launch the web browser.<\/li>\n<li>and open the site https:\/\/www.softwaretestinghelp.com\/.<\/li>\n<li>Display alert window.<\/li>\n<\/ul>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">import org.openqa.selenium.JavascriptExecutor;\r\nimport org.openqa.selenium.chrome.ChromeDriver;\r\nimport org.testng.annotations.Test;\r\n\r\n\r\npublic class ExecuteScript{\r\n   @Test\r\n   public void Softwaretestinghelp(){\r\n   System.setProperty(&quot;webdriver.chrome.driver&quot;,&quot;C:\\\\Users\\\\ADMIN\\\\Documents\\\\chromedriver.exe&quot;);\r\nChromeDriver driver = new ChromeDriver();\r\n\r\n\/\/Creating the object for JavascriptExecutor \r\nJavascriptExecutor obj = (JavascriptExecutor)driver;\r\n\r\n\/\/ Maximize the browser\r\ndriver.manage().window().maximize();\r\n\r\n\/\/ Launch Website\r\ndriver.get(&quot;https:\/\/www.softwaretestinghelp.com\/&quot;);\r\n\r\n\/\/Generate Alert window \r\nobj.executeScript(&quot;alert('Welcome to Sotware Testing Help');&quot;); \r\n\r\n}\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>When the code is executed successfully,<\/p>\n<ul>\n<li>Successfully navigated to the \u201cSoftaretestinghelp\u201d webpage.<\/li>\n<li>Alert window will be displayed (see image below).<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/output2.png\"><img decoding=\"async\" class=\"alignnone wp-image-221363 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/output2.png\" alt=\"Softaretestinghelp\" width=\"413\" height=\"154\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/output2.png 413w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/output2-300x112.png 300w\" sizes=\"(max-width: 413px) 100vw, 413px\" \/><\/a><\/p>\n<h4>Example #2: Scroll Down using JavaScriptExecutor.<\/h4>\n<p>In this example, we will discuss scrolling through the webpages using JavaScriptExecutor in two methods<\/p>\n<ul>\n<li>By defining pixels<\/li>\n<li>Scrolling up to the bottom of the webpage<\/li>\n<\/ul>\n<p><strong>#1) Scrolling webpages by defining the pixels<\/strong><\/p>\n<p>In this method, we will scroll through the pages using this basic syntax:<\/p>\n<p><strong><em>window.scrollBy(xnum,ynum);<\/em><\/strong><\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<p><strong>Xnum(Horizontal scrolling):<\/strong> It is the number required to define the pixels to scroll by, along the horizontal axis. Values greater than zero will scroll to the right side of the webpage, while values less than zero will scroll to the left side of the webpage.<\/p>\n<p><strong>Ynum(Vertical Scrolling):<\/strong> It is the number required to define the pixels to scroll along the vertical axis. Values greater than zero will scroll to the bottom side of the webpage, while values less than zero will scroll to the upside of the webpage.<\/p>\n<p>Let\u2019s see how to scroll the webpage vertically by 500px:<\/p>\n<ul>\n<li>Open the URL \u201chttps:\/\/www.softwaretestinghelp.com\/ \u201d.<\/li>\n<li>Scroll the page vertically by 500px.<\/li>\n<\/ul>\n<p><strong>Syntax:<\/strong><\/p>\n<p>js.executeScript(&#8220;window.scrollBy(0,500)&#8221;, &#8220;&#8221;);<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">import org.openqa.selenium.JavascriptExecutor;\r\nimport org.openqa.selenium.chrome.ChromeDriver;\r\nimport org.testng.annotations.Test;\r\n\r\n\r\npublic class ExecuteScript{\r\n  @Test\r\n  public void Softwaretestinghelp(){\r\n  System.setProperty(&quot;webdriver.chrome.driver&quot;,&quot;C:\\\\Users\\\\ADMIN\\\\Documents\\\\chromedriver.exe&quot;);\r\nChromeDriver driver = new ChromeDriver();\r\n\r\n\/\/Creating the object for JavascriptExecutor interface\r\nJavascriptExecutor js = (JavascriptExecutor)driver;\r\n\r\n\/\/ Maximize the browser\r\ndriver.manage().window().maximize();\r\n\r\n\/\/ Launch Website\r\ndriver.get(&quot;https:\/\/www.softwaretestinghelp.com\/&quot;);\r\n\r\n\/\/ Scroll Down by 500 pixels\r\njs.executeScript(&quot;window.scrollBy(0,500)&quot;, &quot;&quot;);\r\n\r\n}\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>When the code is implemented, the webpage is scrolled down by 500 pixels,<\/p>\n<p><strong>See the image below for output:<\/strong><\/p>\n<h3><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/Scrolled-down-by-500px.png\"><img decoding=\"async\" class=\"alignnone wp-image-221364 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/Scrolled-down-by-500px.png\" alt=\"Scrolled down by 500px\" width=\"600\" height=\"404\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/Scrolled-down-by-500px.png 600w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/Scrolled-down-by-500px-300x202.png 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><\/h3>\n<p><strong>#2) Scrolling webpages up to the bottom of the page:<\/strong><\/p>\n<p>In this example, we will discuss scrolling the web pages down to the bottom of the page.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p><strong>js.executeScript(&#8220;window.scrollBy(0,document.body.scrollHeight)&#8221;, &#8220;&#8221;);<\/strong><\/p>\n<p>Let\u2019s see how to scroll the webpage up to the bottom of the page:<\/p>\n<ul>\n<li>Open the URL \u201chttps:\/\/www.softwaretestinghelp.com\/ \u201d.<\/li>\n<li>Scroll down the page using executeScript \u201cwindow.scrollBy(0,document.body.scrollHeight)\u201d.<\/li>\n<\/ul>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">import org.openqa.selenium.JavascriptExecutor;\r\nimport org.openqa.selenium.chrome.ChromeDriver;\r\nimport org.testng.annotations.Test;\r\n\r\n\r\npublic class ExecuteScript {\r\n  @Test\r\n  public void Softwaretestinghelp(){\r\n    System.setProperty(&quot;webdriver.chrome.driver&quot;,&quot;C:\\\\Users\\\\ADMIN\\\\Documents\\\\chromedriver.exe&quot;);\r\nChromeDriver driver = new ChromeDriver();\r\n\r\n\/\/Creating the object\r\nJavascriptExecutor js = (JavascriptExecutor)driver;\r\n\r\n\/\/ Maximize the browser\r\ndriver.manage().window().maximize();\r\n\r\n\/\/ Launch Website\r\ndriver.get(&quot;https:\/\/www.softwaretestinghelp.com\/&quot;);\r\n\r\n\/\/ Scroll Down till the bottom of the page\r\njs.executeScript(&quot;window.scrollBy(0,document.body.scrollHeight)&quot;);\r\n\r\n}\r\n}<\/pre>\n<p><strong>OUTPUT:<\/strong><\/p>\n<p>After executing the code, the webpage is scrolled down to the bottom of the page,<\/p>\n<p><strong>See the below image for output:<\/strong><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/Scrolled-down.png\"><img decoding=\"async\" class=\"alignnone wp-image-221366 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/Scrolled-down.png\" alt=\"Scrolled down - javascriptexecutor in selenium\" width=\"599\" height=\"182\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/Scrolled-down.png 599w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/Scrolled-down-300x91.png 300w\" sizes=\"(max-width: 599px) 100vw, 599px\" \/><\/a><\/p>\n<h4>Example #3: Capture Webpage Data and Navigate to different pages.<\/h4>\n<p>In this example, we will<\/p>\n<ul>\n<li>Launch the site \u201chttps:\/\/www.softwaretestinghelp.com\/ \u201d.<\/li>\n<li>Fetch details of the website such as the Domain name of the current visiting website, URL of the website and, Title name of the website. Then navigate to a different page.<\/li>\n<\/ul>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">import org.openqa.selenium.JavascriptExecutor;\r\nimport org.openqa.selenium.chrome.ChromeDriver;\r\nimport org.testng.annotations.Test;\r\n\r\n\r\npublic class ExecuteScript{\r\n   @Test\r\n   public void Softwaretestinghelp(){\r\n   System.setProperty(&quot;webdriver.chrome.driver&quot;,&quot;C:\\\\Users\\\\ADMIN\\\\Documents\\\\chromedriver.exe&quot;);\r\nChromeDriver driver = new ChromeDriver();\r\n\r\n\/\/In the initial step, we have to create the object for JavascriptExecutor\r\nJavascriptExecutor obj= (JavascriptExecutor)driver;\r\n\r\n\/\/ Maximize the browser\r\ndriver.manage().window().maximize();\r\n\r\n\/\/ Launch Website\r\ndriver.get(&quot;https:\/\/www.softwaretestinghelp.com\/&quot;);\r\n\r\n\/\/Getting the Name of the website. \r\nString Websitename =obj.executeScript(&quot;return document.domain;&quot;).toString(); \r\nSystem.out.println(&quot;Websitename = &quot;+Websitename);\r\n\r\n\/\/Getting the Title of the website\r\nString Title = obj.executeScript(&quot;return document.title;&quot;).toString(); \r\nSystem.out.println(&quot;Title = &quot;+Title);\r\n\r\n\/\/Getting the URL of the website. \r\nString weburl = obj.executeScript(&quot;return document.URL;&quot;).toString(); \r\nSystem.out.println(&quot;URL = &quot;+weburl);\r\n\r\n\r\n\/\/move to the new webpage \r\nobj.executeScript(&quot;window.location = 'https:\/\/www.softwaretestinghelp.com\/selenium-tutorial-1\/'&quot;); \r\n}\r\n}<\/pre>\n<p><strong>OUTPUT:<\/strong><\/p>\n<p>After the code is implemented, we will get the name of the website, Url, and the title of the current website. Then it will take you to the new site.<\/p>\n<p>Websitename = www.softwaretestinghelp.com<\/p>\n<p>URL = https:\/\/www.softwaretestinghelp.com\/<\/p>\n<p>Title = Software Testing Help &#8211; FREE IT Courses and Business Software\/Services Reviews<\/p>\n<p>PASSED: Softwaretestinghelp<\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/titleoutput.png\"><img decoding=\"async\" class=\"alignnone wp-image-221371 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/titleoutput.png\" alt=\"titleoutput - javascriptexecutor in selenium\" width=\"599\" height=\"279\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/titleoutput.png 599w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/titleoutput-300x140.png 300w\" sizes=\"(max-width: 599px) 100vw, 599px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/navigation.png\"><img decoding=\"async\" class=\"alignnone wp-image-221374 size-full\" src=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/navigation.png\" alt=\"navigation\" width=\"600\" height=\"251\" srcset=\"https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/navigation.png 600w, https:\/\/www.softwaretestinghelp.com\/wp-content\/qa\/uploads\/2022\/06\/navigation-300x126.png 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<h2>Conclusion<\/h2>\n<p>JavaScriptExecutor in Selenium is like a terminal that is used when Webdriver is not working as expected because some pop-up or an alert can arise on the webpage. JavaScriptExecutor allows executing JavaScript code on the web page from selenium Webdriver, enabling it to handle different types of operations by using only Java.<\/p>\n<p>In this article, we learned two types: JavascriptExecutor in Selenium WebDriver and also how to use that in applications. Further, we covered various scenarios of using the JavaScriptExecutor on the webpage.<\/p>\n<p><strong>We learned the following by implementing practical examples:<\/strong><\/p>\n<ul>\n<li>Generate Alert window.<\/li>\n<li>Scrolling webpages by defining pixels.<\/li>\n<li>Scrolling webpages up to the bottom of the page.<\/li>\n<li>Retrieved Website details using JavaScriptExecutor.<\/li>\n<li>Navigated to different webpages using JavaScriptExecutor.<\/li>\n<\/ul>\n\r\n\t\t\t<div id=\"daexthefup-container\"\r\n\t\t\t\tclass=\"daexthefup-container daexthefup-layout-stacked daexthefup-alignment-center\"\r\n\t\t\t\tdata-post-id=\"221286\">\r\n\r\n\t\t\t\t<div class=\"daexthefup-feedback\">\r\n\t\t\t\t\t<div class=\"daexthefup-text\">\r\n\t\t\t\t\t\t<h3 class=\"daexthefup-title\">Was this helpful?<\/h3>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<div class=\"daexthefup-buttons-container\">\r\n\t\t\t\t\t\t<div class=\"daexthefup-buttons\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t<div class=\"daexthefup-yes daexthefup-button daexthefup-button-type-icon\" data-value=\"1\">\r\n\t\t\t\t\r\n                <svg>\r\n                    <defs>\r\n                        <style>.thumb-up-cls-1{fill:#c9c9c9;}.thumb-up-cls-2{fill:#e1e1e1;}.thumb-up-cls-3{fill:#676767;}<\/style>\r\n                    <\/defs>\r\n                    <g id=\"thumb_up\">\r\n                        <path class=\"thumb-up-cls-2 daexthefup-icon-circle\" d=\"m24,3c11.58,0,21,9.42,21,21s-9.42,21-21,21S3,35.58,3,24,12.42,3,24,3m0-1C11.85,2,2,11.85,2,24s9.85,22,22,22,22-9.85,22-22S36.15,2,24,2h0Z\" \/>\r\n                        <g>\r\n                            <rect class=\"thumb-up-cls-3 daexthefup-icon-secondary-color\" x=\"10\" y=\"20\" width=\"6\" height=\"15\" rx=\"1.5\" ry=\"1.5\" \/>\r\n                            <path class=\"thumb-up-cls-1 daexthefup-icon-primary-color\" d=\"m30.57,9.06l-.49-.1c-.81-.17-1.61.35-1.78,1.16l-5.3,11.74c-.17.81,3.16,1.61,3.97,1.78l1.96.41c.81.17,1.61-.35,1.78-1.16l2.18-10.27c.34-1.61-.7-3.21-2.31-3.56Z\" \/>\r\n                            <path class=\"thumb-up-cls-1 daexthefup-icon-primary-color\" d=\"m38.17,20h-18.67c-.83,0-1.5.67-1.5,1.5v12c0,.83.67,1.5,1.5,1.5h16.27c.71,0,1.33-.5,1.47-1.21l2.4-12c.19-.93-.53-1.8-1.47-1.8Z\" \/>\r\n                        <\/g>\r\n                    <\/g>\r\n                <\/svg>\t\t\t<\/div>\r\n\r\n\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t<div class=\"daexthefup-no daexthefup-button daexthefup-button-type-icon\" data-value=\"0\">\r\n\t\t\t\t\r\n                <svg>\r\n                    <defs>\r\n                        <style>.thumb-down-cls-1{fill:#c9c9c9;}.thumb-down-cls-2{fill:#e1e1e1;}.thumb-down-cls-3{fill:#676767;}<\/style>\r\n                    <\/defs>\r\n                    <g id=\"thumb_down\">\r\n                        <path class=\"thumb-down-cls-2 daexthefup-icon-circle\" d=\"m24,3c11.58,0,21,9.42,21,21s-9.42,21-21,21S3,35.58,3,24,12.42,3,24,3m0-1C11.85,2,2,11.85,2,24s9.85,22,22,22,22-9.85,22-22S36.15,2,24,2h0Z\" \/>\r\n                        <g>\r\n                            <rect class=\"thumb-down-cls-3 daexthefup-icon-secondary-color\" x=\"10\" y=\"13\" width=\"6\" height=\"15\" rx=\"1.5\" ry=\"1.5\" \/>\r\n                            <path class=\"thumb-down-cls-1 daexthefup-icon-primary-color\" d=\"m30.57,38.94l-.49.1c-.81.17-1.61-.35-1.78-1.16l-5.3-11.74c-.17-.81,3.16-1.61,3.97-1.78l1.96-.41c.81-.17,1.61.35,1.78,1.16l2.18,10.27c.34,1.61-.7,3.21-2.31,3.56Z\" \/>\r\n                            <path class=\"thumb-down-cls-1 daexthefup-icon-primary-color\" d=\"m38.17,28h-18.67c-.83,0-1.5-.67-1.5-1.5v-12c0-.83.67-1.5,1.5-1.5h16.27c.71,0,1.33.5,1.47,1.21l2.4,12c.19.93-.53,1.8-1.47,1.8Z\" \/>\r\n                        <\/g>\r\n                    <\/g>\r\n                <\/svg>\t\t\t<\/div>\r\n\r\n\t\t\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t<\/div>\r\n\r\n\t\t\t\t<div class=\"daexthefup-comment\">\r\n\t\t\t\t\t<div class=\"daexthefup-comment-top-container\">\r\n\t\t\t\t\t\t<label id=\"daexthefup-comment-label\" class=\"daexthefup-comment-label\"><\/label>\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t<div class=\"daexthefup-comment-character-counter-container\">\r\n\t\t\t\t\t\t\t\t<div id=\"daexthefup-comment-character-counter-number\"\r\n\t\t\t\t\t\t\t\t\tclass=\"daexthefup-comment-character-counter-number\"><\/div>\r\n\t\t\t\t\t\t\t\t<div class=\"daexthefup-comment-character-counter-text\"><\/div>\r\n\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t<\/div>\r\n\t\t\t\t\t<textarea id=\"daexthefup-comment-textarea\" class=\"daexthefup-comment-textarea\"\r\n\t\t\t\t\t\t\t\tplaceholder=\"Type your message\"\r\n\t\t\t\t\t\t\t\tmaxlength=\"\r\n\t\t\t\t\t\t\t\t400\t\t\t\t\t\t\t\t\t\"><\/textarea>\r\n\t\t\t\t\t<div class=\"daexthefup-comment-buttons-container\">\r\n\t\t\t\t\t\t<button class=\"daexthefup-comment-submit daexthefup-button\">Submit<\/button>\r\n\t\t\t\t\t\t<button class=\"daexthefup-comment-cancel daexthefup-button\">Cancel<\/button>\r\n\t\t\t\t\t<\/div>\r\n\t\t\t\t<\/div>\r\n\r\n\t\t\t\t<div class=\"daexthefup-successful-submission-text\">Thanks for your feedback!<\/div>\r\n\r\n\t\t\t<\/div>\r\n\r\n\t\t\t","protected":false},"excerpt":{"rendered":"<p>In this hands-on tutorial you will learn what is JavaScriptExecutor, how to use JavaScriptExecutor in Selenium WebDriver, its types, syntax, and usage scenarios with programming code examples. What is Selenium? Selenium is an open-source web automation testing tool used to validate web applications across different browsers, such as Google Chrome, &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"How to use JavaScriptExecutor in Selenium: A Complete Guide\" class=\"read-more button\" href=\"https:\/\/www.softwaretestinghelp.com\/javascriptexecutor-in-selenium\/#more-221286\" aria-label=\"Read more about How to use JavaScriptExecutor in Selenium: A Complete Guide\">Read more<\/a><\/p>\n","protected":false},"author":9,"featured_media":221398,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_acf_changed":false,"_helpful_pro_status":1,"footnotes":""},"categories":[214],"tags":[],"class_list":{"0":"post-221286","1":"page","2":"type-page","3":"status-publish","4":"has-post-thumbnail","6":"category-selenium-tutorials"},"acf":[],"_links":{"self":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages\/221286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/comments?post=221286"}],"version-history":[{"count":0,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/pages\/221286\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/media\/221398"}],"wp:attachment":[{"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/media?parent=221286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/categories?post=221286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softwaretestinghelp.com\/wp-json\/wp\/v2\/tags?post=221286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}