Python+Selenium WebDriver API: Summary of common functions and variables of browsers and elements

Since webpage automation needs to operate browsers and browser page elements, here the author summarizes the commonly used functions and variables of browsers and page elements for readers to refer to when writing webpage automation tests.

copy code
from selenium import webdriver
driver=webdriver.Firefox()
driver.get(r'http://www.baidu.com/')
print 'driver attributes:'
print dir (driver)
elem=driver.find_element_by_id('kw')
print 'WebElement attributes:'
print dir (elem)
copy code
Among them: red bold is data (variable). The black bold is a method (function), and the function call needs to be parenthesized. 
What is an attribute? Attributes are data or function elements that belong to an object (the built-in function dir can view object attributes), and can be accessed through the attribute node identifier.
 
Browser properties:
driver attributes:
['NATIVE_EVENTS_ALLOWED', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_file_detector', '_is_remote', '_mobile', '_switch_to', '_unwrap_value', '_wrap_value', ' add_cookie ', 'application_cache', ' back ', 'binary', 'capabilities', ' close ', 'command_executor', 'create_web_element', ' current_url ', ' current_window_handle ', ' delete_all_cookies ', ' delete_cookie ', 'desired_capabilities', 'error_handler', 'execute', 'execute_async_script', ' execute_script ', 'file_detector',  'find_element', 'find_element_by_class_name', 'find_element_by_css_selector', 'find_element_by_id', 'find_element_by_link_text', 'find_element_by_name', 'find_element_by_partial_link_text', 'find_element_by_tag_name', 'find_element_by_xpath', 'find_elements', 'find_elements_by_class_name', 'find_elements_by_css_selector', 'find_elements_by_id', 'find_elements_by_link_text', 'find_elements_by_name', 'find_elements_by_partial_link_text', 'find_elements_by_tag_name', 'find_elements_by_xpath' , 'firefox_profile', ' forward ', ' get ', ' get_cookie ', ' get_cookies ', 'get_log', 'get_screenshot_as_base64', ' get_screenshot_as_file ', 'get_screenshot_as_png', ' get_window_position ', ' get_window_size ', ' implicitly_wait ', 'log_types', ' maximize_window ', 'mobile', 'name', 'orientation', ' page_source ', 'profile', ' quit ', ' refresh ', 'save_screenshot', 'session_id', 'set_page_load_timeout', 'set_script_timeout', 'set_window_position', ' set_window_size ', 'start_client', 'start_session', 'stop_client', ' switch_to ', 'switch_to_active_element', ' switch_to_alert ', 'switch_to_default_content', ' switch_to_frame ', ' switch_to_window ', ' title ', 'w3c', ' window_handles ']
 
Calling instructions:
driver.property value
 
Variable description:
1.driver.current_url: The URL used to get the current page
2.driver.title: used to get the title of the current page
3.driver.page_source: used to get the page html source code
4.driver.current_window_handle: used to get the current window handle
5.driver.window_handles: used to get all window handles
 
Function description:
1.driver.find_element*(): locate elements, see another blog post for details: Selenuim+Python element positioning summary and example description
2.driver.get(url): The browser loads the url.
实例:driver.get("http//:www.baidu.com")
3.driver.forward(): The browser moves forward (click the forward button).
4.driver.back(): The browser goes backward (click the back button).
5.driver.refresh(): Browser refresh (click the refresh button).
6.driver.close(): Close the current window, or the last opened window.
7.driver.quit(): Close all associated windows and safely close the session.
8.driver.maximize_window(): Maximize the browser window.
9.driver.set_window_size (width, height): Set the browser window size.
10.driver.get_window_size(): Get the length and width of the current window.
11.driver.get_window_position(): Get the current window coordinates.
12.driver.get_screenshot_as_file(filename): Intercept the current window.
实例:driver.get_screenshot_as_file('D:/selenium/image/baidu.jpg')
13.driver.implicitly_wait (seconds): Implicitly wait, wait for an element on the page to be loaded for a certain period of time.
If the element is located in advance, the execution continues. If it is not loaded within the time limit, a NoSuchElementException will be thrown.
Example: driver.implicitly_wait(10) #Wait for 10 seconds
14.driver.switch_to_frame (id or name attribute value): switch to a new form (the same window). If there is no id or attribute value, you can locate the iframe through xpath first, and then pass the value to switch_to_frame()
15.driver.switch_to.parent_content(): Jump out of the current level form. This method defaults to the switch_to.frame() method closest to it.
16.driver.switch_to.default_content(): Jump back to the outermost page.
17.driver.switch_to_window (window handle): switch to a new window.
18.driver.switch_to.window (window handle): switch to a new window.
19.driver.switch_to_alert(): Warning box processing. Handle alerts, confirms, and prompts generated by JavaScript.
20.driver.switch_to.alert(): Warning box processing.
21.driver.execute_script(js): call js.
22.driver.get_cookies(): Get all cookie information of the current session.
23.driver.get_cookie(cookie_name): Returns the cookie information whose key is "cookie_name" in the dictionary.
实例:driver.get_cookie("NET_SessionId")
24.driver.add_cookie(cookie_dict): Add cookies. "cookie_dict" refers to a dictionary object, which must have name and value values.
25.driver.delete_cookie(name,optionsString): delete cookie information.
26.driver.delete_all_cookies(): delete all cookie information.
 
Page element properties:
WebElement attributes:
['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_execute', '_id', '_parent', '_upload', '_w3c', ' clear ', ' click ', 'find_element', 'find_element_by_class_name', 'find_element_by_css_selector', 'find_element_by_id', 'find_element_by_link_text', 'find_element_by_name', 'find_element_by_partial_link_text', 'find_element_by_tag_name', 'find_element_by_xpath', 'find_elements', 'find_elements_by_class_name', 'find_elements_by_css_selector', 'find_elements_by_id', 'find_elements_by_link_text', 'find_elements_by_name', 'find_elements_by_partial_link_text', 'find_elements_by_tag_name', 'find_elements_by_xpath', ' get_attribute ', 'id', ' is_displayed ', 'is_enabled', ' is_selected ', 'location', 'location_once_scrolled_into_view', 'parent', 'rect', 'screenshot', 'screenshot_as_base64', 'screenshot_as_png', ' send_keys ', ' size ', ' submit ', ' tag_name ', ' text ', 'value_of_css_property']
 
Calling instructions:
driver.find_element*.property value
or
element=driver.find_element*
element.property value
 
Variable description:
1.element.size: Get the size of the element.
2.element.text: Get the text of the element.
3.element.tag_name: Get the tag name.
 
Function description:
1.element.clear(): Clear the text.
2.element.send_keys(value): Enter text or keyboard keys (the Keys module needs to be imported).
3.element.click(): Click the element.
4.element.get_attribute(name): Get the attribute value
5.element.is_displayed(): Returns whether the element result is visible (True or False)
6.element.is_selected(): Returns whether the element result is selected (True or False)
7.element.find_element*(): locate elements for secondary positioning. .
 
Reprinted in: https://www.cnblogs.com/yufeihlf/p/5764807.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325251731&siteId=291194637