{"id":1014668,"date":"2024-12-27T11:58:21","date_gmt":"2024-12-27T03:58:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1014668.html"},"modified":"2024-12-27T11:58:24","modified_gmt":"2024-12-27T03:58:24","slug":"%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96app%e6%a0%87%e9%a2%98python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1014668.html","title":{"rendered":"\u5982\u4f55\u83b7\u53d6app\u6807\u9898python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25094733\/a3a47ec2-a50f-4ab4-b60b-ff072d19db69.webp\" alt=\"\u5982\u4f55\u83b7\u53d6app\u6807\u9898python\" \/><\/p>\n<p><p> <strong>\u83b7\u53d6\u5e94\u7528\u7a0b\u5e8f\u6807\u9898\u7684Python\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Android API\u3001\u901a\u8fc7Appium\u81ea\u52a8\u5316\u6d4b\u8bd5\u6846\u67b6\u3001\u5229\u7528APK\u89e3\u6790\u5de5\u5177\u3002<\/strong> \u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528Appium\u81ea\u52a8\u5316\u6d4b\u8bd5\u6846\u67b6\u662f\u4e00\u4e2a\u8f83\u4e3a\u901a\u7528\u4e14\u6613\u4e8e\u5b9e\u73b0\u7684\u9009\u62e9\u3002Appium\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u81ea\u52a8\u5316\u6d4b\u8bd5\u5de5\u5177\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u7f16\u5199\u6d4b\u8bd5\u811a\u672c\u6765\u63a7\u5236\u79fb\u52a8\u5e94\u7528\u7a0b\u5e8f\uff0c\u4ece\u800c\u8f7b\u677e\u83b7\u53d6\u5e94\u7528\u7a0b\u5e8f\u7684\u6807\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Appium\u65f6\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5Appium\u670d\u52a1\u5668\u4ee5\u53ca\u76f8\u5173\u7684\u5ba2\u6237\u7aef\u5e93\u3002\u901a\u8fc7\u7f16\u5199Python\u811a\u672c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Appium\u8fde\u63a5\u5230\u79fb\u52a8\u8bbe\u5907\u4e0a\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u5b9a\u4f4d\u6807\u9898\u5143\u7d20\u5e76\u83b7\u53d6\u5176\u6587\u672c\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u8bf4\u660e\u5982\u4f55\u4f7f\u7528Appium\u83b7\u53d6\u5e94\u7528\u7a0b\u5e8f\u6807\u9898\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u914d\u7f6eAppium<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528Appium\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u548c\u914d\u7f6eAppium\u670d\u52a1\u5668\u4ee5\u53ca\u76f8\u5173\u7684\u5ba2\u6237\u7aef\u5e93\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Node.js\u548cAppium\uff1a<\/strong><\/p>\n<p>Appium\u662f\u57fa\u4e8eNode.js\u7684\uff0c\u56e0\u6b64\u9996\u5148\u9700\u8981\u5b89\u88c5Node.js\u3002\u53ef\u4ee5\u4eceNode.js\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u5e76\u5b89\u88c5\u6700\u65b0\u7248\u672c\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u901a\u8fc7\u547d\u4ee4\u884c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5Appium\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">npm install -g appium<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5Appium\u5ba2\u6237\u7aef\u5e93\uff1a<\/strong><\/p>\n<p>\u5728Python\u73af\u5883\u4e2d\uff0c\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5Appium\u7684Python\u5ba2\u6237\u7aef\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Appium-Python-Client<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6eAndroid SDK\u548c\u8bbe\u5907\uff1a<\/strong><\/p>\n<p>\u786e\u4fddAndroid SDK\u5df2\u5b89\u88c5\uff0c\u5e76\u914d\u7f6e\u597d\u73af\u5883\u53d8\u91cf\u3002\u8fde\u63a5\u771f\u5b9e\u8bbe\u5907\u6216\u542f\u52a8Android\u6a21\u62df\u5668\uff0c\u542f\u7528\u5f00\u53d1\u8005\u9009\u9879\u548cUSB\u8c03\u8bd5\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Appium\u83b7\u53d6\u5e94\u7528\u6807\u9898<\/h3>\n<\/p>\n<p><p>\u5728\u5b8c\u6210Appium\u7684\u5b89\u88c5\u548c\u914d\u7f6e\u540e\uff0c\u53ef\u4ee5\u5f00\u59cb\u7f16\u5199Python\u811a\u672c\u6765\u83b7\u53d6\u5e94\u7528\u7a0b\u5e8f\u7684\u6807\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u811a\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from appium import webdriver<\/p>\n<h2><strong>\u914d\u7f6eDesired Capabilities<\/strong><\/h2>\n<p>desired_caps = {<\/p>\n<p>    &#39;platformName&#39;: &#39;Android&#39;,<\/p>\n<p>    &#39;platformVersion&#39;: &#39;9&#39;,  # \u8bbe\u5907\u7684Android\u7248\u672c<\/p>\n<p>    &#39;deviceName&#39;: &#39;emulator-5554&#39;,  # \u8bbe\u5907\u540d\u79f0<\/p>\n<p>    &#39;appPackage&#39;: &#39;com.example.myapp&#39;,  # \u5e94\u7528\u7a0b\u5e8f\u5305\u540d<\/p>\n<p>    &#39;appActivity&#39;: &#39;.M<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nActivity&#39;  # \u5e94\u7528\u7a0b\u5e8f\u4e3b\u6d3b\u52a8<\/p>\n<p>}<\/p>\n<h2><strong>\u542f\u52a8Appium\u4f1a\u8bdd<\/strong><\/h2>\n<p>driver = webdriver.Remote(&#39;http:\/\/localhost:4723\/wd\/hub&#39;, desired_caps)<\/p>\n<h2><strong>\u83b7\u53d6\u5e94\u7528\u7a0b\u5e8f\u6807\u9898<\/strong><\/h2>\n<h2><strong>\u5047\u8bbe\u6807\u9898\u5143\u7d20\u7684\u8d44\u6e90ID\u4e3a &quot;com.example.myapp:id\/title&quot;<\/strong><\/h2>\n<p>title_element = driver.find_element_by_id(&#39;com.example.myapp:id\/title&#39;)<\/p>\n<p>app_title = title_element.text<\/p>\n<p>print(f&quot;\u5e94\u7528\u7a0b\u5e8f\u6807\u9898: {app_title}&quot;)<\/p>\n<h2><strong>\u7ed3\u675f\u4f1a\u8bdd<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u89e3\u6790APK\u6587\u4ef6\u83b7\u53d6\u5e94\u7528\u4fe1\u606f<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528Appium\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u89e3\u6790APK\u6587\u4ef6\u6765\u83b7\u53d6\u5e94\u7528\u7a0b\u5e8f\u7684\u6807\u9898\u4fe1\u606f\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528APK\u89e3\u6790\u5de5\u5177\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528APKTool\u53cd\u7f16\u8bd1APK\uff1a<\/strong><\/p>\n<p>APKTool\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u53cd\u7f16\u8bd1\u5de5\u5177\uff0c\u53ef\u4ee5\u5c06APK\u6587\u4ef6\u53cd\u7f16\u8bd1\u6210\u53ef\u8bfb\u7684\u683c\u5f0f\u3002\u901a\u8fc7\u53cd\u7f16\u8bd1APK\uff0c\u53ef\u4ee5\u5728\u8d44\u6e90\u6587\u4ef6\u4e2d\u627e\u5230\u5e94\u7528\u7a0b\u5e8f\u7684\u6807\u9898\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u89e3\u6790AndroidManifest.xml\uff1a<\/strong><\/p>\n<p>\u5728\u53cd\u7f16\u8bd1\u540e\u7684\u6587\u4ef6\u5939\u4e2d\uff0c\u67e5\u627e<code>AndroidManifest.xml<\/code>\u6587\u4ef6\uff0c\u89e3\u6790\u5176\u4e2d\u7684<code>&lt;application&gt;<\/code>\u6807\u7b7e\uff0c\u901a\u5e38\u4f1a\u5305\u542b\u5e94\u7528\u7a0b\u5e8f\u7684\u6807\u7b7e\u4fe1\u606f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528aapt\u5de5\u5177\uff1a<\/strong><\/p>\n<p>aapt\uff08Android Asset Packaging Tool\uff09\u662fAndroid SDK\u4e2d\u7684\u4e00\u4e2a\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u63d0\u53d6APK\u6587\u4ef6\u7684\u4fe1\u606f\u3002\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u83b7\u53d6\u5e94\u7528\u7684\u6807\u9898\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">aapt dump badging app.apk | grep application-label<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u5229\u7528Android API\u83b7\u53d6\u5e94\u7528\u6807\u9898<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5728\u5e94\u7528\u7a0b\u5e8f\u5185\u90e8\u83b7\u53d6\u6807\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528Android API\u3002\u5728Android\u5e94\u7528\u5f00\u53d1\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u83b7\u53d6\u5e94\u7528\u6807\u9898\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u83b7\u53d6\u5f53\u524dActivity\uff1a<\/strong><\/p>\n<p>\u5728\u5e94\u7528\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>getActivity()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5f53\u524d\u6d3b\u52a8\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u83b7\u53d6\u5e94\u7528\u6807\u9898\uff1a<\/strong><\/p>\n<p>\u901a\u8fc7<code>getTitle()<\/code>\u65b9\u6cd5\u83b7\u53d6\u5f53\u524dActivity\u7684\u6807\u9898\uff0c\u6216\u8005\u901a\u8fc7<code>getApplicationInfo().labelRes<\/code>\u83b7\u53d6\u5e94\u7528\u7a0b\u5e8f\u7684\u6807\u7b7e\u8d44\u6e90ID\uff0c\u5e76\u901a\u8fc7<code>getString()<\/code>\u65b9\u6cd5\u83b7\u53d6\u6807\u9898\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662fAndroid\u5e94\u7528\u4e2d\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">Activity activity = getActivity();<\/p>\n<p>CharSequence title = activity.getTitle();<\/p>\n<p>System.out.println(&quot;\u5e94\u7528\u7a0b\u5e8f\u6807\u9898: &quot; + title);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u83b7\u53d6\u5e94\u7528\u7a0b\u5e8f\u6807\u9898\u7684Python\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u73af\u5883\u3002\u4f7f\u7528Appium\u8fdb\u884c\u81ea\u52a8\u5316\u6d4b\u8bd5\u662f\u4e00\u79cd\u901a\u7528\u4e14\u6613\u4e8e\u5b9e\u73b0\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\u3002\u800c\u89e3\u6790APK\u6587\u4ef6\u548c\u4f7f\u7528Android API\u5219\u9002\u7528\u4e8e\u7279\u5b9a\u60c5\u51b5\u4e0b\u7684\u9700\u6c42\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u5173\u952e\u5728\u4e8e\u719f\u6089\u5de5\u5177\u548cAPI\u7684\u4f7f\u7528\uff0c\u786e\u4fdd\u83b7\u53d6\u51c6\u786e\u7684\u5e94\u7528\u4fe1\u606f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u83b7\u53d6\u5e94\u7528\u7a0b\u5e8f\u7684\u6807\u9898\uff1f<\/strong><br \/>\u8981\u83b7\u53d6\u5e94\u7528\u7a0b\u5e8f\u7684\u6807\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>subprocess<\/code>\u6a21\u5757\u7ed3\u5408\u64cd\u4f5c\u7cfb\u7edf\u7684\u547d\u4ee4\u884c\u5de5\u5177\u3002\u4f8b\u5982\uff0c\u5728Windows\u4e0a\uff0c\u53ef\u4ee5\u4f7f\u7528<code>tasklist<\/code>\u547d\u4ee4\uff0c\u800c\u5728Linux\u4e0a\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528<code>ps<\/code>\u547d\u4ee4\u3002\u901a\u8fc7\u6267\u884c\u8fd9\u4e9b\u547d\u4ee4\u5e76\u89e3\u6790\u8f93\u51fa\uff0c\u53ef\u4ee5\u63d0\u53d6\u6240\u9700\u7684\u5e94\u7528\u7a0b\u5e8f\u6807\u9898\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u901a\u8fc7Python\u83b7\u53d6\u7279\u5b9a\u5e94\u7528\u7a0b\u5e8f\u7684\u6807\u9898\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>psutil<\/code>\u5e93\u6765\u76d1\u63a7\u7279\u5b9a\u8fdb\u7a0b\u5e76\u83b7\u53d6\u5176\u6807\u9898\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5<code>psutil<\/code>\u5e93\uff0c\u7136\u540e\u53ef\u4ee5\u901a\u8fc7\u8fdb\u7a0bID\u6216\u8fdb\u7a0b\u540d\u79f0\u627e\u5230\u8be5\u5e94\u7528\u7a0b\u5e8f\u7684\u8be6\u7ec6\u4fe1\u606f\uff0c\u5305\u62ec\u6807\u9898\u3002<\/p>\n<p><strong>\u5728\u83b7\u53d6\u5e94\u7528\u7a0b\u5e8f\u6807\u9898\u65f6\uff0c\u662f\u5426\u9700\u8981\u8003\u8651\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u5dee\u5f02\uff1f<\/strong><br \/>\u786e\u5b9e\uff0c\u83b7\u53d6\u5e94\u7528\u7a0b\u5e8f\u6807\u9898\u7684\u65b9\u6cd5\u53ef\u80fd\u56e0\u64cd\u4f5c\u7cfb\u7edf\u800c\u5f02\u3002Windows\u3001Linux\u548cmacOS\u6709\u4e0d\u540c\u7684\u547d\u4ee4\u548cAPI\u53ef\u4f9b\u4f7f\u7528\u3002\u56e0\u6b64\uff0c\u7f16\u5199\u8de8\u5e73\u53f0\u7684\u4ee3\u7801\u65f6\uff0c\u5e94\u8003\u8651\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u5904\u7406\u4e0d\u540c\u7684\u64cd\u4f5c\u7cfb\u7edf\uff0c\u4ee5\u786e\u4fdd\u80fd\u591f\u6b63\u786e\u83b7\u53d6\u6807\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u83b7\u53d6\u5e94\u7528\u7a0b\u5e8f\u6807\u9898\u7684Python\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Android API\u3001\u901a\u8fc7Appium\u81ea\u52a8\u5316\u6d4b\u8bd5\u6846\u67b6\u3001\u5229\u7528APK [&hellip;]","protected":false},"author":3,"featured_media":1014680,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1014668"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1014668"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1014668\/revisions"}],"predecessor-version":[{"id":1014682,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1014668\/revisions\/1014682"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1014680"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1014668"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1014668"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1014668"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}