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)"

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,080 points

1 answer to this question.

+1 vote

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

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')
var element = arguments[0];
"", 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)
" ")

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
• 950 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,050 points
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
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
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,050 points
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
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
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
• 950 points