Last active
October 17, 2019 18:45
-
-
Save sauceaaron/5a590d17b2292ba94390be3f5a620410 to your computer and use it in GitHub Desktop.
Show how to add elements into a page with Selenium using JavascriptExecutor and executeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.openqa.selenium.JavascriptExecutor; | |
| import org.openqa.selenium.WebDriver; | |
| import org.openqa.selenium.chrome.ChromeDriver; | |
| public class AddElementToPage | |
| { | |
| public static void main(String[] args) | |
| { | |
| String code = | |
| "body = document.querySelector('body');" + | |
| "element = document.createElement('div');" + | |
| "text = document.createTextNode('Hello');" + | |
| "element.appendChild(text);" + | |
| "body.append(element);"; | |
| WebDriver driver = new ChromeDriver(); // or FirefoxDriver or RemoteWebDriver, etc. | |
| String url = "about:blank"; // or whatever page you want to go to | |
| driver.get(url); | |
| JavascriptExecutor js = (JavascriptExecutor) driver; | |
| js.executeScript(code); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment