Python + Selenium notes (7): WebDriver and WebElement,

Source: Internet
Author: User

Python + Selenium notes (7): WebDriver and WebElement,

 

(1)WebDriver

WebDriver provides many functions and settings for interacting with browsers. Through WebDriver functions and some methods, it can interact with browser windows, warnings, frameworks, and pop-up windows, it also provides automated browser navigation, setting cookies, screenshots, and other features to facilitate our testing.

(2)WebDriverFunctions and Methods

Function/attribute

Simple Description

Current_url

Get the URL of the current page (driver. current_url)
In this way, driver refers to the browser driver instance.
window_handle
Get the handle of the current window
name
Obtain the underlying browser name of the instance.
orientation
Obtains the location of the current device.
page_source
Obtain the source code of the current page
title
Obtain the title of the current page
window_handles
Obtains the handles of all windows in the current session.

 

Method

Simple Description

close()
Close current browser window
back()
Step back
forward()
Step forward
get(url)
Access the URL and load the webpage to the current browser session
maximize_window()
Maximize browser window
quit()
Exit the current driver instance and close all related windows.
refresh()
Refresh current page
implicitly_wait()
Wait time, in seconds
set_page_load_timeout()
Sets the time-out wait time for a page to be fully loaded, in seconds.
set_script_timeout()
Set the timeout time for Script Execution

 

(3)WebElementFunctions and Methods

Through WebElement, you can interact with website page elements, including text boxes, text fields, buttons, single-choice, multi-choice boxes, tables, rows, columns, and div.

Function/attribute

Simple Description

size
Obtains the element size (for example, element. size)
In this way, element refers to an element to be located.
tag_name
Get Tag Name
text
Obtains the text value of an element.

 

Method

Simple Description

clear()
Clear text box or text field content
click()
Click Element
get_attribute(name)
Gets the attribute value of an element. name: the name of the attribute to be obtained.
is_displayed()
Check whether the element is visible to the user
is_enabled()
Check whether the element is available
is_selected()
Check whether the element is selected. It is mainly used for single-choice and check boxes.
send_keys(value)
Input text. value is the value to be input.
submit()
Submit the form. If an element is used, the form to which the element belongs is submitted.
value_of_css_property(property_name)
Obtains the value of the CSS attribute. property_name is the name of the CSS attribute.

 

(4)Operation form, text box, check box, single-choice button

Automated interaction with various HTML controls through WebElement, such as inputting text in a text box, clicking a button, selecting a single button or check box, and obtaining the text and attribute values of elements.

For example, the following figure shows how to automate the registration function of the blog Garden:

(Here is just an example. It is useless to copy directly. The following code only locates some fields (mailbox, login name, and Registration button) and does not process the verification code, how to deal with the verification code)

1 def test_register_new_user (self): 2 3 # locate and click Register 4 5 login_area = self. driver. find_element_by_css_selector ('# login_area') 6 7 register = login_area.find_element_by_link_text ('registration') 8 9 register. click () 10 11 # Check whether the title of the opened webpage is 'user registration-blog service' 12 13 self. assertTrue ('user registration-blog service' = self. driver. title) 14 15 # locate fields on the registration page and register the button 16 17 user_email = self. driver. find_element_by_id ('email ') 18 19 user_login_name = self. driver. find_element_by_id ('loginname') 20 21 register_btn = self. driver. find_element_by_id ('submitbtn ') 22 23 # Check whether the maximum and minimum input characters allowed by the field are as expected 24 25 self. assertEqual ('2', user_login_name.get_attribute ('data-val-length-min') 26 27 self. assertEqual ('30', user_login_name.get_attribute ('data-val-length-max ') 28 29 # Check whether each field and button are visible to the user and available for 30 31 self. assertTrue (user_email.is_displayed () and user_email.is_enabled () 32 33 # enter user information 34 35 user_email.send_keys ('test @ 163.com ') 36 37 values ('test ') 38 39 # click the Registration button 40 41 register_btn.click () 42 43 # Check whether the registration successful prompt 44 45 self.assertTrue(self.driver.find_element_by_css_selector('p.txt-title. success-color '). text = 'registered successfully ')

 

 

For example, you can use the following method to check whether the check box on the logon page of the blog garden is selected.

1 def test_login (self): 2 3... The code used to open the logon page is omitted (This section is not commented #) 4 5 automatic_login = self. driver. find_element_by_id ('Remember _ me ') 6 7 # Check the check box on the logon page to check whether the logon page is deselected 8 9 self by default. assertFalse (automatic_login.is_selected () 10 11 # click the check box 12 13 automatic_login.click ()

 

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.