Skip to content

Instantly share code, notes, and snippets.

@MartinRogalla
Last active February 6, 2016 06:04
Show Gist options
  • Select an option

  • Save MartinRogalla/2b0de7bf7c3fe1929ee9 to your computer and use it in GitHub Desktop.

Select an option

Save MartinRogalla/2b0de7bf7c3fe1929ee9 to your computer and use it in GitHub Desktop.
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
@Slf4j
public class MinimalExample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://stackoverflow.com/questions/11430773/how-to-use-javascript-with-selenium-webdriver-using-java");
try {
Thread.sleep(5000); // Let the user actually see something!
}
catch (InterruptedException e) {
e.printStackTrace();
}
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
jsExecutor.executeAsyncScript("return getEventListeners(document.getElementById('wmd-input'));");
try {
Thread.sleep(5000); // Let the user actually see something!
}
catch (InterruptedException e) {
e.printStackTrace();
}
driver.quit();
}
}
[0.338][INFO]: COMMAND InitSession {
"desiredCapabilities": {
"browserName": "chrome",
"chromeOptions": {
"args": [ ],
"extensions": [ ]
},
"platform": "ANY",
"version": ""
}
}
[0.343][INFO]: Populating Preferences file: {
"alternate_error_pages": {
"enabled": false
},
"autofill": {
"enabled": false
},
"browser": {
"check_default_browser": false
},
"distribution": {
"import_bookmarks": false,
"import_history": false,
"import_search_engine": false,
"make_chrome_default_for_user": false,
"show_welcome_page": false,
"skip_first_run_ui": true
},
"dns_prefetching": {
"enabled": false
},
"profile": {
"content_settings": {
"pattern_pairs": {
"https://*,*": {
"media-stream": {
"audio": "Default",
"video": "Default"
}
}
}
},
"default_content_setting_values": {
"geolocation": 1
},
"default_content_settings": {
"geolocation": 1,
"mouselock": 1,
"notifications": 1,
"popups": 1,
"ppapi-broker": 1
},
"password_manager_enabled": false
},
"safebrowsing": {
"enabled": false
},
"search": {
"suggest_enabled": false
},
"translate": {
"enabled": false
}
}
[0.343][INFO]: Populating Local State file: {
"background_mode": {
"enabled": false
},
"ssl": {
"rev_checking": {
"enabled": false
}
}
}
[0.345][INFO]: Launching chrome: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome --disable-background-networking --disable-client-side-phishing-detection --disable-component-update --disable-default-apps --disable-hang-monitor --disable-popup-blocking --disable-prompt-on-repost --disable-sync --disable-web-resources --enable-logging --ignore-certificate-errors --load-extension=/var/folders/zs/5skcq3ps3dl31s81632fkx800000gn/T/.org.chromium.Chromium.gDb0sZ/internal --log-level=0 --metrics-recording-only --no-first-run --password-store=basic --remote-debugging-port=12252 --safebrowsing-disable-auto-update --safebrowsing-disable-download-protection --test-type=webdriver --use-mock-keychain --user-data-dir=/var/folders/zs/5skcq3ps3dl31s81632fkx800000gn/T/.org.chromium.Chromium.yE4T7e data:,
[1.283][INFO]: RESPONSE InitSession {
"acceptSslCerts": true,
"applicationCacheEnabled": false,
"browserConnectionEnabled": false,
"browserName": "chrome",
"chrome": {
"chromedriverVersion": "2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4)",
"userDataDir": "/var/folders/zs/5skcq3ps3dl31s81632fkx800000gn/T/.org.chromium.Chromium.yE4T7e"
},
"cssSelectorsEnabled": true,
"databaseEnabled": false,
"handlesAlerts": true,
"hasTouchScreen": false,
"javascriptEnabled": true,
"locationContextEnabled": true,
"mobileEmulationEnabled": false,
"nativeEvents": true,
"platform": "Mac OS X",
"rotatable": false,
"takesHeapSnapshot": true,
"takesScreenshot": true,
"version": "49.0.2623.39",
"webStorageEnabled": true
}
[1.320][INFO]: COMMAND Navigate {
"url": "http://stackoverflow.com/questions/11430773/how-to-use-javascript-with-selenium-webdriver-using-java"
}
[1.320][INFO]: Waiting for pending navigations...
[1.322][INFO]: Done waiting for pending navigations
[1.839][INFO]: Waiting for pending navigations...
[2.654][INFO]: Done waiting for pending navigations
[2.654][INFO]: RESPONSE Navigate
[7.660][INFO]: COMMAND ExecuteAsyncScript {
"args": [ ],
"script": "return getEventListeners(document.getElementById('wmd-input'));"
}
[7.660][INFO]: Waiting for pending navigations...
[7.661][INFO]: Done waiting for pending navigations
[7.669][INFO]: Waiting for pending navigations...
[7.670][INFO]: Done waiting for pending navigations
[7.670][INFO]: RESPONSE ExecuteAsyncScript javascript error: getEventListeners is not defined
JavaScript stack:
ReferenceError: getEventListeners is not defined
at eval (eval at executeAsyncScript (unknown source), <anonymous>:2:27)
at eval (eval at executeAsyncScript (unknown source), <anonymous>:2:86)
at executeAsyncScript (<anonymous>:329:26)
at <anonymous>:345:29
at callFunction (<anonymous>:237:33)
at <anonymous>:247:23
at <anonymous>:248:3
at Object.InjectedScript._evaluateOn (<anonymous>:878:140)
at Object.InjectedScript._evaluateAndWrap (<anonymous>:811:34)
at Object.InjectedScript.evaluate (<anonymous>:667:21)
(Session info: chrome=49.0.2623.39)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment