03
Sep

Functions to execute javascript during WebDriver script execution

Following generic code can be used to execute a JavaScript with Selenium WebdDriver

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class testJavaScriptErrors {

@Test
public void testForFramesCount() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get(“http://google.com”);

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(“document.getElementById(‘gbw’).click();”); ///<== Thread.sleep(20000); driver.quit(); } } few other options available inplace of document.getElementById are: #1. Set the text value via JavascriptExecutor: js.executeScript("document.getElementById('some id').value='someValue';"); #2. Click a button / link on web page via JavascriptExecutor: js.executeScript("document.getElementById('enter your element id').click();"); //or js.executeScript("arguments[0].click();", firstElement); #3. To uncheck / check a radio button / checkbox via JavascriptExecutor: js.executeScript("document.getElementById('enter element id').checked=false;"); #4. To display an alert via JavascriptExecutor: js.executeScript("alert('Welcome To SoftwareTestingMaterial');"); #5. To refresh the webPage via JavascriptExecutor: js.executeScript("history.go(0)"); #6. To get the innerText (or anyother attribute) of a WebElement via JavascriptExecutor: String sText = js.executeScript("return document.documentElement.innerText;").toString(); System.out.println(sText); #7. To get the Title of a webpage via JavascriptExecutor: String title = (String) js.executeScript("return document.title;").toString(); System.out.println(title); #8. To get the URL of webpage via JavascriptExecutor: JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.location = 'https://www.amazon.in';"); #9. To scroll down by 500 pixels via JavascriptExecutor: js.executeScript("window.scrollBy(0,500)"); OR WebElement element = driver.findElement(By.xpath(".//*[@id='mCSB_3_container']/p[3]")); je.executeScript("arguments[0].scrollIntoView(true);",element); #10. To navigate to another page via JavascriptExecutor: js.executeScript("window.location = 'https://www.amazon.in';"); #11. To find the currently focused element in the document String element = js.executeScript("return document.activeElement.tagName;").toString(); System.out.println(element); #12. Get the name of an attribute with tagName="Button" String x = js.executeScript("return document.getElementsByTagName("BUTTON")[0].attributes[0].name;").toString(); String x = js.executeScript("return document.getElementsByTagName("H1")[0].getAttribute("class");").toString(); #13. To navigate back and forward via JavascriptExecutor: js.executeScript("window.history.back();"); js.executeScript("window.history.forward();"); js.executeScript("window.history.go(-2);"); #14. Selecting elements by ID, Name etc. var x = js.executeScript("return document.getElementById('demo');"); var y = js.executeScript("return document.getElementsByClassName('example');"); var z = js.executeScript("return document.getElementsByName('fname');"); var a = js.executeScript("return document.getElementsByTagName('LI');"); #15. Get all links count on a webpage: Long x = (Long) js.executeScript("return document.links.length;"); System.out.println(x); #16. Get current page URL: String element = js.executeScript("return document.URL;").toString(); System.out.println(element);