- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I delete an element in Selenium using Python?
We can delete an element in Selenium webdriver using Python with the help of JavaScript Executor. Selenium is not capable of modifying the structure of DOM directly.
It has the feature of injecting JavaScript into the webpage and changing the DOM with the help execute_script method. The JavaScript command to be used is passed as a parameter to this method.
JavaScript command to delete an element is −
var l = document.getElementsByClassName("tp-logo")[0]; l.parentNode.removeChild(l);
The above script is to be passed as a parameter to the execute_script method.
Let us try to remove the highlighted logo from the below page −
Example
from selenium import webdriver #set chromodriver.exe path driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") #implicit wait driver.implicitly_wait(0.5) #maximize browser driver.maximize_window() #launch URL driver.get("https://www.tutorialspoint.com/about/about_careers.htm") #delete element with JavaScript Executor driver.execute_script(""" var l = document.getElementsByClassName("tp-logo")[0]; l.parentNode.removeChild(l); """)
Output
- Related Articles
- How can I scroll a web page using selenium webdriver in python?
- How can I parse a website using Selenium and Beautifulsoup in python?
- How do I send a DELETE keystroke to a text field using Selenium with Python?
- How do I find an element that contains specific text in Selenium WebDriver (Python)?
- How can I delete an item from an Object in MongoDB?
- How can I consistently remove the default text from an input element with Selenium?
- How can I clear text of a textbox using Python Selenium WebDriver?
- How can I get all element's immediate children with css selectors using selenium?
- How can I select an element by tag name using jQuery?
- How can I show and hide an HTML element using jQuery?
- How can I display an image using cv2 in Python?
- How can I perform mouse hover action in Selenium-Python?
- How can I manually set proxy settings in Python Selenium?
- How to find an element using the “XPath” in Selenium?
- How can I select an element by its class name using jQuery?

Advertisements