day02 selenium basis

A 
selenium request library
1. What is selenium?
Opening is an automated testing tool, it is the driving principle of the browser
to perform some definite good operation. The reptile is essentially analog browser,
so you can use it for reptiles
2. Why should I use selenium?
Advantages:
- js code execution
- no need to analyze complex communication processes
- do pop-browser drop down other operations
- **** obtain dynamic data
- **** crack login authentication
Cons:
- inefficient! So we generally use it for login authentication
3. Installation and Use
1. Install selenium request library:
PIP3 install selenium
2. You must install the browser
Google or Firefox
3. Download the browser driver
http://npm.taobao.org/mirrors /chromedriver/2.38/
Windows:
Download win32
the Selenium simple to use
from the Selenium Import webdriver   # used to drive the browser 
from selenium.webdriver Import ActionChains   # crack the code when using slide can drag pictures 
from selenium.webdriver.common.by Import By   # in what ways to find, By.ID, By.CSS_SELECTOR 
from selenium.webdriver.common.keys Import keys   # keyboard operation 
from selenium.webdriver.support Import expected_conditions EC AS   # and together with the following WebDriverWait 
from selenium.webdriver.support.wait Import WebDriverWait   #Wait for page load certain elements 
Import Time 

# way: by opening a browser driver 
# Driver = webdriver.Chrome (r'D: /chromedriver.exe ') 

# way: the driving into the python interpreter webdriver.exe installation directory / scripts folder 
# Python interpreter installation directory / scripts configure the environment variables 

Driver = webdriver.Chrome ()
 the try : 
    driver.get ( ' https://www.jd.com/ ' )
     # acquires the display objects wait 10 seconds 
    # a label can wait 10 seconds to load 
    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 input box 
    input_tag.send_keys ( ' doll ' )
     # press the keyboard Enter key 
    input_tag.send_keys (Keys.ENTER) 
    the time.sleep ( 20 is )
 the finally :
     # close the browser release operation system resources 
    driver.close ()

selenium selector

from Selenium Import the webdriver   # Web drive 
from selenium.webdriver.common.keys Import Keys   # keyboard operation 
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: you need to call after GET 
    the time.sleep (5)

'' '
=============== all methods ===================
Element is to find a label
elements is to find all the labels
' ''

# auto-login Baidu Start
# 1, # find_element_by_link_text go through text links
    = driver.find_element_by_link_text LOGIN_LINK ( ' login ' ) 
    login_link.click ()   # Click Login
time.sleep (1)



# 2, find_element_by_id # id go through
    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('')
    login_link.click()

6 #, find_element_by_css_selector
# find elements based on attribute selectors
    # .: class
    # #: id
    login2_link = driver.find_element_by_css_selector('.tang-pass-footerBarULogin')
    login2_link.click()

# 7、find_element_by_tag_name
    div = driver.find_elements_by_tag_name('div')
    print(div)


    time.sleep(20)


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

 





Guess you like

Origin www.cnblogs.com/cl007/p/11120093.html