{"id":237417,"date":"2022-03-22T09:26:01","date_gmt":"2022-03-22T01:26:01","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=237417"},"modified":"2022-03-11T11:26:45","modified_gmt":"2022-03-11T03:26:45","slug":"selenium-get-mouse-pointing","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/selenium-get-mouse-pointing.html","title":{"rendered":"\u4f7f\u7528\u5c0f\u6280\u5de7\u6559\u4f60\u7528Selenium\u83b7\u53d6\u9f20\u6807\u6307\u5411\u7684\u5143\u7d20"},"content":{"rendered":"
\u6709\u4e00\u4e2a\u540c\u5b66\u5728Gne\u7684\u7fa4\u91cc\u9762\u54a8\u8be2\u5982\u4f55\u901a\u8fc7Selenium\u83b7\u53d6\u5f53\u524d\u9f20\u6807\u6307\u5411\u7684\u5143\u7d20\uff0c\u5728\u6211\u8bb2\u4e86\u65b9\u6cd5\u4ee5\u540e\uff0c\u4ed6\u8fc7\u4e86\u4e24\u5929\u53c8\u6765\u95ee\uff1a
\n
\n\u90a3\u4e48\uff0c\u6211\u4eca\u5929\u5c31\u6765\u5199\u4e00\u7bc7\u6587\u7ae0\uff0c\u5177\u4f53\u8bf4\u8bf4\u5e94\u8be5\u600e\u4e48\u64cd\u4f5c\u3002<\/p>\n
\u8fd9\u4e2a\u65b9\u6cd5\u7684\u6838\u5fc3\uff0c\u662f\u501f\u52a9JavaScript\u7684\u4e8b\u4ef6(event)\u6765\u83b7\u53d6\u9f20\u6807\u6240\u5728\u7684\u5143\u7d20\u3002\u7136\u540e\u518d\u628a\u8fd9\u4e2a\u5143\u7d20\u4f20\u9012\u7ed9Selenium\u3002\u6211\u4eec\u5148\u6765\u7b2c\u4e00\u6b65\uff0c\u4e0d\u8003\u8651Selenium\uff0c\u53ea\u4f7f\u7528JavaScript\uff0c\u5982\u4f55\u83b7\u53d6\u5f53\u524d\u9f20\u6807\u6307\u5411\u7684\u5143\u7d20\u5462?<\/p>\n
\u6211\u4eec\u9996\u5148\u9700\u8981\u77e5\u9053\u5728JavaScript\u4e2d\u7684\u4e00\u4e2a\u4e8b\u4ef6\u53e5\u67c4\uff0c\u53eb\u505awindow.onmousemove\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5b83\u7684\u503c\u662fnull\uff1a
\n
\n\u6211\u4eec\u53ef\u4ee5\u628a\u5b83\u7684\u503c\u4fee\u6539\u6210\u4e00\u4e2a\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u63a5\u6536\u4e00\u4e2aevent\u53c2\u6570\uff0c\u8fd9\u6837\u5f53\u9f20\u6807\u5728\u7f51\u9875\u4e0a\u79fb\u52a8\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u51fd\u6570\u5c31\u4f1a\u88ab\u8c03\u7528\u3002\u800cevent\u53c2\u6570\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u6709\u4e24\u4e2a\u5c5e\u6027.clientX\u548c.clientY\uff0c\u5206\u522b\u8868\u793a\u9f20\u6807\u76f8\u5f53\u4e8e\u7f51\u9875\u7684\u6a2a\u5750\u6807\u548c\u7eb5\u5750\u6807\uff1a<\/p>\n
\r\nfunction track_mouse(event){\r\n var x = event.clientX, y = event.clientY\r\n console.log('\u5f53\u524d\u9f20\u6807\u6240\u5728\u4f4d\u7f6e\u7684\u5750\u6807\uff1ax=' + x + 'y=' + y)\r\n}\r\n<\/pre>\n\u8fd0\u884c\u6548\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n
<\/p>\n
\u4f60\u6267\u884c\u547d\u4ee4\u4ee5\u540e\uff0c\u53ea\u8981\u5728\u9875\u9762\u4e0a\u79fb\u52a8\u9f20\u6807\uff0c\u4f60\u5c31\u4f1a\u5728\u63a7\u5236\u53f0\u770b\u5230\u5927\u91cf\u7684\u5750\u6807\u88ab\u6253\u5370\u51fa\u6765\u3002<\/p>\n
\u63a5\u4e0b\u6765\uff0c\u65e2\u7136\u4f60\u6709\u4e86\u5f53\u524d\u9f20\u6807\u6240\u5728\u4f4d\u7f6e\u7684\u5750\u6807\uff0c\u90a3\u4e48\u4f60\u53ea\u9700\u8981\u6839\u636e\u5750\u6807\u67e5\u8be2\u5230\u8fd9\u4e2a\u5143\u7d20\u662f\u4ec0\u4e48\u5c31\u53ef\u4ee5\u4e86\u3002\u5728JavaScript\u4e2d\uff0c\u6709\u4e00\u4e2a\u51fd\u6570\u53eb\u505adocument.elementFromPoint\uff0c\u5c31\u80fd\u5b9e\u73b0\u8fd9\u4e2a\u6548\u679c\uff1a<\/p>\n
\r\nfunction track_mouse(event){\r\n var x = event.clientX, y = event.clientY\r\n var element = document.elementFromPoint(x, y)\r\n if (!element) {\r\n return \/\/ \u5f53\u524d\u4f4d\u7f6e\u6ca1\u6709\u5143\u7d20\r\n } \r\n return element\r\n}\r\n<\/pre>\n\u90a3\u4e48\uff0c\u5982\u4f55\u628a\u8fd9\u4e2a\u53c2\u6570\u8fd4\u56de\u7ed9Selenium\u5462?\u5176\u5b9e\u4e5f\u975e\u5e38\u7b80\u5355\uff0c\u6211\u4eec\u8bbe\u7f6e\u4e00\u4e2a\u5168\u5c40\u53d8\u91cfwindow.hovered_element\uff0c\u7136\u540e\u628a\u5f53\u524d\u9f20\u6807\u5bf9\u5e94\u7684\u5143\u7d20\u8d4b\u503c\u7ed9\u5b83\u3002\u7136\u540e\u5728Selenium\u4e2d\uff0c\u4f7f\u7528.execute_script\u83b7\u53d6window.hovered_element\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n
\u6211\u4eec\u5148\u6765\u770b\u770b\u5b8c\u6574\u7684JavaScript\uff1a<\/p>\n
\r\nwindow.hovered_element = null\r\nfunction track_mouse(event){\r\n var x = event.clientX, y = event.clientY\r\n var element = document.elementFromPoint(x, y)\r\n if (!element) {\r\n window.hovered_element = null\r\n return \/\/ \u5f53\u524d\u4f4d\u7f6e\u6ca1\u6709\u5143\u7d20\r\n } \r\n window.hovered_element = element\r\n}\r\nwindow.onmousemove = track_mouse\r\n<\/pre>\n\u7136\u540e\u6211\u4eec\u518d\u6765\u770b\u770bSelenium\u4e2d\u7684Python\u4ee3\u7801\uff1a<\/p>\n
\r\nimport time\r\nfrom selenium.webdriver import Chrome\r\n\r\ndriver = Chrome('.\/chromedriver')\r\ndriver.get('https:\/\/www.kingname.info\/')\r\n\r\njs = '''\r\nwindow.hovered_element = null\r\nfunction track_mouse(event){\r\n var x = event.clientX, y = event.clientY\r\n var element = document.elementFromPoint(x, y)\r\n if (!element) {\r\n window.hovered_element = null\r\n return \/\/ \u5f53\u524d\u4f4d\u7f6e\u6ca1\u6709\u5143\u7d20\r\n } \r\n window.hovered_element = element\r\n}\r\nwindow.onmousemove = track_mouse\r\n'''\r\n\r\ndriver.execute_script(js)\r\nwhile True:\r\n element = driver.execute_script('return window.hovered_element')\r\n if element:\r\n print(f'\u5f53\u524d\u9f20\u6807\u6240\u5728\u7684\u6807\u7b7e\u4e3a\uff1a{element.tag_name}, \u5176\u4e2d\u7684\u6587\u672c\u5185\u5bb9\u4e3a\uff1a{element.text}')\r\n time.sleep(1)\r\n<\/pre>\n\u8fd0\u884c\u6548\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n
<\/p>\n
\u83b7\u53d6\u5230\u4e86\u5f53\u524d\u9f20\u6807\u6240\u5728\u7684\u5143\u7d20\u7684\u6807\u7b7e\u548c\u6807\u7b7e\u91cc\u9762\u7684\u6587\u5b57\u3002<\/p>\n
\u5230\u8fd9\u91cc\uff0c\u8fd9\u4e2a\u540c\u5b66\u9700\u8981\u7684\u529f\u80fd\u5df2\u7ecf\u5b8c\u5168\u5b9e\u73b0\u4e86\u3002<\/p>\n
\u4f46\u53ef\u80fd\u6709\u806a\u660e\u7684\u540c\u5b66\u4f1a\u53d1\u73b0\uff0c\u4ed6\u8fd9\u4e2a\u9700\u6c42\u662f\u6709\u95ee\u9898\u7684\u3002\u6211\u4eec\u80fd\u770b\u5230\u81f3\u5c11\u6709\u4e09\u4e2a\u95ee\u9898\uff1a<\/p>\n
\u56e0\u4e3awindow.onmousemove\u592a\u7075\u654f\u4e86\uff0c\u5b83\u7684\u91c7\u6837\u65f6\u95f4\u662f\u6beb\u79d2\u7ea7\u522b\u7684\uff0c\u9f20\u6807\u7a0d\u7a0d\u79fb\u52a8\u4e00\u70b9\u70b9\u5c31\u4f1a\u751f\u6210\u4e00\u4e2a\u4e8b\u4ef6\u3002\u4f46\u662f\uff0c\u4e00\u4e2a\u5143\u7d20\u7684\u533a\u57df\u662f\u5f88\u5927\u7684\uff0c\u5728\u4e00\u4e2a\u5143\u7d20\u5185\u90e8\u79fb\u52a8\u9f20\u6807\uff0c\u5176\u5b9e\u6839\u672c\u6ca1\u6709\u5fc5\u8981\u66f4\u65b0window.hovered_element\u3002
\n\u5728Selenium\u91cc\u9762\uff0c\u662f\u901a\u8fc7while True\u6bcf1\u79d2\u67e5\u8be2\u4e00\u6b21window.hovered_element\uff0c\u867d\u7136\u6211\u4eec\u5df2\u7ecf\u964d\u4f4e\u4e86\u9891\u7387\uff0c\u4f46\u5927\u5bb6\u4ece\u4e0a\u9762\u7684\u56fe\u4e2d\u53ef\u4ee5\u770b\u5230\uff0c\u8fd8\u662f\u4f1a\u83b7\u53d6\u5230\u5f88\u591a\u91cd\u590d\u7684\u6570\u636e\u3002\u8fd9\u662f\u7531\u4e8e\u6709\u4e00\u4e9b\u5143\u7d20\u975e\u5e38\u5927\uff0c\u6211\u4eec\u9f20\u6807\u5982\u679c\u5728\u4e0a\u9762\u6162\u6162\u79fb\u52a8\uff0c\u65f6\u95f4\u4f1a\u8d85\u8fc71\u79d2\uff0c\u90a3\u4e48Selenium\u5c31\u4f1a\u91cd\u590d\u83b7\u53d6\u5230\u6570\u636e\u3002
\n\u7531\u4e8ewindow.onmousemove\u7684\u91c7\u6837\u65f6\u95f4\u95f4\u9694\u5f88\u5c0f\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u8fd1\u4f3c\u628a\u9f20\u6807\u7684\u79fb\u52a8\u770b\u505a\u662f\u8fde\u7eed\u7684\u79fb\u52a8\u3002\u56e0\u6b64\uff0c\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u8bb0\u5f55\u9f20\u6807\u8f68\u8ff9\u8def\u5f84\u4e0a\u9762\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u3002\u4f46\u5b9e\u9645\u4e0a\uff0c\u6211\u4eec\u5e76\u4e0d\u4f1a\u5bf9\u7f51\u9875\u4e0a\u6240\u6709\u7684\u5185\u5bb9\u90fd\u611f\u5174\u8da3\uff0c\u6211\u4eec\u53ea\u4f1a\u5bf9\u7279\u5b9a\u7684\u5185\u5bb9\u611f\u5174\u8da3\u3002\u56e0\u6b64\uff0c\u83b7\u53d6\u5f53\u524d\u9f20\u6807\u6240\u5728\u4f4d\u7f6e\u7684\u5143\u7d20\uff0c\u5176\u5b9e\u662f\u4e00\u4e2a\u4f2a\u9700\u6c42\uff0c\u5b83\u6839\u672c\u6ca1\u6709\u4ec0\u4e48\u5b9e\u9645\u4e0a\u7528\u5904\uff0c\u56e0\u4e3a\u566a\u58f0\u592a\u5927\u4e86\uff0c\u65e0\u7528\u7684\u6570\u636e\u592a\u591a\u4e86!
\n\u5b9e\u9645\u4e0a\uff0c\u6211\u89c9\u5f97\u771f\u6b63\u7684\u9700\u6c42\u5e94\u8be5\u662f\u8fd9\u6837\u7684\uff1a\u5982\u679c\u9f20\u6807\u5728\u7f51\u9875\u4e0a\u9762\u67d0\u4e2a\u5143\u7d20\u505c\u7559\u65f6\u95f4\u8d85\u8fc75\u79d2\uff0c\u90a3\u4e48\u83b7\u53d6\u8fd9\u4e2a\u5143\u7d20\u3002<\/p>\n\u4f46\u8fd9\u6837\u505a\u592a\u8d39\u65f6\u95f4\u4e86\u3002\u6bcf\u6b21\u90fd\u8981\u7b495\u79d2\uff0c\u5c82\u4e0d\u662f\u5e26\u85aa\u6478\u9c7c?\u90a3\u9700\u6c42\u80fd\u4e0d\u80fd\u6539\u6210\u83b7\u53d6\u5f53\u524d\u9f20\u6807\u70b9\u51fb\u7684\u5143\u7d20\u5462?\u5982\u679c\u4f60\u5b9e\u8df5\u4e00\u4e0b\uff0c\u4f60\u4f1a\u53d1\u73b0\uff0c\u5f53\u4f60\u70b9\u51fb\u4e00\u4e2a\u94fe\u63a5\u7684\u65f6\u5019\uff0c\u7f51\u9875\u81ea\u52a8\u5c31\u8df3\u8f6c\u5230\u53e6\u4e00\u4e2a\u9875\u9762\u53bb\u4e86\uff0c\u5e76\u4e0d\u80fd\u83b7\u53d6\u5230\u4f60\u9700\u8981\u7684\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u6709\u4e00\u4e2a\u540c\u5b66\u5728Gne\u7684\u7fa4\u91cc\u9762\u54a8\u8be2\u5982\u4f55\u901a\u8fc7Selenium\u83b7\u53d6\u5f53\u524d\u9f20\u6807\u6307\u5411\u7684\u5143\u7d20\uff0c\u5728\u6211\u8bb2\u4e86\u65b9\u6cd5\u4ee5\u540e\uff0c\u4ed6\u8fc7\u4e86\u4e24\u5929\u53c8\u6765 […]<\/p>\n","protected":false},"author":1898,"featured_media":237422,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[828],"class_list":["post-237417","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-selenium"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/237417","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=237417"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/237417\/revisions"}],"predecessor-version":[{"id":237424,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/237417\/revisions\/237424"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/237422"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=237417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=237417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=237417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}