How to use executeScript method of org.openqa.selenium.Interface JavascriptExecutor class

Best Selenium code snippet using org.openqa.selenium.Interface JavascriptExecutor.executeScript

Source:JavaScriptUtil.java Github

copy

Full Screen

...36 //https://www.twolugs.com/37 public static void scrollIntoView(WebElement element, WebDriver driver)38 {39 JavascriptExecutor js = (JavascriptExecutor)driver;40 js.executeScript("arguments[0].scrollIntoView(true);", element);41 }42 //Vertical scroll down by 600 pixels 43 //js.executeScript("window.scrollBy(0,600)");44 45 46 public static void verticalScrollDown(WebDriver driver)47 { 48 //Launching the Site. 49 driver.get("http://moneyboats.com/"); 50 51 //Maximize window 52 driver.manage().window().maximize();53 JavascriptExecutor js = (JavascriptExecutor)driver;54 js.executeScript("window.scrollBy(0,600)");55 }56 public static void scrollPageDown(WebDriver driver)57 {58 JavascriptExecutor js = (JavascriptExecutor)driver;59 js.executeScript("window.scrollTo(0,document.body.scrollHeight)");60 }61/*********************************Flushing by changing Color*********************************************/ 62 public static void flash(WebElement element, WebDriver driver)63 {64 65 //The method 'getCssValue("backgroundColor");' capture the color66 String bgcolor = element.getCssValue("backgroundColor");67 68 for(int i = 0; i < 50; i++)//Changing the color 50 times69 {70 changeColor("#000000", element, driver);//1-->Black-->#00000071 changeColor(bgcolor, element, driver);//2-->Going back bgcolor(backgroundColor72 }73 }74 75 public static void changeColor(String color,WebElement element,WebDriver driver)76 77 {78 JavascriptExecutor js = (JavascriptExecutor)driver;79 js.executeScript("arguments[0].style.backgroundColor = '" + color + "'", element);80 try81 {82 Thread.sleep(2000);83 } catch (InterruptedException e) 84 {85 86 e.printStackTrace();87 }88 }89/******************************End Changing color*************************************************/ 90 91 public static void drawBorder(WebElement element, WebDriver driver)92 {93 JavascriptExecutor js = (JavascriptExecutor)driver;94 js.executeScript("arguments[0].style.border='3px solid red'", element);95 }96 97 //Capture title of the page98 //driver.getTitle();99 public static String getTitle(WebDriver driver)100 { // //Creating the JavascriptExecutor interface object by Type casting101 JavascriptExecutor js = (JavascriptExecutor)driver;102 String title = js.executeScript("return document.title;").toString();103 return title;104 }105 106 public static void clickElementByJS(WebElement element,WebDriver driver)107 { // //Creating the JavascriptExecutor interface object by Type casting108 JavascriptExecutor js = (JavascriptExecutor)driver;109 js.executeScript("arguments[0].click();", element);110 111 }112 113 114 public static void generateAlert(WebDriver driver,String message)115 { // //Creating the JavascriptExecutor interface object by Type casting116 JavascriptExecutor js = (JavascriptExecutor)driver;117 js.executeScript("alert('" + message +"')");118 119 }120 121 //refreshing page122 //driver.navigate().refresh();123 124 public static void refreshBrowserByJS(WebDriver driver)125 { //Creating the JavascriptExecutor interface object by Type casting126 JavascriptExecutor js = (JavascriptExecutor)driver;127 js.executeScript("history.go(0)");128 129 }130 131//==================================================================================132 133//JavaScriptExecutor is an Interface that helps to execute JavaScript 134//through Selenium Webdriver. JavaScriptExecutor provides two methods 135//"executescript" & "executeAsyncScript" to run javascript on the selected 136//window or current page.137 138/*139Summary:140JavaScriptExecutor is used when Selenium Webdriver fails to click on any 141element due to some issue....

Full Screen

Full Screen

Source:DebugMainMethod.java Github

copy

Full Screen

