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 in Selenium by Vaishnavi
• 1,160 points
849 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 by tonystark
• 500 points

selected 5 days ago 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 by Surya
• 970 points

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

Related Questions In Selenium

+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,090 points
6,523 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,420 points
5,717 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
78 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 in Selenium by Anvi
• 12,860 points
57 views
0 votes
1 answer
0 votes
1 answer

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,090 points
592 views
0 votes
1 answer
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,130 points
901 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
578 views