Selenium's Python source interpretation-webdriver inheritance relationship

Source: Internet
Author: User
Tags deprecated tag name xpath

First, Webdriver inheritance relations

In selenium, whether it's the usual Firefox Driver or Chrome Driver and IE drive, they all inherit webdriver.py classes in Webdriver under Selenium\webdriver\remote, As follows

Chrome Webdriver

Selenium\webdriver\chrome under webdriver.py in Webdriver defined as follows

 from Import Webdriver as Remotewebdriver class Webdriver (remotewebdriver): """ Controls The Chromedriver and allows the browser. """

Firefox webdriver

Selenium\webdriver\firefox under webdriver.py in Webdriver defined as follows

 from Import Webdriver as Remotewebdriver class Webdriver (remotewebdriver):     Pass

IE Webdriver

Selenium\webdriver\ie under webdriver.py in Webdriver defined as follows

 from Import Webdriver as Remotewebdriver class Webdriver (remotewebdriver):     def __init__ (Self, executable_path='IEDriverServer.exe', capabilities=None,                 Port =default_port, Timeout=default_timeout, host=default_host,                 log_level=default_log_level, log _file=default_log_file):    ...

As above source: from Selenium.webdriver.remote.webdriver import Webdriver as Remotewebdriver are inherited to Remotewebdriver, and mainly rewrite __init__ Method

Other methods are primarily inherited to the parent class Remotewebdriver, so focus on the methods in the Remotewebdriver class

1. Find class

Scripting common ways to find page elements

deffind_element_by_id (Self, id_):#Finds an element by ID. "    Passdeffind_elements_by_id (Self, id_):#Finds multiple elements by ID.    PassdefFind_element_by_xpath (Self, XPath):#Finds an element by XPath.    PassdefFind_elements_by_xpath (Self, XPath):#Finds multiple elements by XPath.    PassdefFind_element_by_link_text (Self, link_text):#Finds an element by link text    PassdefFind_elements_by_link_text (self, text):#Finds elements by link text.    PassdefFind_element_by_partial_link_text (Self, link_text):#Finds elements by a partial match of their link text.    PassdefFind_elements_by_partial_link_text (Self, link_text):#Finds An element is a partial match of its link text.    Passdeffind_element_by_name (self, name):#Finds an element by name.    Passdeffind_elements_by_name (self, name):#Finds elements by name.    Passdeffind_element_by_tag_name (self, name):#Finds an element by tag name.    Passdeffind_elements_by_tag_name (self, name):#Finds elements by tag name.    Passdeffind_element_by_class_name (self, name): Finds a element byclassname. Passdeffind_elements_by_class_name (self, name):#Finds elements by class name.    Passdeffind_element_by_css_selector (Self, css_selector):#Finds an element by CSS selector.    Passdeffind_elements_by_css_selector (Self, css_selector):#Finds elements by CSS selector.    PassdefFind_element (self, by=by.id, value=None):#' Private ' method used by the find_element_by_* methods.    PassdefFind_elements (self, by=by.id, value=None):#' Private ' method used by the find_elements_by_* methods.    Pass

By looking at the source code, in fact, the above methods are called by

Self.find_element (by=by.xxx, Value=name) or self.find_elements (By=by.xxx, Value=name) method to redefine the

    defFind_element (self, by=by.id, value=None):"""' Private ' method used by the find_element_by_* methods.        : Usage:use the corresponding find_element_by_* instead of this. : Rtype:webelement"""        ifSELF.W3C:ifby = =By.ID:by=By.css_selector Value='[id= "%s"]'%valueelifby = =By.TAG_NAME:by=By.css_selectorelifby = =By.CLASS_NAME:by=By.css_selector Value=".%s"%valueelifby = =By.NAME:by=By.css_selector Value='[name= "%s"]'%valuereturnSelf.execute (command.find_element, {'using': By,'value': value}) ['value']

where by. XXX is a static constant defined by class in by.py file under Selenium\webdriver\common

classby (object):"""Set of supported locator strategies. """ID="ID"XPATH="XPath"Link_text="Link Text"Partial_link_text="Partial link text"NAME="name"tag_name="Tag Name"class_name="class name"Css_selector="CSS Selector"

