Selenium-webdriver (Python) (11) How to control the scroll bar to the bottom

Source: Internet
Author: User
Tags sleep

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/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.