Appium上下文和H5測試(二)

{"type":"doc","content":[{"type":"heading","attrs":{"align":"center","level":2},"content":[{"type":"text","text":"堅持原創輸出,點擊藍字關注我吧","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/6c/6c566c07e586927aa4268eeb3baf4ea3.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"作者:清菡","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"博客:oschina、雲+社區、知乎等各大平臺都有。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":"center","level":2},"content":[{"type":"text","text":"文章總覽圖","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/8e/8e21659ecfc6b6766c14be44051c13b3.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"一、往期回顧","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"loc='new UiSelector().text(\"全程班\")'\nWebDriverWait(driver,20).until(EC.visibility_of_element_located((MobileBy.ANDROID_UIAUTOMATOR,loc))\ndriver.find_element_by_android_uiautomator(loc).click()\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"這個步驟後進入了這個頁面:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ac/ac7bddc9e8d84a6adb0b07c8f24cf634.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"進入這個頁面也是需要時間的。WebView 這個元素當中,放的纔是 html 頁面。真的等到 html 頁面加載出來之後,再去獲取所有相關的內容,這樣比較好。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"萬一切過來的時候,html 頁面還沒有開始加載,我就馬上去獲取當前所有可以操作的對象,這樣很容易丟失,所以也一樣要講究等待。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"講究等待,首先等到 WebView 這個元素出現。等到 WebView 這個 class 控件出現,class 值代表它的控件。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"# 等待Web View元素出現 -Web View裏面放的是Html\nWebDriverWait(driver,20).until(EC.visibility_of_element_located((MobileBy.CLASS_NAME,'android.webkit.WebView')))","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"sleep","attrs":{}}],"attrs":{}},{"type":"text","text":"1 秒鐘,確保裏面的 html,所有的都能加載完成。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"time.sleep(1)","attrs":{}}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"二、怎麼切換?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"用什麼樣的語句來獲取我們的 WebView、獲取我們的原生控件呢?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"它這個東西在我們 App 當中叫做","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"context","attrs":{}}],"attrs":{}},{"type":"text","text":",翻譯成中文就是","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"上下文。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"上下文在我們自動化中就是指可以切換的東西,就是我們的原生控件。","attrs":{}},{"type":"text","text":" 原生控件是我們默認的,就像窗口切換就是我們默認的窗口是一樣的。原生控件是它的默認上下文。打開 app,默認就是在它的原生控件當中。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"WebView 就是它的第二種","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"context","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/9f/9f5b5f38c12e5fc935ce436cbb2fa2c8.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"只要當前頁面中有 WebView,它就會顯示出來,有 2 個就會顯示 2 個。如圖片中這個例子中只有一個 Webview,所以它只顯示一個 WebView。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b1/b1c06c964e1284dbdbe9dfa13053115e.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這就是","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"context","attrs":{}}],"attrs":{}},{"type":"text","text":"上下文。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"只有這種情況下需要切換,其它情況下都是原生控件就不需要切換,不用管它,一旦有 html 頁面就需要考慮這些事情了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"三、上下文切換","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"可用的上下文(Contexts)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"列出所有可用的上下文(contexts)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"driver.contexts","attrs":{}}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"driver.window_handles","attrs":{}}],"attrs":{}},{"type":"text","text":" 獲取所有窗口的 handle,返回 list 列表。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"當前上下文(context):列出當前的上下文(context)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"driver.current_context","attrs":{}}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"切換至默認的上下文(context)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"切換回默認的上下文(context)。(譯者注:一般就是原生上下文 “NATIVE_APP”)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"driver.switch_to.context(None)","attrs":{}}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"當前 Activity:獲取當前的 Acticity。僅支持 Android。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"driver.current_activity","attrs":{}}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"當前包名(package):獲取當前包名(package)。僅支持 Android 。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"driver.current_package","attrs":{}}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上下文的操作方式在這裏,和 Windows 窗口是一模一樣的。和 Web 自動化中所謂的窗口是一樣的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先列出所有可用的上下文。就像列出目前所有打開的窗口是一樣的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個上下文,有 WebView 的時候,也是在執行代碼的時候,它進入了有 WebView 的","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"頁面當中","attrs":{}},{"type":"text","text":",纔會有多個,沒有進入有 WebView 的頁面當中只有一個 WebView 的(相當於一個大箱子,箱子打開後有多個)。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"列出所有可用的上下文,再去切換至需要的上下文。怎麼切換呢?他們得到的結果也是個列表啊。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"列表當中放的值呢,不是原生控件就是 WebView。所以它也有下標。如果要切換的話就是","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"driver.switch_to.context(None)","attrs":{}}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"None 表示什麼呢?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"表示切換回默認的上下文,按照 Web 自動化的講法就是默認的窗口,在我們這裏就是默認的原生控件裏面。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你想切換到 WebView 的話,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"driver.contexts","attrs":{}}],"attrs":{}},{"type":"text","text":"返回值 0,列表取下標 1,2,3,4 都是可以取得。也可以將你得到的 Web 名稱放在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"driver.switch_to.context(None)","attrs":{}}],"attrs":{}},{"type":"text","text":"中替換 None 就可以了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"driver.switch_to.context(None)","attrs":{}}],"attrs":{}},{"type":"text","text":"可以切進去,也可以切出來。如果你想獲取當前的窗口,當前的上下文,叫做","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"driver.current_context","attrs":{}}],"attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"它的做法與窗口是一模一樣的。","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Web 自動化中叫做窗口,這裏叫做上下文。","attrs":{}},{"type":"text","text":" 其它的時候不需要切換,但是有窗口需要交替的時候就必須要切換。有 iframe,需要更換 html 頁面的時候就需要切換,其它情況下就不切換。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"現在在這個地方已經等到了這個所有的 WebView 出現了,所以接下來這樣做:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/4c/4cd16ac8e86630be87a37b64fcd7adc0.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"button[@class=\"bottom-btn buy\"]","attrs":{}}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/a8/a8e36cd47441c787ffca40b2719142a1.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"相當於 App 自動化和 Web 自動化組合起來用了,無縫切換,不需要改什麼,照着套路用就好了:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"# 切換之後:當前的操作對象:html頁面。\n# 等待元素可見\n# 因爲是通用的,所以接下來的代碼是web自動化的代碼\nWebDriverWait(driver,20).until(EC.visibility_of_element_located((MobileBy.XPATH,'//button[@class=\"bottom-btn buy\"]')))\n# 這個用Mobileby或者By都無所謂。\ndriver.find_element_by_xpath('//button[@class=\"bottom-btn buy\"]').click()","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/6f/6f5422b4b266806312e92e6bb024bee5.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"列出了當前的上下文:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"['NATIVE_APP', 'WEBVIEW_com.tencent.mobileqq:mini', 'WEBVIEW_com.保密']","attrs":{}}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"NATIVE_APP 是當前的原生控件,按照 web 自動化來說,是默認的主窗口。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"是因爲這段代碼:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"# 1、先列出所有的context\ncons=driver.contexts #列表\n#也是按照出現的先後順序,WebView是操作過程中才出現的,所以它肯定排隊。\nprint(cons)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/23/231f08b3b20f352b77900d31c6d0dac7.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"一定要開啓 webview debug 屬性,如果你沒有開啓它,那麼這 2 項,在這裏獲取的時候是看不到的:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ac/acf73ae3f258f517759aad2f69e02d94.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"就只有一個了,就是 NATIVE","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"APP。只能看到 NATIVE","attrs":{}},{"type":"text","text":"APP 是切換不到 WebView 的。一定要保證能夠識別得到,才能夠去切換。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"技巧:","attrs":{}},{"type":"text","text":" 報錯的時候先看第一行代碼,看看在你自己當前腳本當中到底是哪一行出錯了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"四、樣例代碼","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"手機設置中開啓着顯示佈局邊界的情況下,然後 run 代碼。因爲 App 界面有變更,所以代碼和現有界面不一致,即","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"立即購買","attrs":{}},{"type":"text","text":"現在成了","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"報名截止並跳轉至 QQ 界面","attrs":{}},{"type":"text","text":"。代碼提示找不到元素請不要奇怪,因爲爲了便於理解,放的元素還是","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"立即購買","attrs":{}},{"type":"text","text":"的元素。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"此代碼只是樣例,不一定保證在你的電腦上就能運行成功,請根據實際情況修改。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"python"},"content":[{"type":"text","text":"from appium import webdriver\nimport time\nfrom selenium.webdriver.support.wait import WebDriverWait\nfrom selenium.webdriver.support import expected_conditions as EC\nfrom appium.webdriver.common.mobileby import MobileBy\n\n\ndesired_caps={}\n# 平臺類型\ndesired_caps[\"platformName\"]=\"Android\"\n# 平臺版本號\ndesired_caps[\"platformVersion\"]=\"10\"\n# 設備名稱\ndesired_caps[\"deviceName\"]=\"2NSDU20410017297\"\n# app 包名\ndesired_caps[\"appPackage\"]=\"輸入appPackage\"\n# app 入口 acitivity\ndesired_caps[\"appActivity\"]=\"輸入appActivity\"\n\n\n# 連接Appium server。前提:appium desktop要啓動。有監聽端口。\n# 將desired_caps發送給appium server。打開app\ndriver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)\n\nloc='new UiSelector().text(\"全程班\")'\nWebDriverWait(driver,20).until(EC.visibility_of_element_located((MobileBy.ANDROID_UIAUTOMATOR,loc)))\ndriver.find_element_by_android_uiautomator(loc).click()\n\n\n\n# 等待Web View元素出現 -Web View裏面放的是Html\nWebDriverWait(driver,20).until(EC.visibility_of_element_located((MobileBy.CLASS_NAME,'android.webkit.WebView')))\n# 因爲只是等它這個元素出現了,至於裏面的html有沒有加載完成,並不是很確定。\ntime.sleep(1)#爲了穩定起見,稍微sleep 1秒,確保裏面的Html,所有的都能加載完成。\n\n# 前提:可以識別到WebView,\n# 這個識別不是肉眼識別,而是通過調用代碼的時候可以識別。需要開啓app的webview debug調試屬性,對外可見。\n\n# context #原生控件 #webview\n\n# 1、先列出所有的context\ncons=driver.contexts #列表\n#也是按照出現的先後順序,WebView是操作過程中才出現的,所以它肯定排隊。\nprint(cons)\n\n# 2、切換至WebView,要確保chromedriver的版本要與webView的版本匹配。也要放置在對應的位置。\ndriver.switch_to.context(cons[-1])#這個地方沒有給你提示,不代表你錯了,照着操作就好了。\n# 先寫個-1,因爲現在不知道WebView的名字。但是知道WebView一定是出現在最後的就可以了。\n\n# 3、切換之後:當前的操作對象:html頁面。\n# 等待元素可見\n# 因爲是通用的,所以接下來的代碼是web自動化的代碼\nWebDriverWait(driver,20).until(EC.visibility_of_element_located((MobileBy.XPATH,'//button[@class=\"bottom-btn buy\"]')))\n# 這個用Mobileby或者By都無所謂。\ndriver.find_element_by_xpath('//button[@class=\"bottom-btn buy\"]').click()\n# 這裏爲什麼用MobileBy.XPATH而不是By.XPATH?\n# MobileBy這個類繼承了By,所以這個用Mobileby或者By都無所謂。\n# 原理:大家背後走的都是同一套邏輯,同一段請求,同一種命令。都是find_element\n# 只不過我們查找元素的方式不一樣。所以它只是一個外部的形式而已,在內部也是一樣的。\n# 即便用Selenium Webdriver 寫代碼,也可以把MobileBy引進來,只要我不去用移動端的定位方式,都是可以做的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"遇到的問題","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"appium 報錯 session not created: This version of ChromeDriver only supports Chrome version 84","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"靠譜鏈接:","attrs":{}},{"type":"text","text":" https://www.codenong.com/jsb8d9e8746809/","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"溫馨提示:","attrs":{}},{"type":"text","text":" 如果你的代碼沒問題,還報錯,那麼就換 Appium 版本吧,Appium 的 bug 很多。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上篇文章 中此處代碼錯了,應該改成這樣:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/d8/d8838420592575aedb0467f81159c54f.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule","attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"公衆號 ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"清菡軟件測試","attrs":{}},{"type":"text","text":" 首發,更多原創文章:清菡軟件測試 95+原創文章,歡迎關注、交流,禁止第三方擅自轉載。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章