How can I delete an element in Selenium using Python?

+1 vote

I've been trying to delete an element and I terribly failed at it. The element can only be reached via its class name and I have tried doing this:

js = "var aa=document.getElementsByClassName('classname')[0];aa.parentNode.removeChild(aa)"
driver.execute_script(js)

I get an error that the parent node is undefined.

So what is the best way to delete an element using Selenium?

Mar 26, 2019 in Selenium by Vaishnavi
• 1,180 points
7,564 views

2 answers to this question.

+2 votes
Best answer

You can directly delete the node using the following script, without having to refer to parent:

js_string = "var element = document.getElementById(\"element_id\");element.remove();"

browser.execute_script(js_string)

If you have the reference to the element, you can get the id using selenium : element.get_attribute(id)

answered Sep 14, 2019 by tonystark
• 500 points

selected Sep 16, 2019 by Abha
@tonystark thanks for providinrg the solution. Even I have faced the same problem and tried using different solutions. But your answer worked well for me. Great approach!!
0 votes

Hi friend,
getElementByClassName is not a method on document. You'll want to use

getElementsByClassName('classname')[0]...
but only if you are sure it's the only one with that class.

And you can also do this:

element = driver.find_element_by_class_name('classname')
driver.execute_script("string"
var element = arguments[0];
element.parentNode.removeChild(element);
"", element)

Or you can use a solution that relies on JavaScript to find the element:

driver.execute_script(" "
var element = document.querySelector(".classname");
if (element)
    element.parentNode.removeChild(element);
" ")

This solution is much better if you use a remote server to run the tests (like BrowserStack and so on) but there's a non-negligible cost for communication between the Selenium client and the server.

This worked for me. Check it out!

answered Mar 26, 2019 by Surya
• 970 points

edited Mar 27, 2019 by Omkar
Her question is if the parent is not defined. This works if the parent is defined.

Related Questions In Selenium

0 votes
1 answer

How do I find an element that contains specific text in Selenium WebDriver (Python)?

Try the following: driver.find_elements_by_xpath("//*[contains(tex ...READ MORE

answered 6 days ago in Selenium by Gitika
• 52,440 points
29 views
+2 votes
2 answers

How can I press ENTER key with the execute_script in selenium python?

The below code containing Keys.ENTER might just ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,520 points
17,851 views
0 votes
1 answer

How do I get current URL in Selenium Webdriver using Python?

Use current_url element. Example: print browser.current_url READ MORE

answered Aug 8, 2018 in Selenium by Meci Matt
• 9,460 points
20,039 views
0 votes
1 answer

How can I click on male tag in facebook using selenium?

There is an approach for locating an ...READ MORE

answered Dec 20, 2018 in Selenium by Nabarupa
523 views
0 votes
1 answer
0 votes
1 answer

How can I write test scripts in Selenium with python?

Hey Khushi, writing test scripts in Selenium ...READ MORE

answered May 9, 2019 in Selenium by Anvi
• 14,110 points
200 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,520 points
4,056 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31 in Selenium by Sri
• 2,970 points
5,013 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,890 points
2,950 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2 in Selenium by Sri
• 2,970 points
3,381 views