...34 //https://www.twolugs.com/35 public static void scrollIntoView(WebElement element, WebDriver driver)36 {37 JavascriptExecutor js = (JavascriptExecutor)driver;38 js.executeScript("arguments[0].scrollIntoView(true);", element);39 }40 //Vertical scroll down by 600 pixels 41 //js.executeScript("window.scrollBy(0,600)");42 43 44 public static void verticalScrollDown(WebDriver driver)45 { 46 //Launching the Site. 47 driver.get("http://moneyboats.com/"); 48 49 //Maximize window 50 driver.manage().window().maximize();51 JavascriptExecutor js = (JavascriptExecutor)driver;52 js.executeScript("window.scrollBy(0,600)");53 }54 public static void scrollPageDown(WebDriver driver)55 {56 JavascriptExecutor js = (JavascriptExecutor)driver;57 js.executeScript("window.scrollTo(0,document.body.scrollHeight)");58 }59/*********************************Flushing by changing Color*********************************************/ 60 public static void flash(WebElement element, WebDriver driver)61 {62 63 //The method 'getCssValue("backgroundColor");' capture the color64 String bgcolor = element.getCssValue("backgroundColor");65 66 for(int i = 0; i < 50; i++)//Changing the color 50 times67 {68 changeColor("#000000", element, driver);//1-->Black-->#00000069 changeColor(bgcolor, element, driver);//2-->Going back bgcolor(backgroundColor70 }71 }72 73 public static void changeColor(String color,WebElement element,WebDriver driver)74 75 {76 JavascriptExecutor js = (JavascriptExecutor)driver;77 js.executeScript("arguments[0].style.backgroundColor = '" + color + "'", element);78 try79 {80 Thread.sleep(2000);81 } catch (InterruptedException e) 82 {83 84 e.printStackTrace();85 }86 }87/******************************End Changing color*************************************************/ 88 89 public static void drawBorder(WebElement element, WebDriver driver)90 {91 JavascriptExecutor js = (JavascriptExecutor)driver;92 js.executeScript("arguments[0].style.border='3px solid red'", element);93 }94 95 //Capture title of the page96 //driver.getTitle();97 public static String getTitle(WebDriver driver)98 { // //Creating the JavascriptExecutor interface object by Type casting99 JavascriptExecutor js = (JavascriptExecutor)driver;100 String title = js.executeScript("return document.title;").toString();101 return title;102 }103 104 public static void clickElementByJS(WebElement element,WebDriver driver)105 { // //Creating the JavascriptExecutor interface object by Type casting106 JavascriptExecutor js = (JavascriptExecutor)driver;107 js.executeScript("arguments[0].click();", element);108 109 }110 111 112 public static void generateAlert(WebDriver driver,String message)113 { // //Creating the JavascriptExecutor interface object by Type casting114 JavascriptExecutor js = (JavascriptExecutor)driver;115 js.executeScript("alert('" + message +"')");116 117 }118 119 //refreshing page120 //driver.navigate().refresh();121 122 public static void refreshBrowserByJS(WebDriver driver)123 { //Creating the JavascriptExecutor interface object by Type casting124 JavascriptExecutor js = (JavascriptExecutor)driver;125 js.executeScript("history.go(0)");126 127 }128 129//==================================================================================130 131//JavaScriptExecutor is an Interface that helps to execute JavaScript 132//through Selenium Webdriver. JavaScriptExecutor provides two methods 133//"executescript" & "executeAsyncScript" to run javascript on the selected 134//window or current page.135 136/*137Summary:138JavaScriptExecutor is used when Selenium Webdriver fails to click on any 139element due to some issue....

Full Screen

Full Screen

Source:JavaScriptUtility.java Github

copy

Full Screen

