Selenium-iwebdriver control scroll bar to bottom

Source: Internet
Author: User

Sometimes we need to control the scroll bar on the page scroll bar, but the scroll bar is not the element on the page, this time we need to use JS to do the work. 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, but because do not understand JS, so spend 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, scrolltop=0, then to use the scroll bar at the bottom can be scrolltop=100000, so that you can make the scroll bar at the bottom.

Scene One

First to solve the first problem of the field, the legal clause is an inline window, through the Firebug tool can be positioned to the inside of the embedded 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)

Note: Since the text of the legal terms must be read and accepted in order to proceed to the next step, you must first slide the scroll bar to the bottom, then the Accept button is available, and the Next button is available when you click Accept.

Scene Two

There are scroll bar pages everywhere, this is easier to find examples, we operate Baidu search results page for example:

#coding =utf-8 fromSelenium Import webdriverimport 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 ()


Transferred from: http://tech.ddvip.com/2013-10/1383059700204981.html

Here is my own code, C # + IE:

usingSe =Openqa.selenium;usingSIE =OpenQA.Selenium.IE;//SIE. Internetexploreroptions _ieoptions =NULL; SIE. Internetexplorerdriver _iedriver=NULL;//init Internet Driver        Private voidInitie () {_ieoptions=NewSIE.            Internetexploreroptions (); _ieoptions.introduceinstabilitybyignoringprotectedmodesettings=true; _ieoptions.unexpectedalertbehavior=SIE.            Internetexplorerunexpectedalertbehavior.default; _ieoptions.elementscrollbehavior=SIE.            Internetexplorerelementscrollbehavior.bottom; _iedriver=NewSIE.        Internetexplorerdriver (_ieoptions); }Private voidScroll Bar () {IList<Se.IWebElement> frames = _iedriver.findelements (Se.By.TagName ("Frame")); Se.iwebelement Framedisplay=NULL; Se.iwebelement Framecontrolpanel=NULL; foreach(varFrameinchframes) {                if(frame. GetAttribute ("name") =="Elementdisplayframe") {Framedisplay=frame; }                Else if(frame. GetAttribute ("name") =="Controlpanelframe") {Framecontrolpanel=frame; }            }            if(Framedisplay! =NULL) {_iedriver.switchto ().                Frame (Framedisplay); //Scroll to bottomSe.iwebelement Ndacontainer = _iedriver.findelement (Se.By.Id ("Ndacontainer")); stringid = ndacontainer.getattribute ("ID"); varJS ="var q = document.getElementById ('"+ ID +"'). scrolltop=10000"; _iedriver.executescript (JS,NULL);                Elementhp.wait (_iedriver); Se.iwebelement Yesbutton= _iedriver.findelement (Se.By.XPath ("//input[@name = ' I1 ')")); if(Yesbutton! =NULL&&yesbutton.enabled) {Yesbutton.click (); }            }}

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.