Python Selenium webdriver handles browser scroll bar

Source: Internet
Author: User

Use the Up,down button in the lower right corner of the keyboard to handle the page scroll bar This method is very flexible to use very convenient!!!!

 fromSeleniumImportWebdriverImport Time fromSelenium.webdriver.common.keysImportKeys#visit BaiduDriver=Webdriver. Chrome () Driver.get ("http://www.baidu.com")#SearchDRIVER.FIND_ELEMENT_BY_ID ("kw"). Send_keys ("Selenium") driver.find_element_by_id ("su"). Click () time.sleep (3)#Drag the page scroll bar to the bottom by pressing the DOWN ARROW keyDriver.find_element_by_xpath ("//*[@id = ' page ']/a[10]"). Send_keys (Keys.down)Print 'pull the scroll bar to the end'Time.sleep (2) Driver.find_element_by_xpath ("//*[@id = ' s_tab ']/a[9]"). Send_keys (keys.up)Print 'pull the scroll bar to the upper end'Time.sleep (2) Driver.find_element_by_xpath ("//*[@id = ' Con-ar ']/div[3]/a"). Send_keys (Keys.down)Print 'pull the scroll bar to the middle'

You can also tune the JS script to handle the scroll bar, but this method I tried many times did not succeed, nor error, that is, did not respond, do not know why!

#Coding=utf-8 fromSeleniumImportWebdriverImport Time#visit BaiduDriver=Webdriver. Firefox () Driver.get ("http://www.baidu.com")  #SearchDRIVER.FIND_ELEMENT_BY_ID ("kw"). Send_keys ("Selenium") driver.find_element_by_id ("su"). Click () time.sleep (3)  #Drag the page scroll bar to the bottomjs="var q=document.documentelement.scrolltop=100000"driver.execute_script (JS) time.sleep (3)  #move the scroll bar to the top of the pagejs="var q=document.documentelement.scrolltop=0"driver.execute_script (JS) time.sleep (3)  #move the page scroll bar anywhere on the page to change the value after the equals signjs="var q=document.documentelement.scrolltop=50"Driver.execute_script (JS)
" "' to manipulate the scroll bars in the prompt Warning window in the page, first navigate to the inline window, and js= "var Q=document.getelementbyid (' id ') in the scroll bar operation. scrolltop=100000" Driver.execute_script (JS) time.sleep (3)" "

Python Selenium webdriver handles browser scroll bar

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.