Control Browser scroll bar
Sometimes we need to control the scroll bar on the page scroll bar, but the scrollbar is not the element on the page, this time
Need to use JS is to do the operation. The general use of the operation of the scroll bar will be two scenes:
- The legal provisions at the time of registration need to be read to determine whether the user is reading the standard: whether the scroll bar is pulled to the bottom.
- The page element you want to manipulate is not in the suction range and cannot be manipulated, you need to drag the scroll bar
In fact, the implementation of this function as long as a line of code, understand JS quickly can be resolved.
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, scrolltop=0, then to use the scroll bar at the bottom, you can
scrolltop=100000, so that you can make the scroll bar at the bottom.
1.1, Scene one
first to solve the field of the first problem, the legal provisions is an embedded window, through the Firebug tool can be positioned to the embedded
into the window can be positioned to the ID of the element, can be implemented by the following code.
Js= "var q=document.getelementbyid (' id '). scrolltop=10000"
Driver.execute_script (JS)
1.2, scene two
There are scroll bar of the page everywhere, this is easier to find examples, we operate Baidu search results page for example:
#coding =utf-8from Selenium Import webdriverimport time# visit Baidu 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)#将页面滚动条拖到底部js1 = "var q=document.documentelement.scrolltop=10000" Driver.execute_script (JS1) time.sleep (3) #将滚动条移动到页面的顶部js2 = "var q=document.documentelement.scrolltop=0" Driver.execute_script (JS2)time.sleep (3) driver.quit ()
Selenium+python (JS processing browser scroll bar)