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
19,831 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(text(), 'My Button')]") I hope this ...READ MORE

answered Nov 27, 2020 in Selenium by Gitika
• 65,770 points
6,461 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 28, 2018 in Selenium by nsv999
• 5,500 points
27,378 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
26,003 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 21, 2018 in Selenium by Nabarupa
1,935 views
0 votes
1 answer

How can I travel forward and back in a browser using selenium?

You can use the Navigate interface to ...READ MORE

answered Dec 31, 2018 in Selenium by Nabarupa
12,245 views
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,150 points
899 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,500 points
13,673 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

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

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,142 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,910 points
7,221 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 3, 2020 in Selenium by Sri
• 3,190 points
7,874 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP