selenium headless browser - Google

    What is headless browser?  

Headless browser, my understanding is that when executing the script, do not open the browser interface, but the code still executes

    Headless browser when to use?    

When the test need to open the browser, but open the browser will affect the normal operation of a computer, can not continue to work on the same computer,

In linux or when running a script, without having to open a browser, only the implementation of the code on the line

Then we use the headless browser, without having to open the browser, you only need to execute code

 

   A simple look at the headless browser configuration to Google headless browser as an example    

Import Time
 from Selenium Import the webdriver 


# Create a parameter object, to control opening in a non-chrome interface mode 
chrome_options = webdriver.ChromeOptions () 
chrome_options.add_argument ( ' --headless ' )   # # does not provide visual browser page 
chrome_options.add_argument ( ' --disable-GPU ' )   # disable GPU acceleration, Chrome GPU acceleration may cause a black screen and the CPU occupancy rate of over 80% 

# to create a browser object 
Driver = webdriver.Chrome (Options = chrome_options, executable_path = r ' / the Users / Downloads / chromedriver ' ) 
driver.implicitly_wait ( 10 ) # wait implicit

# To access the URL of 
driver.get ( ' https://www.baidu.com ' )   # unopened browser interface, you can execute code 
Print (driver.title)   # Baidu, you know 
the time.sleep (2 ) 

# Close the browser 
driver.quit ()

 

selenium add_argument parameter table

Reference link: https: //peter.sh/experiments/chromium-command-line-switches/

 

chrome_options.add_argument ( ' --user-Agent = "" ' )   # the User-Settings - Agent request header 
chrome_options.add_argument ( ' --window-size = 1280x1024 ' )   # Set the browser resolution (window size) 
chrome_options.add_argument ( ' --start-maximized ' )   # maximize operational (full-screen window), not set, taking the elements will complain 
chrome_options.add_argument ( ' --disable-infobars ' )   # disable the browser is being automated process control tips 
chrome_options. add_argument ( ' --incognito ' )   # stealth mode (incognito) 
chrome_options.add_argument ( 'scrollbars---hide ' )   # scroll bar is hidden, to deal with some special pages 
chrome_options.add_argument ( ' --disable-JavaScript ' )   # disable JavaScript 
chrome_options.add_argument ( ' --blink-Settings = imagesEnabled = false ' )   # no load pictures, lifting speed 
chrome_options.add_argument ( ' --headless ' )   # browser does not offer the visual page 

chrome_options.add_argument ( ' --ignore-errors-Certificate ' )   # disable extensions and achieve maximized window 
chrome_options.add_argument ( ' - GPU--disable ')  # 禁用GPU加速
chrome_options.add_argument('–disable-software-rasterizer')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--start-maximized')

 

 

 

   Example: Using a headless browser to log NetEase mailbox to send mail   

Import Time
 Import datetime   # get the current time 
from getpass Import getpass   # password entered is not displayed 
from the Selenium Import webdriver 


# Login NetEase mailbox 
DEF Longin (url, username, password): 
    driver.get (url = url) 
    driver.find_element_by_id ( ' accName ' ) .send_keys (username)   # enter a username 
    driver.find_element_by_id ( ' accpwd ' ) .send_keys (password)   # enter a username 
    driver.find_element_by_class_name (' Loginbtn ' ) .submit ()   # Click Login BTN 


# write messages and send 
DEF SEND_EMAIL (title, Addressee, msg):
     the try :
         # be logged 
        Longin (url, username, password) 
        
        # 1, click [write] 
        driver. find_element_by_class_name ( ' P-Edit ' ) .click () 
        
        '' ' 
        2, enter the recipient's title and 
           using a method of locating a target can not, so the use of various positioning cross 
        '' ' 
        driver.find_elements_by_class_name ( ' right- Inner ' ) [0] .find_element_by_name ( ' to ' ).
            find_element_by_tag_name(
            ' The INPUT ' ) .send_keys (Addressee)   # Enter the recipient 
        driver.find_elements_by_class_name ( ' right-Inner ' ) [0] .find_element_by_name ( ' to ' ) .find_element_by_tag_name (
             ' the INPUT ' ) .click ()   # click Write human input boxes, drop-down box to avoid blocking the subject input box 
        driver.find_element_by_class_name ( ' Subject ' ) .find_element_by_tag_name ( ' the iNPUT ' ) .send_keys (title)   # enter the subject 
        
        '' '
        3. Enter the message content (text only) 
           because the text box is in the ifrme, ifrme is embedded page elements, you must first enter into ifrme, then operation
        '' ' 
        Iframe = driver.find_element_by_id ( ' ueditor_0 ' )   # to obtain iframe, to locate it in the position 
        driver.switch_to.frame (iframe)   # switch to an iframe 
        driver.find_element_by_xpath ( ' / HTML / body ' ) .send_keys (msg)   # operation elements ifrme because there is no id no class, so use xpath 
        driver.switch_to.default_content ()   # after switching to the iframe, you can not operate on elements other than iframe, and then we need to quit iframe continue 
        
        # click [send] 
        the time.sleep (5 ) 
        driver.find_element_by_id ( ' main ' ) .find_element_by_class_name ( 'MNC-G ' ) .find_element_by_xpath (
             ' // * [@ class = "G-MNC"] / div [. 1] / div [. 1] / div [. 1] / div [. 1] ' ) .click ()
         Print ( ' sent completed !!! ' )
     a finally : 
        the time.sleep ( 3 ) 
        driver.close ()   # regardless of whether the message was successfully sent before finally closing the browser 


IF  __name__ == ' __main__ ' : 
    Chrome = r ' / the Users / Downloads / chromedriver '   # path to the drivers chrome 
    
    URL = ' https://qiye.163.com/login/?from=ym'   # E-mail address 
    username = the INPUT ( ' User name: ' ) .strip ()   # -mail account 
    password = getpass ( ' Password: ' ) .strip ()   # mail password 
    
    title = ' headless Send '   # message header 
    addressee = input ( ' the recipient's mailbox: ' ) .strip ()   # recipient mailbox 
    '' ' 
    the message content 
        datetime.datetime.now () the current time after the decimal point there are six 
        . datetime.datetime.now () strftime ('% Y-% m-% d% H: % M:% S ') the current time, only accurate to the second 
    ' '' 
    MSG = 'Best to wait 5s click send [], or may be designated as spam \ n-{} ' .format (datetime.datetime.now () the strftime (. ' % Y-M-% D%% H:% M :% S ' )) 

    ' '' 
    headless browser configuration 
    '' ' 
    chrome_options = webdriver.ChromeOptions ()   # build a parameter object, to control opening in a non-chrome interface mode 
    chrome_options.add_argument ( ' --headless ' )   # browsers do not offer the visual page 
    chrome_options.add_argument ( ' --disable-GPU ' )   # Jingyong 
    
    Driver = webdriver.Chrome (Options = chrome_options, executable_path = Chrome)   # create a browser object
    driver.implicitly_wait (10)   # Implicit wait 10s 
    
    SEND_EMAIL (title, Addressee, MSG)

 

 Headless very simple code unchanged, but added a headless configuration

The code with the original mail codes in different places as follows:

 

 

 

 

 

 

 

 

 

End!

Guess you like

Origin www.cnblogs.com/caoyinshan/p/12132295.html