Sometimes we need to control the scroll bar on the page scroll, but the scroll bar is not the element on the page, this time need to use JS is to operate. Typically, two scenes are used to manipulate the scroll bar:
The legal provisions for registration need to be read to determine whether the user is reading the standard: whether the scroll bar to the bottom.
The page element to be manipulated is not in the suction range and cannot be manipulated, you need to drag the scroll bar
In fact, to achieve this function as long as a line of code, but because do not understand JS, so it took a little effort to find this method.
Code to identify the position of the scroll bar
<body onload= "Document.body.scrolltop=0" >
<body onload= "document.body.scrolltop=100000" >
If the scroll bar is at the top of the scrolltop=0, then you want to use the scroll bar at the bottom can be scrolltop=100000, so you can make the scroll bar at the bottom.
Scene One
To solve the first problem of the field first, the legal provisions is an embedded window, through the Firebug tool can be positioned inside the embedded window can be located to the ID of the element, can be implemented through the following code.
Js= "var Q=document.getelementbyid (' id '). scrolltop=10000"
Driver.execute_script (JS)
Note: Since no suitable examples have been found, no validation, from the reference point of view this should be no problem, such as please feedback.
Scene Two
There is a scroll bar everywhere visible, this is easier to find examples, we operate Baidu search results page for example:
#coding =utf-8 from
Selenium import webdriver The
import time
#访问百度
driver=webdriver. Firefox ()
driver.get ("http://www.baidu.com")
#搜索
driver.find_element_by_id ("kw"). Send_keys (" Selenium ")
driver.find_element_by_id (" su "). Click ()
time.sleep (3)
#将页面滚动条拖到底部
js=" var q= document.documentelement.scrolltop=10000 "
driver.execute_script (JS)
time.sleep (3)
#将滚动条移动到页面的顶部
js= "var q=document.documentelement.scrolltop=0"
driver.execute_script (JS)
time.sleep (3)
Driver.quit ()
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/