Selenium-webdriver (Python) (vi) Manipulating test objects

Source: Internet
Author: User
Tags sleep

The previous mentioned a lot of knowledge are positioning elements, positioning is only the first step, after the positioning of the original element to operate.

Mouse click or keyboard input, depending on our positioning is the button also input box.

In general, there are several ways to compare commonly used objects in Webdriver

· Click Object

· Send_keys to simulate key input on an object

· Clear clears the contents of the object, if possible

In the first example of the opening of this series we used to click and Send_skys, do not turn back to find, I paste the code:

# coding = Utf-8 from
Selenium import webdriver
    
browser = webdriver. Firefox ()
    
browser.get ("http://www.baidu.com")
browser.find_element_by_id ("kw"). Clear ()
browser.find_element_by_id ("kw"). Send_keys ("Selenium")
browser.find_element_by_id ("su"). Click ()
Browser.quit ()

Send_keys ("XX") is used to enter content in an input box.

Click () to tap a button.

Clear () for clearing the contents of the input box, such as Baidu input box by default, "Please enter the keyword" information, such as our landing box generally default will have "account" "password" such as the default information. Clear can help us clean up this information.

Webelement Other common methods:

· Text gets the literal of the element

· Submit Submission Form

· Get_attribute Get Property value

Text

Used to get textual information for an element

Below the Baidu home page at the bottom of the statement print output

#coding =utf-8 from
Selenium import webdriver
    
import  time
    
driver = Webdriver. Firefox ()
driver.get ("http://www.baidu.com")
Time.sleep (2)
    
#id = text information for the CP element
Data=driver.find_ ELEMENT_BY_ID ("CP"). Text
print data   #打印信息
    
time.sleep (3)
driver.quit ()

Output:

>>>
2013 Baidu to use the former must read Beijing ICP Certificate No. No. 030173

Submit

Submitting a form

We put "Baidu Click" Operation from click to submit:

#coding =utf-8 from
Selenium import webdriver
    
import time
    
driver = Webdriver. Firefox ()
driver.get ("http://www.baidu.com")
    
driver.find_element_by_id ("kw"). Send_keys ("Selenium")
Time.sleep (2)
#通过submit () to operate
the driver.find_element_by_id ("Su"). Submit ()  
    
Time.sleep (3)
Driver.quit ()

Here with submit and click the effect of the same, I have not thought can only use submit can not use Click Scene. What is the difference between them, know the students please leave a message to tell me.

Related Article

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.