Executing JavaScript statements
1. Hide Baidu Pictures
From selenium import Webdriver
Import time
Driver = Webdriver. PHANTOMJS ()
Driver.get ("https://www.baidu.com/")
# JavaScript script to search for the input box red
JS = "var Q=document.getelementbyid (\" kw\ "); q.style.border=\" 2px solid red\ ";"
# Call to search input box labeled Red JS Script
Driver.execute_script (JS)
#查看页面快照
Driver.save_screenshot ("Redbaidu.png")
#js隐藏元素, hide the captured picture element
IMG1 = Driver.find_element_by_xpath ("//*[@id = ' lg ']/img")
Driver.execute_script (' $ (arguments[0]). FadeOut () ', IMG1)
#重要 (If you do not wait may cause JS to not load and get the desired page)
Time.sleep (10)
Print (' =========== ')
# # Scroll down to the bottom of the page
Driver.execute_script ("$ ('. Scroll_top ')." Click (function () {$ (' html,body '). Animate ({scrolltop: ' 0px '}, 800);}); ")
#查看页面快照
Driver.save_screenshot ("Nullbaidu.png")
Driver.quit ()
2. Scroll to the bottom of the simulation scroll bar
From selenium import Webdriver
Import time
Driver = Webdriver. PHANTOMJS ()
Driver.get ("https://movie.douban.com/typerank?type_name= plot &type=11&interval_id=100:90&action=")
# Scroll down 10000 pixels
JS = "document.body.scrolltop=10000"
#js = "var q=document.documentelement.scrolltop=10000"
Time.sleep (3)
#查看页面快照
Driver.save_screenshot ("Douban.png")
# Execute JS Statement
Driver.execute_script (JS)
Time.sleep (10)
#查看页面快照
Driver.save_screenshot ("Newdouban.png")
Driver.quit ()
Crawler's Dynamic HTML processing (selenium and PHANTOMJS) executes JavaScript statements