SHARE
TWEET

Selenium - remove header from HTML

furas Jul 14th, 2018 (edited) 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. # Header disappears only if I remove two elements.
  3. # Maybe there is better single selector which remove all.
  4. #
  5.  
  6. from selenium import webdriver
  7.  
  8. def fullpage_screenshot(driver, file):
  9.  
  10.     all_elements = driver.find_elements_by_css_selector('div.metabar')
  11.     for element in all_elements:
  12.         # older method
  13.         #driver.execute_script("item=arguments[0]; item.parentNode.removeChild(item)", element)
  14.         # new method
  15.         driver.execute_script("arguments[0].remove()", element)
  16.  
  17.     # OR using only JavaScript
  18.  
  19.     #driver.execute_script("document.querySelector('div.metabar').remove()")
  20.    
  21.     driver.execute_script("document.querySelectorAll('div.metabar').forEach(x => x.remove())")
  22.    
  23. # ------------
  24.  
  25. driver = webdriver.Chrome()
  26.  
  27. url = "https://towardsdatascience.com/object-detection-with-10-lines-of-code-d6cb4d86f606"
  28. driver.get(url)
  29. fullpage_screenshot(driver, "test1236.png")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top