day2 python from entry to abandon ---- selenium basic use

from the Selenium Import webdriver   # Web drive 
from selenium.webdriver.common.by Import By   # to find in what way, By.ID, By.CSS_SELECTOR 
from selenium.webdriver.common.keys Import Keys   # keyboard key operation 
from selenium.webdriver.support Import expected_conditions AS EC   # , and together with the following WebDriverWait 
from selenium.webdriver.support.wait Import WebDriverWait   # wait for a page to load certain elements 
Import Time 

Import Time 

# way: by driving open browser 
#driver = webdriver.Chrome (r 'drive absolute path /webdriver.exe') 

# way: the driving into the python interpreter webdriver.exe installation directory / Scripts folder 
# python interpreter installation directory / Scripts configure the environment variables 
# Python interpreter installation directory configuration environment variable 
Driver = webdriver.Chrome () 

the try : 

    driver.get ( ' https://www.jd.com/ ' ) 

    # obtain explicit wait 10 seconds objects 
    # may wait loading a label 10 seconds 
    the wait = WebDriverWait (Driver, 10 ) 

    # find an element of id Key 
    The input_tag = wait.until (EC.presence_of_element_located ( 
        (By.ID, ' Key ' ) 
    )) 

    the time.sleep ( . 5) 
            
    # Enter a product name in the input box 
    input_tag.send_keys ( ' doll ' ) 

    # press the keyboard Enter key 
    input_tag.send_keys (Keys.ENTER) 


    the time.sleep ( 20 ) 

a finally :
     # close the browser release operating system resources 
    driver. Close () 



'' '' '' 
from Selenium Import the webdriver   # Web drive 
from selenium.webdriver.common.keys Import keys   # keyboard operation 
Import Time 

Import Time 

driver = webdriver.Chrome () 

the try : 

    #Implicit wait: call the prior GET 
    # Wait 10 seconds to load any element 
    driver.implicitly_wait (10 ) 

    driver.get ( ' https://www.baidu.com/ ' ) 

    # explicit wait: call need after GET 
    Time .sleep (. 5 ) 

    '' ' 
    =============== all methods =================== 
        Element is to find a tag 
        elements It is to find all the labels 
    ' '' 
    # auto-login Baidu Start 
    # 1, # find_element_by_link_text go through linked text 
    LOGIN_LINK = driver.find_element_by_link_text ( ' login ' ) 
    login_link.click ()   # click Sign 

    the time.sleep ( 1 ) 

    # 2、find_element_by_id # 通过id去找
    user_login = driver.find_element_by_id('TANGRAM__PSP_10__footerULoginBtn')
    user_login.click()

    time.sleep(1)

    # 3、find_element_by_class_name
    user = driver.find_element_by_class_name('pass-text-input-userName')
    user.send_keys('*****')

    # 4、find_element_by_name
    pwd = driver.find_element_by_name('password')
    pwd.send_keys('*****')

    submit Driver.find_element_by_id = ( ' TANGRAM__PSP_10__submit ' ) 
    submit.click () 
    # End 

    # . 5, find_element_by_partial_link_text 
    # local link text search 
    LOGIN_LINK = driver.find_element_by_partial_link_text ( ' registration ' ) 
    login_link.click () 

    # . 6, find_element_by_css_selector 
    # Find The attribute selector elements 
    # :. class 
    # #: ID 
    login2_link = driver.find_element_by_css_selector ( ' .tang-Pass-footerBarULogin ' ) 
    login2_link.click () 

    # . 7, find_element_by_tag_name
    = driver.find_elements_by_tag_name div ( ' div ' )
     Print (div) 


    the time.sleep ( 20 ) 

a finally :
     # close the browser release operating system resources 
    driver.close ()

 

Guess you like

Origin www.cnblogs.com/tankfaledeblog/p/11123338.html