Examples of Python and selenium and Chrome's primary automation operations

Source: Internet
Author: User
Examples of Python and selenium and Chrome's primary automation operations

#coding =utf-8from Selenium Import webdriverimport os,timechromedriver = "C:\Users\li.liu\AppData\Local\Google\Chrome \chromedriver.exe "'" Environ is an image object for the context of a string, and all Keyos.environ in the Os.environ.keys ()  home directory display key+ content. Windows: os.environ[' HomePath ': The current user home directory. os.environ[' temp ']: Temp directory path. Os.environ[pathext ']: executable file. os.environ[' SYSTEMROOT ': System home directory. os.environ[' LogonServer ': machine name. os.environ[' PROMPT ': Set prompt. linux:os.environ[' user ': currently using users. os.environ[' lc_collate ': the alphabetical order at which the results of the path extension are sorted. os.environ[' Shell ': uses the type of shell. os.environ[' LAN ': the language used. os.environ[' ssh_auth_sock ']:ssh execution path. "' os.environ[" webdriver.chrome.driver "] = Chromedriverdriver =  webdriver. Chrome (Chromedriver) driver.get ("http://baidu.com") time.sleep (3) Print (U ' window maximized ') 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 complete ' 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 maximized ' Driver.maximize_window () Time.sleep (1) Print U ' back to previous page ' 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        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 () Driv er.find_element_by_id (' kw '). Send_keys (U ' test Case ') time.sleep (3) Print U ' close browser ' driver.close () pri NT U ' exit Browser service ' Driver.quit () if __name__== "__main__": Unittest.main ()

A browser developer mode

Firefox-->firebug front-end tools, and for Chrome browser-right-click ' Review elements ' or ' F12 ', you can see some code developed for writing, which is useful for getting some properties of test objects (test controls).

b Browser Basic operation

Maximize browser window and size settings, as well as forward, rewind
Here are a few notes:
Xxxx.get (URL) go to URL address
Time.sleep (NS) Wait time
Xxxx.back () return to previous browser
Xxxx.forward () forward to the next browser
Xxxx.set_window_size (width,height) Width,height we can set ourselves as needed.
Xxxx.maximize () window maximization
Xxxx.title get the title of the browser
Xxxx.quit () Browser exit
Xxxx.close () Browser off

Import time-> to ensure the stability of script operation
Browser=webdriver. Chrome (), calling Chrome browser
Use the print () statement to see if the script is running 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.