...30 31 private void changeColor(String color,WebElement element,WebDriver driver)32 {33 JavascriptExecutor js=(JavascriptExecutor)driver;34 js.executeScript("arguments[0].style.backgroundColor='"+color+"'",element);35 36 try37 {38 Thread.sleep(2000);39 }40 catch(InterruptedException ie)41 {42 43 }44 }45 46 public void navigateToWebsite(WebDriver driver,String url)47 {48 JavascriptExecutor js=(JavascriptExecutor)driver;49 js.executeScript("window.location='"+url+"'");50 }51 52 public void getPageTitle(WebDriver driver)53 {54 JavascriptExecutor js=(JavascriptExecutor)driver;55 String title=js.executeScript("return document.title;").toString();56 System.out.println(title);57 }58 59 public void enterText(WebDriver driver,String ID,String value)60 {61 JavascriptExecutor js=(JavascriptExecutor)driver;62 js.executeScript("document.getElementById('"+ID+"').value='"+value+"'");63 }64 65 public void click(WebDriver driver,WebElement element)66 {67 JavascriptExecutor js=(JavascriptExecutor)driver;68 js.executeScript("arguments[0].click();",element);69 }70 71 public void drawBorder(WebElement element,WebDriver driver)72 {73 JavascriptExecutor js=(JavascriptExecutor)driver;74 js.executeScript("arguments[0].style.border='3px solid red'",element);75 }76 77 public void generateAlert(WebDriver driver,String message)78 {79 JavascriptExecutor js=(JavascriptExecutor)driver;80 js.executeScript("alert('"+message+"')");81 }82 83 public void refreshBrowser(WebDriver driver)84 {85 JavascriptExecutor js=(JavascriptExecutor)driver;86 js.executeScript("history.go(0)");87 }88 89 public String getPageInnerText(WebDriver driver)90 {91 JavascriptExecutor js=(JavascriptExecutor)driver;92 String PageText=js.executeScript("return document.documentElement.innerText;").toString();93 return PageText;94 }95 96 public void scrollPageDown(WebDriver driver)97 {98 JavascriptExecutor js=(JavascriptExecutor)driver;99 js.executeScript("window.scrollTo(0,document.body.scrollHeight)");100 }101 102 public void scrollIntoView(WebDriver driver,WebElement element)103 {104 JavascriptExecutor js=(JavascriptExecutor)driver;105 js.executeScript("arguments[0].scrollIntoView(true);",element);106 }107 108 109 110 111 112 113 114 115 116 117 118 119 ...

Full Screen

Full Screen

Source:Frames.java Github

copy

Full Screen

...26 driver.switchTo().frame("iframeResult");27 //driver.findElement(By.xpath("/html/body/button")).click();28 29 // If driver instance was of Chrome Driver / any other browser , then I could have directly used 30 //executeScript() of JavaScriptExecutor Interface , as all browsers are implementing classes of that interface31 // ChromeDriver driver = new ChromeDriver();32 //driver.executeScript("myfunction()");33 // for WebDriver driver instance , we need to add JavaScriptExecutor castType to driver instance 34 ((JavascriptExecutor) driver).executeScript("myFunction()");35 ((JavascriptExecutor) driver).executeScript("arguments[0].style.border='3px solid red'", driver.findElement(By.id("mySubmit")));36 37 // return to the default content 38 driver.switchTo().defaultContent();39 40 List<WebElement> frames = driver.findElements(By.tagName("iframe"));41 System.out.println("the Total Number of Frames on the page are - " + frames.size());42 43 // display the IDs of the frames present 44 45 for(WebElement frame : frames)46 System.out.println(frame.getAttribute("id"));47 48 49 // If driver instance was of Chrome Driver / any other browser , then I could have directly used 50 //executeScript() of JavaScriptExecutor Interface , as all browsers are implementing classes of that interface51 // ChromeDriver driver = new ChromeDriver();52 //driver.executeScript("myfunction()");53 // for WebDriver driver instance , we need to add JavaScriptExecutor castType to driver instance 54 55 56 57 58 59 }60}...

Full Screen

Full Screen

Source:Example20_ScrollInSelenium.java Github

copy

Full Screen

...39 in the context of the currently selected frame or window40*/ 41 42// to scroll vertically down - to the bottom43 ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");4445 46 Thread.sleep(4000);4748 // to scroll vertically up - to the top49 ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, - document.body.scrollHeight)");5051 Thread.sleep(4000);52 // to scroll down by mentioned number of pixels53 ((JavascriptExecutor)driver).executeScript("window.scrollBy(0, 1500)");54 55 Thread.sleep(4000);56 //to scroll up by mentioned number of pixels57 ((JavascriptExecutor)driver).executeScript("window.scrollBy(0, -1500)");58 59 Thread.sleep(4000);60 //to scroll to the position of the object mentioned61 ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", driver.findElement(By.xpath("//a[text()='Create Your Package']")));62 63 64 65 } ...

