Appium 在切換到 webview 後,正肯定位到元素,可是click () 事件後界面無響應,腳本運行正常不會報錯。
主要緣由是:混合APP 時監聽全用的是tap事件,不是click事件html
在使用appium,切換到webview後,點擊webview裏面的某個標籤,已經定位到元素了web
webview切換成功,而且已經定位到裏面的‘酒店問題’這個元素app
driver.switch_to.context("WEBVIEW_com.yipiao") print(driver.current_context) time.sleep(3) # click無效 driver.find_element_by_xpath('//*[text()="酒店問題"]').click()
這個問題主要緣由是開發寫的webview界面的元素監聽的是tap事件,不是click事件。selenium裏面有個TouchActions類,能夠操做tap事件,
以前在寫淘寶的wap模式時候有提到過http://www.javashuo.com/article/p-anydydxk-hy.htmlcode
from selenium.webdriver.common.touch_actions import TouchActions class TouchAction(object): def __init__(self, driver=None): self._driver = driver self._actions = [] def tap(self, element=None, x=None, y=None, count=1): 模擬手指觸摸屏 def press(self, el=None, x=None, y=None): 短按:模擬手指按住一個元素,或者座標 def long_press(self, el=None, x=None, y=None, duration=1000): 長按:模擬按住一個元素,或者座標 def wait(self, ms=0): 按住元素後的等待時間 def move_to(self, el=None, x=None, y=None): 移動手指到另一個元素,或者座標,注意這裏座標不是絕對座標,是偏移量 def release(self): 釋放手指 def perform(self):
解決辦法:先定位到元素後,用tap方法操做元素orm
driver.switch_to.context("WEBVIEW_com.yipiao") print(driver.current_context) # click無效 # driver.find_element_by_xpath('//*[text()="酒店問題"]').click() # 解決辦法 from selenium.webdriver.common.touch_actions import TouchActions el = driver.find_element_by_xpath('//*[text()="酒店問題"]') TouchActions(driver).tap(el).perform()
注意:這裏是selenium裏面的TouchActions
不是appium裏面的TouchAction http://www.javashuo.com/article/p-asrketnp-mt.htmlhtm
appiumQQ羣:330467341blog