How can I delete an element in Selenium using Python?

0 votes

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
• 720 points
68 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
+1 vote

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
• 820 points

edited Mar 27 by Omkar

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,030 points
4,028 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,400 points
2,665 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
36 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,030 points
77 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
449 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
57 views
0 votes
1 answer

How can I automate Google Chrome using Selenium RC?

Chrome is used to launch a special ...READ MORE

answered Mar 6 in Selenium by Surya
• 820 points
34 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.