Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebDriver remove element from page

Tags:

I'm using Selenium WebDriver(ChromeDriver). I need to remove some elements from page after processing(from DOM model).

For example I have a following element:

WebElement starRatingElement = reviewElement.findElement(By.className("review-info-star")); 

How to remove starRatingElement from browser DOM model ?

How it can be achieved in Java with Selenium WebDriver ? Please show an example.

like image 796
alexanoid Avatar asked Oct 18 '15 15:10

alexanoid


People also ask

How to remove the element using selenium?

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.

How do you remove an element?

Removing an element using the removeChild() method First, select the target element that you want to remove using DOM methods such as querySelector() . Then, select the parent element of the target element and use the removeChild() method.


1 Answers

You'll have to execute a JavaScript code to make any DOM changes.

WebDriver driver = new ChromeDriver(); JavascriptExecutor js; if (driver instanceof JavascriptExecutor) {     js = (JavascriptExecutor) driver; } js.executeScript("return document.getElementsByClassName('review-info-star')[0].remove();"); 
like image 122
JRodDynamite Avatar answered Sep 25 '22 08:09

JRodDynamite