Full Screen

Full Screen

Source:javascriptexecuter.java Github

copy

Full Screen

...21 driver.findElement(By.name("uid")).sendKeys("mngr377113");22 driver.findElement(By.name("password")).sendKeys("yzurEnY");23 24 // click action by javascriptexecutor interface25 js.executeScript("arguments[0].click();", button);26 driver.switchTo().alert().accept();27 28 // to generate alert window using javascriptexecutor, display message29 js.executeAsyncScript("alert('Welcome to selenium automation');");30 Thread.sleep(2000);31 driver.switchTo().alert().accept();32 33 // vertical scroll by 400 pixels34 //js.executeAsyncScript("window.scrollBy(0,400)");35 36 Thread.sleep(3000);37 //refresh the browser38 js.executeScript("history.go(0)");39 40 //get inner text of a webpage41 //String text = js.executeScript("return document.documentElement.innerText;").toString();42 // System.out.println("Inner text of a webpage: "+text);43 44 // get title of a webpage45 String text1 = js.executeScript("return document.title;").toString();46 System.out.println("Title of a web page: "+text1);47 System.out.println("complete");48 49 }50}...

Full Screen

Full Screen

Source:Javascript_Runtime_Changes.java Github

copy

Full Screen

...27 WebElement Email_editbox=driver.findElement(By.id("email"));28 29 30 //Disable Element31 js.executeScript("arguments[0].disabled=true;",Email_editbox);32 Thread.sleep(5000);33 34 //Enabled element35 js.executeScript("arguments[0].disabled=false;",Email_editbox);36 Thread.sleep(5000);37 38 39 //Hide element from webpage40 js.executeScript("arguments[0].style.visibility='hidden';",Email_editbox);41 Thread.sleep(5000);42 43 //Visibility of element44 js.executeScript("arguments[0].style.visibility='visible';",Email_editbox);45 Thread.sleep(5000);46 47 //Convert Single Selection dropdown to multiple selection dropdown48 js.executeScript("document.getElementById('month')"49 + ".setAttribute('multiple','multiple')");50 51 }5253} ...

Full Screen

Full Screen

Source:A27_JavascriptExecutorTypeTest.java Github

copy

Full Screen

...18 19 //interface== WebDriver, WebElement, JavascriptExecutor, TakesScreenshot20 21 //((TakesScreenshot)wd).getScreenshotAs(OutputType.FILE);22 //((JavascriptExecutor)wd).executeScript("", "")23 24 JavascriptExecutor js = (JavascriptExecutor)wd;25 26 //js.executeScript("javascript", "e1","e2","e3");27 // "js",0,1,228 29 js.executeScript("arguments[0].value='chennai'", searchBox);30 31 }32}...

Full Screen

Full Screen

executeScript

Using AI Code Generation

copy

Full Screen

1JavascriptExecutor js = (JavascriptExecutor) driver;2js.executeScript("window.scrollBy(0,1000)");3JavascriptExecutor js = (JavascriptExecutor) driver;4js.executeAsyncScript("window.scrollBy(0,1000)");5JavascriptExecutor js = (JavascriptExecutor) driver;6js.executeScript("window.scrollTo(0, document.body.scrollHeight)");7JavascriptExecutor js = (JavascriptExecutor) driver;8js.executeAsyncScript("window.scrollTo(0, document.body.scrollHeight)");9JavascriptExecutor js = (JavascriptExecutor) driver;10js.executeScript("window.scrollBy(0,document.body.scrollHeight)");11JavascriptExecutor js = (JavascriptExecutor) driver;12js.executeAsyncScript("window.scrollBy(0,document.body.scrollHeight)");13JavascriptExecutor js = (JavascriptExecutor) driver;14js.executeScript("window.scrollBy(0,document.body.scrollHeight)");15JavascriptExecutor js = (JavascriptExecutor) driver;16js.executeAsyncScript("window.scrollBy(0,document.body.scrollHeight)");17JavascriptExecutor js = (JavascriptExecutor) driver;18js.executeScript("window.scrollTo(0,document.body.scrollHeight)");19JavascriptExecutor js = (JavascriptExecutor) driver;20js.executeAsyncScript("window.scrollTo(0,document.body.scrollHeight)");21JavascriptExecutor js = (JavascriptExecutor) driver;22js.executeScript("window.scrollBy(0,document.body.scrollHeight)");23JavascriptExecutor js = (JavascriptExecutor) driver;24js.executeAsyncScript("window.scrollBy(0,document.body.scrollHeight)");25JavascriptExecutor js = (JavascriptExecutor) driver;

