简体   繁体   中英

How to delete DOM element from Selenium Java

I am trying to delete an element from a website using Selenium and Java, I have the xpath of the element

WebElement m = driver.findElement (By.xpath ("//*[contains(text(),'discord.gg/')]"));

Thats the element I have. I want to delete it. I tried

$("//*[contains(text(),'discord.gg/')]").remove();

But that doesnt work either.

Thanks

I used selenium only in python, and in there I used method to execute javascript in the driver context. Try this . Inside javascriptExecutor run this javascript (change accordingly):

var badTableEval = document.evaluate (
    "//body/center/center/table",
    document.documentElement,
    null,
    XPathResult.FIRST_ORDERED_NODE_TYPE,
    null
);

if (badTableEval  &&  badTableEval.singleNodeValue) {
    var badTable  = badTableEval.singleNodeValue;
    badTable.parentNode.removeChild (badTable);
}

Try this:

WebElement m = driver
    .findElement (By.xpath ("//*[contains(text(),'discord.gg/')]"));
JavascriptExecutor js = (JavascriptExecutor)driver; 
js.executeScript("arguments[0].remove();", m);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM