Jeanluca Scaljeri
Jeanluca Scaljeri

Reputation: 29179

Webdriver: How to remove an elementy using js

I'm trying to remove an element using webdriver.

Unfortunately a WebDriver element doesn't have a remove method.

I've tried to remove an element using its parent

 child.findElement(webdriver.By.xpath("./.."))
                .then((parent) => {
                    parent.remove(element);
                });

Any suggestion how I can remove child ? Also, is there an easier way to get the parent element ?

Upvotes: 2

Views: 1084

Answers (1)

RemcoW
RemcoW

Reputation: 4336

Unfortunately an element is not able to suicide. The parent will have to kill it.

You could change the Xpath to child.findelement(webdriver.By.xpath("..")) as you you're automatically calling the current element so there is no need for the ./.

Another way you could remove your element is by executing a javascript command manually.

driver.executeScript("arguments[0].parentNode.removeChild(arguments[0]);", child);

Upvotes: 2

Related Questions