Full Screen

Full Screen

executeScript

Using AI Code Generation

copy

Full Screen

1JavascriptExecutor js = (JavascriptExecutor) driver;2js.executeScript("return document.readyState").equals("complete");3JavascriptExecutor js = (JavascriptExecutor) driver;4js.executeAsyncScript("return document.readyState").equals("complete");5JavascriptExecutor js = (JavascriptExecutor) driver;6WebElement element = driver.findElement(By.id("elementid"));7js.executeScript("arguments[0].scrollIntoView(true);", element);8JavascriptExecutor js = (JavascriptExecutor) driver;9WebElement element = driver.findElement(By.id("elementid"));10js.executeAsyncScript("arguments[0].scrollIntoView(true);", element);11JavascriptExecutor js = (JavascriptExecutor) driver;12js.executeScript("window.scrollBy(0,250)", "");13JavascriptExecutor js = (JavascriptExecutor) driver;14js.executeAsyncScript("window.scrollBy(0,250)", "");15JavascriptExecutor js = (JavascriptExecutor) driver;16js.executeScript("window.scrollBy(0,document.body.scrollHeight)");17JavascriptExecutor js = (JavascriptExecutor) driver;18js.executeAsyncScript("window.scrollBy(0,document.body.scrollHeight)");19JavascriptExecutor js = (JavascriptExecutor) driver;20js.executeScript("window.scrollTo(0, document.body.scrollHeight)");21JavascriptExecutor js = (JavascriptExecutor) driver;22js.executeAsyncScript("window.scrollTo(0, document.body.scrollHeight)");23JavascriptExecutor js = (JavascriptExecutor) driver;24js.executeScript("window.scrollTo(0, -document.body.scrollHeight)");25JavascriptExecutor js = (JavascriptExecutor) driver;26js.executeAsyncScript("window.scrollTo(0, -document.body.scrollHeight)");

Full Screen

Full Screen

executeScript

Using AI Code Generation

copy

Full Screen

1JavascriptExecutor js = (JavascriptExecutor)driver;2js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", element);3js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", element);4js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", element);5js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", element);6js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", element);7js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", element);8js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", element);9js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", element);

Full Screen

Full Screen

executeScript

Using AI Code Generation

copy

Full Screen

1JavascriptExecutor js = (JavascriptExecutor) driver;2js.executeScript("window.scrollBy(0, 1500)");3js.executeScript("arguments[0].scrollIntoView();", element);4js.executeScript("window.scrollTo(0, document.body.scrollHeight)");5js.executeScript("window.scrollTo(0, 800)");6js.executeScript("window.scrollTo(0, document.body.scrollHeight)");7js.executeScript("window.scrollTo(0, 800)");8js.executeScript("window.scrollTo(0, document.body.scrollHeight)");9js.executeScript("window.scrollTo(0, 800)");10js.executeScript("window.scrollTo(0, document.body.scrollHeight)");11js.executeScript("window.scrollTo(0, 800)");12js.executeScript("window.scrollTo(0, document.body.scrollHeight)");13js.executeScript("window.scrollTo(0, 800)");14js.executeScript("window.scrollTo(0, document.body.scrollHeight)");15js.executeScript("window.scrollTo(0, 800)");16js.executeScript("window.scrollTo(0, document.body.scrollHeight)");17js.executeScript("window.scrollTo(0, 800)");18js.executeScript("window.scrollTo(0, document.body.scrollHeight)");19js.executeScript("window.scrollTo(0, 800)");20js.executeScript("window.scrollTo(0, document.body.scrollHeight)");

Full Screen

Full Screen

Selenium 4 Tutorial:

LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.

Chapters:

  1. Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.

  2. What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.

  3. Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.

  4. Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.

  5. How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.

  6. Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.

  7. Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.

Selenium 101 certifications:

LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in Interface-JavascriptExecutor

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful