Python + selenium + chrome primary automated operations, seleniumchrome

Source: Internet
Author: User

Python + selenium + chrome primary automated operations, seleniumchrome

Example 1.

# Coding = utf-8from selenium import webdriverimport OS, timechromedriver = "C: \ Users \ li. liu \ AppData \ Local \ Google \ Chrome \ chromedriver.exe "''' environ is the image object of the environment corresponding to a string. OS. environ. all keyos. environ displays key + content. Windows: · OS. environ ['homepath']: Home Directory of the current user. OS. environ ['temp ']: temporary directory path. OS. environ [PATHEXT ']: executable file. OS. environ ['systemroot']: main directory of the system. OS. environ ['logonserver']: Machine name. OS. environ ['propt']: sets the PROMPT. Linux: OS. environ ['user']: the current USER. OS. environ ['lc _ collate']: the alphabetic order of the Path Extension results. OS. environ ['shell']: Specifies the SHELL type. OS. environ ['lan']: language used. OS. environ ['ssh _ AUTH_SOCK ']: the path of SSH execution. ''' OS. environ ["webdriver. chrome. driver "] = chromedriverdriver = webdriver. chrome (chromedriver) driver. get ("http://baidu.com") time. sleep (3) print (u'window maximization ') driver. maximize_window () a = driver. titleprint atime. sleep (1) driver. find_element_by_id ('kw '). send_keys ('selenium ') driver. find_element_by_id ('su '). click () driver. quit ()

 

Example 2.

# Coding = utf-8from selenium import webdriverimport OS, time, unittestimport logimport loggingimport tracebacklogger = log. logger ('e:/1/web_log.log ', clevel = logging. DEBUG, Flevel = logging. INFO) def f (n): logger.info (n) print n class baidu (unittest. testCase): def setUp (self): self. chromedriver = 'C: \ Users \ li. liu \ AppData \ Local \ Google \ Chrome \ chromedriver.exe 'OS. environ ['webdriver. chrome. driver '] = self. chromedriver self. driver = webdriver. chrome (self. chromedriver) print U' driver definition completed 'def test_baidu_search (self): u "Baidu search" "driver = self. driver f (u'open Baidu ') driver. maximize_window () driver. get ('HTTP: // baidu.com ') time. sleep (2) print U' open another URL 'driver. get ('HTTP: // news.baidu.com ') print U' window maximization 'driver. maximize_window () time. sleep (1) print U' returns the previous webpage 'driver. back () time. sleep (3) driver. forward () time. sleep (2) driver. back () time. sleep (1) driver. close () driver. quit () def test_baidu_set (self): driver = self. driver. get ('HTTP: // baidu.com ') time. sleep (3) driver. find_element_by_id ('kw '). send_keys (u'test') driver. find_element_by_id ('su '). click () time. sleep (2) driver. find_element_by_id ('kw1 '). clear () driver. find_element_by_id ('kw '). send_keys (u'test case') time. sleep (3) print u'close the browser 'driver. close () print u'exit the browser service' driver. quit () if _ name __= = "_ main _": unittest. main ()

 

Browser a developer Mode

FireFox --> Firebug front-end tool, and for Chrome browser --> right-click 'review element' or the shortcut key F12, you can see some code written by the developer, which gets the test object (Test Control) some attributes.

Basic operations of browser B

Maximize and set the size of the browser window, and move forward and backward
Note the following points:
XXXX. get (URL) to URL
Time. sleep (ns) wait time
XXXX. back () returns to the previous Browser
XXXX. forward () move forward to the next Browser
XXXX. set_window_size (width, height) width and height can be set as needed.
XXXX. maximize () window Maximization
XXXX. title to get the browser title
XXXX. quit () browser exited
XXXX. close () close the browser

Import time-> ensure the stability of script operation
Browser = webdriver. Chrome ()-> call the Chrome browser
Use the print () statement to check whether the script runs correctly.

 

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.