After locating the elements, the elements to operate. Such as a button, a click operation; edit box, input operation. Here are some common elements in operation:
- clear () Clear text
- send_keys () Analog input keyboard
- click (), click
- submit () submit the form
- text () returns the element text
- size () Returns element size
- get_attibute () returns the property value
- is_displayed () Returns whether the element is visible to the user
- title returns the title of the current page
- url returns the url of the current page
The following were introduced:
1. clear()、send_keys()、click()
from Selenium Import the webdriver Import Time Driver = webdriver.Chrome () driver.get ( ' http://www.baidu.com ' ) Element = driver.find_element_by_id ( ' kW ' ) element.send_keys ( ' Zhangyang ' ) # input Zhangyang the time.sleep (2 ) element.clear () # clear the contents of the input box the time.sleep (2 ) element.send_keys ( ' zhangsanfeng ' ) # input zhangsanfeng driver.find_element_by_id ( ' su ' ) .click () # click the button Baidu, the time.sleep ( 5 ) driver.close ()
send_keys () can not only simulate keyboard input, the keyboard keys may also transmit analog file uploads
element = driver.find_element_by_id('kw') element.send_keys('zhangyang') element.send_keys(Keys.ENTER) # 模拟回车键
2. submit()
Submit the form. For example, after typing Baidu search box, the transport operation, can be adopted () method for simulating submit
from Selenium Import the webdriver Import Time Driver = webdriver.Chrome () driver.get ( ' http://www.baidu.com ' ) Element = driver.find_element_by_id ( ' kW ' ) element.send_keys ( ' Zhangyang ' ) element.submit () # so do not click on Baidu button, and direct submissions entered the time.sleep ( 5 ) driver.close ()
Sometimes submit () can () method is used interchangeably with the click, submit () The same can also submit a button, but submit () applications far and click () wide
= Driver webdriver.Chrome () driver.get ( ' http://www.baidu.com ' ) driver.find_element_by_id ( ' kW ' ) .send_keys ( ' Zhangyang ' ) driver.find_element_by_id ( ' SU ' ) .submit () # submit Baidu button
3. text
For acquiring the text between two angle brackets
For example: Get the Baidu home page news
= driver.find_element_by_name text1 ( ' tj_trnews ' ) .text # get news text Print (text1) # Print: News
4. size()、get_attribute()、is_displayed()
Home to Baidu, for example, as follows:
driver.find_element_by_id = size1 ( ' SU ' ) .size Print (size1) # Print: { 'height': 36, 'width': 100} Result = driver.find_element_by_id ( ' SU ' ) .is_displayed () Print (Result) # print: True text1 = driver.find_element_by_link_text ( ' News ' ) .get_attribute ( ' href ' ) Print (text1) # print: http://news.baidu.com/, if you want to get text on the button, but also through name or class name property acquisition
5. title、url
Each print Baidu home page title and url, as follows:
= TITLE1 driver.title Print (TITLE1) # print: Baidu, you know URL1 = driver.current_url Print (URL1) # print: https: //www.baidu.com/