{"id":755,"date":"2023-02-26T16:06:59","date_gmt":"2023-02-26T08:06:59","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=755"},"modified":"2023-04-29T17:00:47","modified_gmt":"2023-04-29T09:00:47","slug":"python-selenium-keyboard-events","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/python-selenium-keyboard-events\/","title":{"rendered":"Python Selenium\u952e\u76d8\u4e8b\u4ef6"},"content":{"rendered":"<h2>Keys\u7c7b<\/h2>\n<p><code>Keys<\/code>\u7c7b\u63d0\u4f9b\u4e86\u952e\u76d8\u4e0a\u51e0\u4e4e\u6240\u6709\u6309\u952e\u7684\u65b9\u6cd5\u3002\u524d\u9762\u4e86\u89e3\u5230\uff0c<code>send_keys()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u6a21\u62df\u952e\u76d8\u8f93\u5165\uff0c\u9664\u6b64\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u7528\u5b83\u6765\u8f93\u5165\u952e\u76d8\u4e0a\u7684\u6309\u952e\uff0c\u751a\u81f3\u662f\u7ec4\u5408\u952e\uff0c\u5982<code>Ctrl+A<\/code>\u3001<code>Ctrl+C<\/code> \u7b49\u3002<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-python\">from selenium import webdriver\n# \u5f15\u5165 Keys \u6a21\u5757\nfrom selenium.webdriver.common.keys import Keys\n\ndriver = webdriver.Chrome()\ndriver.get(&quot;http:\/\/www.baidu.com&quot;)\n\n# \u8f93\u5165\u6846\u8f93\u5165\u5185\u5bb9\ndriver.find_element_by_id(&quot;kw&quot;).send_keys(&quot;seleniumm&quot;)\n\n# \u5220\u9664\u591a\u8f93\u5165\u7684\u4e00\u4e2a m\ndriver.find_element_by_id(&quot;kw&quot;).send_keys(Keys.BACK_SPACE)\n\n# \u8f93\u5165\u7a7a\u683c\u952e+\u201c\u6559\u7a0b\u201d\ndriver.find_element_by_id(&quot;kw&quot;).send_keys(Keys.SPACE)\ndriver.find_element_by_id(&quot;kw&quot;).send_keys(&quot;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&quot;)\n\n# ctrl+a \u5168\u9009\u8f93\u5165\u6846\u5185\u5bb9\ndriver.find_element_by_id(&quot;kw&quot;).send_keys(Keys.CONTROL, &#039;a&#039;)\n\n# ctrl+x \u526a\u5207\u8f93\u5165\u6846\u5185\u5bb9\ndriver.find_element_by_id(&quot;kw&quot;).send_keys(Keys.CONTROL, &#039;x&#039;)\n\n# ctrl+v \u7c98\u8d34\u5185\u5bb9\u5230\u8f93\u5165\u6846\ndriver.find_element_by_id(&quot;kw&quot;).send_keys(Keys.CONTROL, &#039;v&#039;)\n\n# \u901a\u8fc7\u56de\u8f66\u952e\u6765\u4ee3\u66ff\u5355\u51fb\u64cd\u4f5c\ndriver.find_element_by_id(&quot;su&quot;).send_keys(Keys.ENTER)\ndriver.quit()<\/code><\/pre>\n<p>\u9700\u8981\u8bf4\u660e\u7684\u662f\uff0c\u4ee5\u4e0a\u811a\u672c\u6ca1\u6709\u4ec0\u4e48\u5b9e\u9645\u610f\u4e49\uff0c\u4ec5\u5411\u6211\u4eec\u5c55\u793a\u6a21\u62df\u952e\u76d8\u5404\u79cd\u6309\u952e\u4e0e\u7ec4\u5408\u952e\u7684\u7528\u6cd5\u3002<\/p>\n<ul>\n<li>from selenium.webdriver.common.keys import Keys<\/li>\n<\/ul>\n<p>\u5728\u4f7f\u7528\u952e\u76d8\u6309\u952e\u65b9\u6cd5\u524d\u9700\u8981\u5148\u5bfc\u5165 keys \u7c7b<\/p>\n<h2>\u5e38\u7528\u952e\u76d8\u64cd\u4f5c<\/h2>\n<p>\u4ee5\u4e0b\u4e3a\u5e38\u7528\u7684\u952e\u76d8\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li><code>send_keys(Keys.BACK_SPACE)<\/code> \u5220\u9664\u952e(BackSpace)<\/li>\n<li><code>send_keys(Keys.SPACE)<\/code> \u7a7a\u683c\u952e(Space)<\/li>\n<li><code>send_keys(Keys.TAB)<\/code> \u5236\u8868\u952e(Tab)<\/li>\n<li><code>send_keys(Keys.ESCAPE)<\/code> \u56de\u9000\u952e(Esc)<\/li>\n<li><code>send_keys(Keys.ENTER)<\/code> \u56de\u8f66\u952e(Enter)<\/li>\n<li><code>send_keys(Keys.CONTROL, 'a')<\/code> \u5168\u9009(Ctrl+A)<\/li>\n<li><code>send_keys(Keys.CONTROL, 'c')<\/code> \u590d\u5236(Ctrl+C)<\/li>\n<li><code>send_keys(Keys.CONTROL, 'x')<\/code> \u526a\u5207(Ctrl+X)<\/li>\n<li><code>send_keys(Keys.CONTROL, 'v')<\/code> \u7c98\u8d34(Ctrl+V)<\/li>\n<li><code>send_keys(Keys.F1)<\/code> \u952e\u76d8 F1<br \/>\n\u2026\u2026<\/li>\n<li><code>send_keys(Keys.F12)<\/code> \u952e\u76d8 F12<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Keys\u7c7b Keys\u7c7b\u63d0\u4f9b\u4e86\u952e\u76d8\u4e0a\u51e0\u4e4e\u6240\u6709\u6309\u952e\u7684\u65b9\u6cd5\u3002\u524d\u9762\u4e86\u89e3\u5230\uff0csend_keys()\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u6a21\u62df\u952e\u76d8\u8f93 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[221],"tags":[],"class_list":["post-755","post","type-post","status-publish","format-standard","hentry","category-selenium"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/755","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=755"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/755\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}