Through the above analysis, it is not difficult to find, as long as the master self.find_element (by=by.xxx, Value=name) or self.find_elements (By=by.xxx, Value=name) method, Already means mastering all the ways to locate page elements

2, in addition to commonly used find class methods, the following methods are commonly used in scripting

EF get (self, url):"""Loads A Web page in the current browser session. """@propertydeftitle (self):"""Returns the title of the current page."""@propertydefCurrent_url (self):"""Gets the URL of the current page."""@propertydefCurrent_window_handle (self):"""Returns the handle of the current window."""defMaximize_window (self):"""maximizes The current window, Webdriver is using"""@propertydefswitch_to (self):returnself._switch_to#Target Locatorsdefswitch_to_active_element (self):"""Deprecated Use driver.switch_to.active_element"""defSwitch_to_window (Self, window_name):"""Deprecated Use Driver.switch_to.window"""defswitch_to_frame (Self, frame_reference):"""Deprecated Use Driver.switch_to.frame"""defswitch_to_default_content (self):"""Deprecated Use driver.switch_to.default_content"""defSwitch_to_alert (self):"""Deprecated Use Driver.switch_to.alert"""

Which is decorated with @property, can be used as a property, such as Driver.current_url

3, why in the actual application process through the From Selenium import Webdriver introduced Webdriver, and then through the webdriver. Chrome () can instantiate Chrome's driver object?

From the above selenium directory structure, theoretically need to be passed down to import

# Import Chrome's Webdriver  from Import Webdriver # importing Firefox's webdriver  from Import Webdriver # Import IE's webdriver  from Import Webdriver

Selenium project directory structure

Selenium
│__init__.py

├─common
││exceptions.py
││__init__.py

├─webdriver
││__init__.py
││
│├─android
│││webdriver.py
│││__init__.py
││
│├─blackberry
│││webdriver.py
│││__init__.py
││
│├─chrome
│││options.py
│││remote_connection.py
│││service.py
│││webdriver.py
│││__init__.py
││
│├─common
│││action_chains.py
│││alert.py
│││by.py
│││desired_capabilities.py
│││keys.py
│││proxy.py
│││service.py
│││touch_actions.py
│││utils.py
│││__init__.py
│││
││├─actions
││││action_builder.py
││││input_device.py
││││interaction.py
││││key_actions.py
││││key_input.py
││││mouse_button.py
││││pointer_actions.py
││││pointer_input.py
││││__init__.py
│││
│││
││├─html5
││││application_cache.py
││││__init__.py
││
│├─edge
│││options.py
│││service.py
│││webdriver.py
│││__init__.py
│├─firefox
│││extension_connection.py
│││firefox_binary.py
│││firefox_profile.py
│││options.py
│││remote_connection.py
│││service.py
│││webdriver.py
│││webdriver.xpi
│││webdriver_prefs.json
│││webelement.py
│││__init__.py
││
│├─ie
│││service.py
│││webdriver.py
│││__init__.py
│├─opera
│││options.py
│││webdriver.py
│││__init__.py
│├─phantomjs
│││service.py
│││webdriver.py
│││__init__.py
│├─remote
│││command.py
│││errorhandler.py
│││file_detector.py
│││getattribute.js
│││isdisplayed.js
│││mobile.py
│││remote_connection.py
│││switch_to.py
│││utils.py
│││webdriver.py
│││webelement.py
│││__init__.py
│├─safari
│││service.py
│││webdriver.py
│││__init__.py
│├─support
│││abstract_event_listener.py
│││color.py
│││events.py
│││event_firing_webdriver.py
│││expected_conditions.py
│││select.py
│││ui.py
│││wait.py
│││__init__.py

By looking at Selenium\webdriver under __init__.py file discovery

 from. firefox.webdriverImportWebdriver as Firefox#Noqa from. firefox.firefox_profileImportFirefoxprofile#Noqa from. chrome.webdriverImportWebdriver as Chrome#Noqa from. chrome.optionsImportOptions as Chromeoptions#Noqa from. ie.webdriverImportWebdriver as Ie#Noqa

In fact, because it has been imported, so you can directly use Firefox, Chrome

Selenium's Python source interpretation-webdriver inheritance relationship

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.