Python+selenium Personal Learning Note 10-invoking JavaScript and screenshots

Source: Internet
Author: User

Call JavaScript and

First, call JavaScript

1. Adjust browser scroll bar position

# left margin, top margin

2. Execute JavaScript code with Execute_script ()

js="Window.scrollto (0,500); " driver.execute_script (JS)

Example:

 fromSeleniumImportWebdriver fromTimeImportSleepdriver=Webdriver. Chrome () Driver.get ("http://www.baidu.com")Driver.maximize_window () driver.find_element_by_id ("kw"). Send_keys ("Chen") driver.find_element_by_id ("su"). Click () Sleep (2)#to adjust the position of the browser scroll bar via JavaScriptjs="Window.scrollto (0,500);"driver.execute_script (JS) sleep (3) Driver.quit ()

Ps:

1. Scroll bar back to top:
Js= "var Q=document.getelementbyid (' id '). scrolltop=0"
Driver.execute_script (JS)
2. Scroll bar to the bottom
Js= "var q=document.documentelement.scrolltop=10000"
Driver.execute_script (JS)

Two

1.

Driver.get_screenshot_as_file ()

Example:

 fromSeleniumImportWebdriver fromTimeImportSleepdriver=Webdriver. Chrome () Driver.get ("http://www.baidu.com")Driver.maximize_window () driver.find_element_by_id ("kw"). Send_keys ("Chen") driver.find_element_by_id ("su"). Click () Sleep (2)#intercepts the current window and savesDriver.get_screenshot_as_file ("e:\\chen.jpg") Driver.quit ()

Python+selenium Personal Learning Note 10-call JavaScript and

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.