Selenium+python (JS processing browser scroll bar)

Source: Internet
Author: User

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)

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.