先来看最重要的一个文件:webdriver.py
https://github.com/appium/python-client/tree/master/appium/webdriver
#!/usr/bin/env python
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from selenium import webdriver
from .mobilecommand import MobileCommand as Command
from .errorhandler import MobileErrorHandler
from .switch_to import MobileSwitchTo
from .webelement import WebElement as MobileWebElement
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.common.multi_action import MultiAction
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
class WebDriver(webdriver.Remote):
def __init__(self, command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities=None, browser_profile=None, proxy=None, keep_alive=False):
super(WebDriver, self).__init__(command_executor, desired_capabilities, browser_profile, proxy, keep_alive)
if self.command_executor is not None:
self._addCommands()
self.error_handler = MobileErrorHandler()
self._switch_to = MobileSwitchTo(self)
# add new method to the `find_by_*` pantheon
By.IOS_UIAUTOMATION = MobileBy.IOS_UIAUTOMATION
By.IOS_PREDICATE = MobileBy.IOS_PREDICATE
By.IOS_CLASS_CHAIN = MobileBy.IOS_CLASS_CHAIN
By.ANDROID_UIAUTOMATOR = MobileBy.ANDROID_UIAUTOMATOR
By.ACCESSIBILITY_ID = MobileBy.ACCESSIBILITY_ID
@property
def contexts(self):
"""
Returns the contexts within the current session.
:Usage:
driver.contexts
"""
return self.execute(Command.CONTEXTS)['value']
@property
def current_context(self):
"""
Returns the current context of the current session.
:Usage:
driver.current_context
"""
return self.execute(Command.GET_CURRENT_CONTEXT)['value']
@property
def context(self):
"""
Returns the current context of the current session.
:Usage:
driver.context
"""
return self.current_context
def find_element_by_ios_uiautomation(self, uia_string):
"""Finds an element by uiautomation in iOS.
:Args:
- uia_string - The element name in the iOS UIAutomation library
:Usage:
driver.find_element_by_ios_uiautomation('.elements()[1].cells()[2]')
"""
return self.find_element(by=By.IOS_UIAUTOMATION, value=uia_string)
def find_elements_by_ios_uiautomation(self, uia_string):
"""Finds elements by uiautomation in iOS.
:Args:
- uia_string - The element name in the iOS UIAutomation library
:Usage:
driver.find_elements_by_ios_uiautomation('.elements()[1].cells()[2]')
"""
return self.find_elements(by=By.IOS_UIAUTOMATION, value=uia_string)
def find_element_by_ios_predicate(self, predicate_string):
"""Find an element by ios predicate string.
:Args:
- predicate_string - The predicate string
:Usage:
driver.find_element_by_ios_predicate('label == "myLabel"')
"""
return self.find_element(by=By.IOS_PREDICATE, value=predicate_string)
def find_elements_by_ios_predicate(self, predicate_string):
"""Finds elements by ios predicate string.
:Args:
- predicate_string - The predicate string
:Usage:
driver.find_elements_by_ios_predicate('label == "myLabel"')
"""
return self.find_elements(by=By.IOS_PREDICATE, value=predicate_string)
def find_element_by_ios_class_chain(self, class_chain_string):
"""Find an element by ios class chain string.
:Args:
- class_chain_string - The class chain string
:Usage:
driver.find_element_by_ios_class_chain('XCUIElementTypeWindow/XCUIElementTypeButton[3]')
"""
return self.find_element(by=By.IOS_CLASS_CHAIN, value=class_chain_string)
def find_elements_by_ios_class_chain(self, class_chain_string):
"""Finds elements by ios class chain string.
:Args:
- class_chain_string - The class chain string
:Usage:
driver.find_elements_by_ios_class_chain('XCUIElementTypeWindow[2]/XCUIElementTypeAny[-2]')
"""
return self.find_elements(by=By.IOS_CLASS_CHAIN, value=class_chain_string)
def find_element_by_android_uiautomator(self, uia_string):
"""Finds element by uiautomator in Android.
:Args:
- uia_string - The element name in the Android UIAutomator library
:Usage:
driver.find_element_by_android_uiautomator('.elements()[1].cells()[2]')
"""
return self.find_element(by=By.ANDROID_UIAUTOMATOR, value=uia_string)
def find_elements_by_android_uiautomator(self, uia_string):
"""Finds elements by uiautomator in Android.
:Args:
- uia_string - The element name in the Android UIAutomator library
:Usage:
driver.find_elements_by_android_uiautomator('.elements()[1].cells()[2]')
"""
return self.find_elements(by=By.ANDROID_UIAUTOMATOR, value=uia_string)
def find_element_by_accessibility_id(self, id):
"""Finds an element by accessibility id.
:Args:
- id - a string corresponding to a recursive element search using the
Id/Name that the native Accessibility options utilize
:Usage:
driver.find_element_by_accessibility_id()
"""
return self.find_element(by=By.ACCESSIBILITY_ID, value=id)
def find_elements_by_accessibility_id(self, id):
"""Finds elements by accessibility id.
:Args:
- id - a string corresponding to a recursive element search using the
Id/Name that the native Accessibility options utilize
:Usage:
driver.find_elements_by_accessibility_id()
"""
return self.find_elements(by=By.ACCESSIBILITY_ID, value=id)
def create_web_element(self, element_id):
"""