Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebDriver: add new element

Tags:

webdriver

Is there a method to add/insert an element in the current DOM?

like image 856
KJW Avatar asked Nov 14 '10 06:11

KJW


1 Answers

Let me start by saying, this is a really bad idea. Think long and hard about why you want to do this. Then, if you still want to dynamically add elements, think about it some more. WebDriver is meant to mimic user interaction with your page, users don't typically add elements willy-nilly.

That said, if you're absolutely set on doing this I'd suggest using the JavascriptExecutor to add an element via JavaScript

WebDriver driver; // Assigned elsewhere
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('myDiv').appendChild(document.createTextNode(' New Element'))")

It's ugly for a reason.

like image 63
pnewhook Avatar answered Sep 29 '22 17:09

pnewhook



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!