{"id":1833,"date":"2018-10-25T11:11:31","date_gmt":"2018-10-25T03:11:31","guid":{"rendered":"http:\/\/39.100.100.179\/?p=1833"},"modified":"2020-09-16T22:54:12","modified_gmt":"2020-09-16T14:54:12","slug":"python-%e7%88%ac%e8%99%ab%e5%9f%ba%e7%a1%80selenium%e5%ba%93%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"http:\/\/www.u3d8.com\/?p=1833","title":{"rendered":"Python \u722c\u866b\u57fa\u7840Selenium\u5e93\u7684\u4f7f\u7528"},"content":{"rendered":"<p>\u8f6c\u81ea\uff1ahttps:\/\/blog.csdn.net\/weixin_36279318\/article\/details\/79475388?utm_source=blogxgwz3<\/p>\n<h2>\uff08\u4e00\uff09Selenium\u57fa\u7840<\/h2>\n<p>\u5165\u95e8\u6559\u7a0b\uff1aSelenium\u5b98\u7f51\u6559\u7a0b<\/p>\n<h3>1.Selenium\u7b80\u4ecb<\/h3>\n<p>Selenium\u662f\u4e00\u4e2a\u7528\u4e8e\u6d4b\u8bd5\u7f51\u7ad9\u7684\u81ea\u52a8\u5316\u6d4b\u8bd5\u5de5\u5177\uff0c\u652f\u6301\u5404\u79cd\u6d4f\u89c8\u5668\u5305\u62ecChrome\u3001Firefox\u3001Safari\u7b49\u4e3b\u6d41\u754c\u9762\u6d4f\u89c8\u5668\uff0c\u540c\u65f6\u4e5f\u652f\u6301phantomJS\u65e0\u754c\u9762\u6d4f\u89c8\u5668\u3002<\/p>\n<h3>2.\u652f\u6301\u591a\u79cd\u64cd\u4f5c\u7cfb\u7edf<\/h3>\n<p>\u5982Windows\u3001Linux\u3001IOS\u3001Android\u7b49\u3002<\/p>\n<h3>3.\u5b89\u88c5Selenium<\/h3>\n<pre class=\"lang:default decode:true \">pip install Selenium<\/pre>\n<h3>4.\u5b89\u88c5\u6d4f\u89c8\u5668\u9a71\u52a8<\/h3>\n<p>Selenium3.x\u8c03\u7528\u6d4f\u89c8\u5668\u5fc5\u987b\u6709\u4e00\u4e2awebdriver\u9a71\u52a8\u6587\u4ef6<\/p>\n<p>Chrome\u9a71\u52a8\u6587\u4ef6\u4e0b\u8f7d\uff1a\u70b9\u51fb\u4e0b\u8f7dchromedrive<\/p>\n<p>Firefox\u9a71\u52a8\u6587\u4ef6\u4e0b\u8f7d:\u70b9\u89e3\u4e0b\u8f7dgeckodriver<\/p>\n<h3>5.\u914d\u7f6e\u73af\u5883\u53d8\u91cf<\/h3>\n<p>\u8bbe\u7f6e\u6d4f\u89c8\u5668\u7684\u5730\u5740\u975e\u5e38\u7b80\u5355\u3002 \u6211\u4eec\u53ef\u4ee5\u624b\u52a8\u521b\u5efa\u4e00\u4e2a\u5b58\u653e\u6d4f\u89c8\u5668\u9a71\u52a8\u7684\u76ee\u5f55\uff0c\u5982\uff1a F:\\GeckoDriver , \u5c06\u4e0b\u8f7d\u7684\u6d4f\u89c8\u5668\u9a71\u52a8\u6587\u4ef6\uff08\u4f8b\u5982\uff1achromedriver\u3001geckodriver\uff09\u4e22\u5230\u8be5\u76ee\u5f55\u4e0b\u3002<\/p>\n<p>\u6211\u7684\u7535\u8111\u2013&gt;\u5c5e\u6027\u2013&gt;\u7cfb\u7edf\u8bbe\u7f6e\u2013&gt;\u9ad8\u7ea7\u2013&gt;\u73af\u5883\u53d8\u91cf\u2013&gt;\u7cfb\u7edf\u53d8\u91cf\u2013&gt;Path\uff0c\u5c06\u201cF:\\GeckoDriver\u201d\u76ee\u5f55\u6dfb\u52a0\u5230Path\u7684\u503c\u4e2d\u3002\u6bd4\u5982\uff1aPath\u5b57\u6bb5;F:\\GeckoDriver<\/p>\n<p>\u53c2\u8003\u6d4f\u89c8\u5668\u9a71\u52a8\u73af\u5883\u914d\u7f6e<\/p>\n<h2>(\u4e8c)Selenium \u5feb\u901f\u5165\u95e8<\/h2>\n<p>\u5165\u95e8\u53c2\u8003\u6587\u732e\uff1aSelenium\u5165\u95e8<\/p>\n<h3>1.Selenium\u63d0\u4f9b\u4e868\u79cd\u5b9a\u4f4d\u65b9\u5f0f:<\/h3>\n<ul>\n<li>id<\/li>\n<li>name<\/li>\n<li>class name<\/li>\n<li>tag name<\/li>\n<li>link text<\/li>\n<li>partial link text<\/li>\n<li>xpath<\/li>\n<li>css selector<\/li>\n<\/ul>\n<h3>2.\u5b9a\u4f4d\u5143\u7d20\u76848\u79cd\u65b9\u5f0f<\/h3>\n<p>\u53c2\u8003\uff1aselenium\u5143\u7d20\u5b9a\u4f4d<\/p>\n<table width=\"786\">\n<tbody>\n<tr>\n<td style=\"font-weight: 400;\" width=\"284\"><span style=\"color: #333399;\">\u5b9a\u4f4d\u4e00\u4e2a\u5143\u7d20<\/span><\/td>\n<td style=\"font-weight: 400;\" width=\"294\"><span style=\"color: #333399;\">\u5b9a\u4f4d\u591a\u4e2a\u5143\u7d20<\/span><\/td>\n<td style=\"font-weight: 400;\" width=\"208\"><span style=\"color: #333399;\">\u542b\u4e49<\/span><\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"284\">find_element_by_id<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">find_elements_by_id<\/td>\n<td style=\"font-weight: 400;\" width=\"208\">\u901a\u8fc7\u5143\u7d20id\u5b9a\u4f4d<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"284\">find_element_by_name<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">find_elements_by_name<\/td>\n<td style=\"font-weight: 400;\" width=\"208\">\u901a\u8fc7\u5143\u7d20name\u5b9a\u4f4d<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"284\">find_element_by_xpath<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">find_elements_by_xpath<\/td>\n<td style=\"font-weight: 400;\" width=\"208\">\u901a\u8fc7xpath\u8868\u8fbe\u5f0f\u5b9a\u4f4d<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"284\">find_element_by_link_text<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">find_elements_by_link_tex<\/td>\n<td style=\"font-weight: 400;\" width=\"208\">\u901a\u8fc7\u5b8c\u6574\u8d85\u94fe\u63a5\u5b9a\u4f4d<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"284\">find_element_by_partial_link_text<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">find_elements_by_partial_link_text<\/td>\n<td style=\"font-weight: 400;\" width=\"208\">\u901a\u8fc7\u90e8\u5206\u94fe\u63a5\u5b9a\u4f4d<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"284\">find_element_by_tag_name<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">find_elements_by_tag_name<\/td>\n<td style=\"font-weight: 400;\" width=\"208\">\u901a\u8fc7\u6807\u7b7e\u5b9a\u4f4d<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"284\">find_element_by_class_name<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">find_elements_by_class_name<\/td>\n<td style=\"font-weight: 400;\" width=\"208\">\u901a\u8fc7\u7c7b\u540d\u8fdb\u884c\u5b9a\u4f4d<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"284\">find_elements_by_css_selector<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">find_elements_by_css_selector<\/td>\n<td style=\"font-weight: 400;\" width=\"208\">\u901a\u8fc7css\u9009\u62e9\u5668\u8fdb\u884c\u5b9a\u4f4d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>3.\u5b9e\u4f8b\u6f14\u793a<\/h3>\n<p>\u5047\u5982\u6211\u4eec\u6709\u4e00\u4e2aWeb\u9875\u9762\uff0c\u901a\u8fc7\u524d\u7aef\u5de5\u5177\uff08\u5982\uff0cFirebug\uff09\u67e5\u770b\u5230\u4e00\u4e2a\u5143\u7d20\u7684\u5c5e\u6027\u662f\u8fd9\u6837\u7684\u3002<\/p>\n<pre class=\"lang:default decode:true \">&lt;html&gt;\n  &lt;head&gt;\n  &lt;body link=\"#0000cc\"&gt;\n    &lt;a id=\"result_logo\" href=\"\/\" onmousedown=\"return c({'fm':'tab','tab':'logo'})\"&gt;\n    &lt;form id=\"form\" class=\"fm\" name=\"f\" action=\"\/s\"&gt;\n      &lt;span class=\"soutu-btn\"&gt;&lt;\/span&gt;\n        &lt;input id=\"kw\" class=\"s_ipt\" name=\"wd\" value=\"\" maxlength=\"255\" autocomplete=\"off\"&gt;<\/pre>\n<ul>\n<li>\u901a\u8fc7id\u5b9a\u4f4d:<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">dr.find_element_by_id(\"kw\")<\/pre>\n<ul>\n<li>\u901a\u8fc7name\u5b9a\u4f4d:<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">dr.find_element_by_name(\"wd\")<\/pre>\n<ul>\n<li>\u901a\u8fc7class name\u5b9a\u4f4d:<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">dr.find_element_by_class_name(\"s_ipt\")<\/pre>\n<ul>\n<li>\u901a\u8fc7tag name\u5b9a\u4f4d:<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">dr.find_element_by_tag_name(\"input\")<\/pre>\n<ul>\n<li>\u901a\u8fc7xpath\u5b9a\u4f4d\uff0cxpath\u5b9a\u4f4d\u6709N\u79cd\u5199\u6cd5\uff0c\u8fd9\u91cc\u5217\u51e0\u4e2a\u5e38\u7528\u5199\u6cd5:<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">dr.find_element_by_xpath(\"\/\/*[@id='kw']\")\ndr.find_element_by_xpath(\"\/\/*[@name='wd']\")\ndr.find_element_by_xpath(\"\/\/input[@class='s_ipt']\")\ndr.find_element_by_xpath(\"\/html\/body\/form\/span\/input\")\ndr.find_element_by_xpath(\"\/\/span[@class='soutu-btn']\/input\")\ndr.find_element_by_xpath(\"\/\/form[@id='form']\/span\/input\")\ndr.find_element_by_xpath(\"\/\/input[@id='kw' and @name='wd']\")<\/pre>\n<ul>\n<li>\u901a\u8fc7css\u5b9a\u4f4d\uff0ccss\u5b9a\u4f4d\u6709N\u79cd\u5199\u6cd5\uff0c\u8fd9\u91cc\u5217\u51e0\u4e2a\u5e38\u7528\u5199\u6cd5:<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">dr.find_element_by_css_selector(\"#kw\")\ndr.find_element_by_css_selector(\"[name=wd]\")\ndr.find_element_by_css_selector(\".s_ipt\")\ndr.find_element_by_css_selector(\"html &gt; body &gt; form &gt; span &gt; input\")\ndr.find_element_by_css_selector(\"span.soutu-btn&gt; input#kw\")\ndr.find_element_by_css_selector(\"form#form &gt; span &gt; input\")<\/pre>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u7684\u9875\u9762\u4e0a\u6709\u4e00\u7ec4\u6587\u672c\u94fe\u63a5\u3002<\/p>\n<pre class=\"lang:default decode:true \">&lt;a class=\"mnav\" href=\"http:\/\/news.baidu.com\" name=\"tj_trnews\"&gt;\u65b0\u95fb&lt;\/a&gt;\n&lt;a class=\"mnav\" href=\"http:\/\/www.hao123.com\" name=\"tj_trhao123\"&gt;hao123&lt;\/a&gt;<\/pre>\n<ul>\n<li>\u901a\u8fc7link text\u5b9a\u4f4d:<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">dr.find_element_by_link_text(\"\u65b0\u95fb\")\ndr.find_element_by_link_text(\"hao123\")<\/pre>\n<ul>\n<li>\u901a\u8fc7partial link text\u5b9a\u4f4d:<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">dr.find_element_by_partial_link_text(\"\u65b0\")\ndr.find_element_by_partial_link_text(\"hao\")\ndr.find_element_by_partial_link_text(\"123\")<\/pre>\n<h2>4.Selenium\u5e93\u4e0bwebdriver\u6a21\u5757\u5e38\u7528\u65b9\u6cd5\u7684\u4f7f\u7528<\/h2>\n<h3>1.\u63a7\u5236\u6d4f\u89c8\u5668\u64cd\u4f5c\u7684\u4e00\u4e9b\u65b9\u6cd5<\/h3>\n<table width=\"545\">\n<tbody>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">\u65b9\u6cd5<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u8bf4\u660e<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">set_window_size()<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u8bbe\u7f6e\u6d4f\u89c8\u5668\u7684\u5927\u5c0f<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">back()<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u63a7\u5236\u6d4f\u89c8\u5668\u540e\u9000<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">forward()<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u63a7\u5236\u6d4f\u89c8\u5668\u524d\u8fdb<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">refresh()<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u5237\u65b0\u5f53\u524d\u9875\u9762<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">clear()<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u6e05\u9664\u6587\u672c<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">send_keys&nbsp;(value)<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u6a21\u62df\u6309\u952e\u8f93\u5165<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">click()<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u5355\u51fb\u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">submit()<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u7528\u4e8e\u63d0\u4ea4\u8868\u5355<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">get_attribute(name)<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u83b7\u53d6\u5143\u7d20\u5c5e\u6027\u503c<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">is_displayed()<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u8bbe\u7f6e\u8be5\u5143\u7d20\u662f\u5426\u7528\u6237\u53ef\u89c1<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">size<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u8fd4\u56de\u5143\u7d20\u7684\u5c3a\u5bf8<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">text<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u83b7\u53d6\u5143\u7d20\u7684\u6587\u672c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5b9e\u4f8b\u6f14\u793a<\/p>\n<pre class=\"lang:default decode:true \">from selenium import webdriver\n\nfrom time import sleep\n#1.\u521b\u5efaChrome\u6d4f\u89c8\u5668\u5bf9\u8c61\uff0c\u8fd9\u4f1a\u5728\u7535\u8111\u4e0a\u5728\u6253\u5f00\u4e00\u4e2a\u6d4f\u89c8\u5668\u7a97\u53e3\nbrowser = webdriver.Firefox(executable_path =\"F:\\GeckoDriver\\geckodriver\")\n\n#2.\u901a\u8fc7\u6d4f\u89c8\u5668\u5411\u670d\u52a1\u5668\u53d1\u9001URL\u8bf7\u6c42\nbrowser.get(\"https:\/\/www.baidu.com\/\")\n\nsleep(3)\n\n#3.\u5237\u65b0\u6d4f\u89c8\u5668\nbrowser.refresh()\n\n#4.\u8bbe\u7f6e\u6d4f\u89c8\u5668\u7684\u5927\u5c0f\nbrowser.set_window_size(1400,800)\n\n#5.\u8bbe\u7f6e\u94fe\u63a5\u5185\u5bb9\nelement=browser.find_element_by_link_text(\"\u65b0\u95fb\")\nelement.click()\n\nelement=browser.find_element_by_link_text(\"\u4e60\u8fd1\u5e73\u7684\u201c\u4e0b\u56e2\u7ec4\u201d\u65f6\u95f4\")\nelement.click()<\/pre>\n<h3>2.\u9f20\u6807\u4e8b\u4ef6<\/h3>\n<p>\u5728 WebDriver \u4e2d\uff0c \u5c06\u8fd9\u4e9b\u5173\u4e8e\u9f20\u6807\u64cd\u4f5c\u7684\u65b9\u6cd5\u5c01\u88c5\u5728 ActionChains \u7c7b\u63d0\u4f9b\u3002<\/p>\n<table width=\"545\">\n<tbody>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">\u65b9\u6cd5<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u8bf4\u660e<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">ActionChains(driver)<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u6784\u9020ActionChains\u5bf9\u8c61<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">context_click()<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u6267\u884c\u9f20\u6807\u60ac\u505c\u64cd\u4f5c<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">move_to_element(above)<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u53f3\u51fb<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">double_click()<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u53cc\u51fb<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">drag_and_drop()<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u62d6\u52a8<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">move_to_element(above)<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u6267\u884c\u9f20\u6807\u60ac\u505c\u64cd\u4f5c<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">context_click()<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u7528\u4e8e\u6a21\u62df\u9f20\u6807\u53f3\u952e\u64cd\u4f5c\uff0c&nbsp;\u5728\u8c03\u7528\u65f6\u9700\u8981\u6307\u5b9a\u5143\u7d20\u5b9a\u4f4d<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">perform()<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u6267\u884c\u6240\u6709&nbsp;ActionChains&nbsp;\u4e2d\u5b58\u50a8\u7684\u884c\u4e3a\uff0c\u53ef\u4ee5\u7406\u89e3\u6210\u662f\u5bf9\u6574\u4e2a\u64cd\u4f5c\u7684\u63d0\u4ea4\u52a8\u4f5c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5b9e\u4f8b\u6f14\u793a<\/p>\n<p><img title=\"Python \u722c\u866b\u57fa\u7840Selenium\u5e93\u7684\u4f7f\u7528 - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" alt=\"Python \u722c\u866b\u57fa\u7840Selenium\u5e93\u7684\u4f7f\u7528 - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" loading=\"lazy\" class=\"size-full wp-image-1838 alignnone\" src=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2018\/10\/20180308120859311.jpg\" width=\"1030\" height=\"356\" srcset=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2018\/10\/20180308120859311.jpg 1030w, http:\/\/www.u3d8.com\/wp-content\/uploads\/2018\/10\/20180308120859311-300x104.jpg 300w, http:\/\/www.u3d8.com\/wp-content\/uploads\/2018\/10\/20180308120859311-768x265.jpg 768w, http:\/\/www.u3d8.com\/wp-content\/uploads\/2018\/10\/20180308120859311-1024x354.jpg 1024w\" sizes=\"(max-width: 1030px) 100vw, 1030px\" \/><\/p>\n<pre class=\"lang:default decode:true \">from selenium import webdriver\n#1.\u5f15\u5165 ActionChains \u7c7b\nfrom selenium.webdriver.common.action_chains import ActionChains\n\ndriver= webdriver.Chrome(\"F:\\Chrome\\ChromeDriver\\chromedriver\")\n\ndriver.get(\"https:\/\/www.baidu.cn\")\n\n#2.\u5b9a\u4f4d\u5230\u8981\u60ac\u505c\u7684\u5143\u7d20\nelement= driver.find_element_by_link_text(\"\u8bbe\u7f6e\")\n\n#3.\u5bf9\u5b9a\u4f4d\u5230\u7684\u5143\u7d20\u6267\u884c\u9f20\u6807\u60ac\u505c\u64cd\u4f5c\nActionChains(driver).move_to_element(element).perform()<\/pre>\n<h3>3.\u952e\u76d8\u4e8b\u4ef6<\/h3>\n<p>Selenium\u4e2d\u7684Key\u6a21\u5757\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u6a21\u62df\u952e\u76d8\u6309\u952e\u7684\u65b9\u6cd5\uff0c\u90a3\u5c31\u662fsend_keys()\u65b9\u6cd5\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u6a21\u62df\u952e\u76d8\u8f93\u5165\uff0c\u4e5f\u53ef\u4ee5\u6a21\u62df\u952e\u76d8\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u5e38\u7528\u7684\u952e\u76d8\u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n<table width=\"545\">\n<tbody>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">\u6a21\u62df\u952e\u76d8\u6309\u952e<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u8bf4\u660e<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">send_keys(Keys.BACK_SPACE)<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u5220\u9664\u952e\uff08BackSpace\uff09<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">send_keys(Keys.SPACE)<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u7a7a\u683c\u952e(Space)<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">send_keys(Keys.TAB)<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u5236\u8868\u952e(Tab)<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">send_keys(Keys.ESCAPE)<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u56de\u9000\u952e\uff08Esc\uff09<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">send_keys(Keys.ENTER)<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u56de\u8f66\u952e\uff08Enter\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7ec4\u5408\u952e\u7684\u4f7f\u7528<\/p>\n<table width=\"545\">\n<tbody>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">\u6a21\u62df\u952e\u76d8\u6309\u952e<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u8bf4\u660e<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">send_keys(Keys.CONTROL,\u2018a\u2019)<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u5168\u9009\uff08Ctrl+A\uff09<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">send_keys(Keys.CONTROL,\u2018c\u2019)<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u590d\u5236\uff08Ctrl+C\uff09<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">send_keys(Keys.CONTROL,\u2018x\u2019)<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u526a\u5207\uff08Ctrl+X\uff09<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">send_keys(Keys.CONTROL,\u2018v\u2019)<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u7c98\u8d34\uff08Ctrl+V\uff09<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">send_keys(Keys.F1\u2026Fn)<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u952e\u76d8&nbsp;F1\u2026Fn<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>4.\u83b7\u53d6\u65ad\u8a00\u4fe1\u606f<\/h3>\n<p>\u4e0d\u7ba1\u662f\u5728\u505a\u529f\u80fd\u6d4b\u8bd5\u8fd8\u662f\u81ea\u52a8\u5316\u6d4b\u8bd5\uff0c\u6700\u540e\u4e00\u6b65\u9700\u8981\u62ff\u5b9e\u9645\u7ed3\u679c\u4e0e\u9884\u671f\u8fdb\u884c\u6bd4\u8f83\u3002\u8fd9\u4e2a\u6bd4\u8f83\u7684\u79f0\u4e4b\u4e3a\u65ad\u8a00\u3002\u901a\u8fc7\u6211\u4eec\u83b7\u53d6title \u3001URL\u548ctext\u7b49\u4fe1\u606f\u8fdb\u884c\u65ad\u8a00\u3002<\/p>\n<table width=\"545\">\n<tbody>\n<tr>\n<td width=\"251\">\u5c5e\u6027<\/td>\n<td width=\"294\">\u8bf4\u660e<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">title<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u7528\u4e8e\u83b7\u5f97\u5f53\u524d\u9875\u9762\u7684\u6807\u9898<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">current_url<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u7528\u6237\u83b7\u5f97\u5f53\u524d\u9875\u9762\u7684URL<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">text<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u83b7\u53d6\u641c\u7d22\u6761\u76ee\u7684\u6587\u672c\u4fe1\u606f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5b9e\u4f8b\u6f14\u793a<\/p>\n<pre class=\"lang:default decode:true \">from selenium import webdriver\nfrom time import sleep\n\ndriver = webdriver.Firefox(executable_path =\"F:\\GeckoDriver\\geckodriver\")\ndriver.get(\"https:\/\/www.baidu.com\")\n\nprint('Before search================')\n\n# \u6253\u5370\u5f53\u524d\u9875\u9762title\ntitle = driver.title\nprint(title)\n\n# \u6253\u5370\u5f53\u524d\u9875\u9762URL\nnow_url = driver.current_url\nprint(now_url)\n\ndriver.find_element_by_id(\"kw\").send_keys(\"selenium\")\ndriver.find_element_by_id(\"su\").click()\nsleep(1)\n\nprint('After search================')\n\n# \u518d\u6b21\u6253\u5370\u5f53\u524d\u9875\u9762title\ntitle = driver.title\nprint(title)\n\n# \u6253\u5370\u5f53\u524d\u9875\u9762URL\nnow_url = driver.current_url\nprint(now_url)\n\n# \u83b7\u53d6\u7ed3\u679c\u6570\u76ee\nuser = driver.find_element_by_class_name('nums').text\nprint(user)\n\n#\u5173\u95ed\u6240\u6709\u7a97\u53e3\ndriver.quit()<\/pre>\n<p>\u6253\u5370\u8f93\u51fa\u7ed3\u679c<\/p>\n<pre class=\"lang:default decode:true \">Before search================\n\u767e\u5ea6\u4e00\u4e0b\uff0c\u4f60\u5c31\u77e5\u9053\nhttps:\/\/www.baidu.com\/\nAfter search================\nselenium_\u767e\u5ea6\u641c\u7d22\nhttps:\/\/www.baidu.com\/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu&wd=selenium&rsv_pq=a1d51b980000e36e&rsv_t=a715IZaMpLd1w92I4LNUi7gKuOdlAz5McsHe%2FSLQeBZD44OUIPnjY%2B7pODM&rqlang=cn&rsv_enter=0&rsv_sug3=8&inputT=758&rsv_sug4=759\n\u641c\u7d22\u5de5\u5177\n\u767e\u5ea6\u4e3a\u60a8\u627e\u5230\u76f8\u5173\u7ed3\u679c\u7ea67,170,000\u4e2a<\/pre>\n<h3>5.\u8bbe\u7f6e\u5143\u7d20\u7b49\u5f85:<a href=\"http:\/\/www.testclass.net\/selenium_python\/element-wait\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">\u53c2\u8003\u6587\u732e<\/a><\/h3>\n<h3>6.\u5b9a\u4f4d\u4e00\u7ec4\u5143\u7d20<\/h3>\n<p>\u5b9a\u4f4d\u4e00\u7ec4\u5143\u7d20\u7684\u65b9\u6cd5\u4e0e\u5b9a\u4f4d\u5355\u4e2a\u5143\u7d20\u7684\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u552f\u4e00\u7684\u533a\u522b\u662f\u5728\u5355\u8bcdelement\u540e\u9762\u591a\u4e86\u4e00\u4e2as\u8868\u793a\u590d\u6570\u3002<\/p>\n<p>\u5b9e\u4f8b\u6f14\u793a<\/p>\n<pre class=\"lang:default decode:true \">from selenium import webdriver\nfrom time import sleep\n\ndriver =webdriver.Firefox(executable_path =\"F:\\GeckoDriver\\geckodriver\")\ndriver.get(\"https:\/\/www.baidu.com\")\n\ndriver.find_element_by_id(\"kw\").send_keys(\"selenium\")\ndriver.find_element_by_id(\"su\").click()\nsleep(1)\n\n#1.\u5b9a\u4f4d\u4e00\u7ec4\u5143\u7d20\nelements = driver.find_elements_by_xpath('\/\/div\/h3\/a')\nprint(type(elements))\n\n#2.\u5faa\u73af\u904d\u5386\u51fa\u6bcf\u4e00\u6761\u641c\u7d22\u7ed3\u679c\u7684\u6807\u9898\nfor t in elements:\n    print(t.text)\n    element=driver.find_element_by_link_text(t.text)\n    element.click()\n    sleep(3)\n\ndriver.quit()<\/pre>\n<p>7.\u591a\u8868\u5355\u5207\u6362<\/p>\n<p>\u5728Web\u5e94\u7528\u4e2d\u7ecf\u5e38\u4f1a\u9047\u5230frame\/iframe\u8868\u5355\u5d4c\u5957\u9875\u9762\u7684\u5e94\u7528\uff0cWebDriver\u53ea\u80fd\u5728\u4e00\u4e2a\u9875\u9762\u4e0a\u5bf9\u5143\u7d20\u8bc6\u522b\u4e0e\u5b9a\u4f4d\uff0c\u5bf9\u4e8eframe\/iframe\u8868\u5355\u5185\u5d4c\u9875\u9762\u4e0a\u7684\u5143\u7d20\u65e0\u6cd5\u76f4\u63a5\u5b9a\u4f4d\u3002<\/p>\n<p>\u8fd9\u65f6\u5c31\u9700\u8981\u901a\u8fc7switch_to.frame()\u65b9\u6cd5\u5c06\u5f53\u524d\u5b9a\u4f4d\u7684\u4e3b\u4f53\u5207\u6362\u4e3aframe\/iframe\u8868\u5355\u7684\u5185\u5d4c\u9875\u9762\u4e2d\u3002<\/p>\n<table width=\"545\">\n<tbody>\n<tr>\n<td width=\"251\">\u65b9\u6cd5<\/td>\n<td width=\"294\">\u8bf4\u660e<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">switch_to.frame()<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u5c06\u5f53\u524d\u5b9a\u4f4d\u7684\u4e3b\u4f53\u5207\u6362\u4e3aframe\/iframe\u8868\u5355\u7684\u5185\u5d4c\u9875\u9762\u4e2d<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">switch_to.default_content()<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u8df3\u56de\u6700\u5916\u5c42\u7684\u9875\u9762<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"lang:default decode:true \">&lt;html&gt;\n  &lt;body&gt;\n    ...\n    &lt;iframe id=\"x-URS-iframe\" ...&gt;\n      &lt;html&gt;\n         &lt;body&gt;\n           ...\n           &lt;input name=\"email\" &gt;<\/pre>\n<p>126\u90ae\u7bb1\u767b\u5f55\u6846\u7684\u7ed3\u6784\u5927\u6982\u662f\u8fd9\u6837\u5b50\u7684\uff0c\u60f3\u8981\u64cd\u4f5c\u767b\u5f55\u6846\u5fc5\u987b\u8981\u5148\u5207\u6362\u5230iframe\u8868\u5355\u3002<\/p>\n<pre class=\"lang:default decode:true \">from selenium import webdriver\n\ndriver = webdriver.Chrome()\ndriver.get(\"http:\/\/www.126.com\")\n\ndriver.switch_to.frame('x-URS-iframe')\ndriver.find_element_by_name(\"email\").clear()\ndriver.find_element_by_name(\"email\").send_keys(\"username\")\ndriver.find_element_by_name(\"password\").clear()\ndriver.find_element_by_name(\"password\").send_keys(\"password\")\ndriver.find_element_by_id(\"dologin\").click()\ndriver.switch_to.default_content()\n\ndriver.quit()<\/pre>\n<p>switch_to.frame() \u9ed8\u8ba4\u53ef\u4ee5\u76f4\u63a5\u53d6\u8868\u5355\u7684id \u6216name\u5c5e\u6027\u3002\u5982\u679ciframe\u6ca1\u6709\u53ef\u7528\u7684id\u548cname\u5c5e\u6027\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u65b9\u5f0f\u8fdb\u884c\u5b9a\u4f4d\u3002<\/p>\n<pre class=\"lang:default decode:true \">\u2026\u2026\n#\u5148\u901a\u8fc7xpth\u5b9a\u4f4d\u5230iframe\nxf = driver.find_element_by_xpath('\/\/*[@id=\"x-URS-iframe\"]')\n\n#\u518d\u5c06\u5b9a\u4f4d\u5bf9\u8c61\u4f20\u7ed9switch_to.frame()\u65b9\u6cd5\ndriver.switch_to.frame(xf)\n\u2026\u2026\ndriver.switch_to.parent_frame()<\/pre>\n<h3>8.\u591a\u7a97\u53e3\u5207\u6362<\/h3>\n<p>\u5728\u9875\u9762\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\u6709\u65f6\u5019\u70b9\u51fb\u67d0\u4e2a\u94fe\u63a5\u4f1a\u5f39\u51fa\u65b0\u7684\u7a97\u53e3\uff0c\u8fd9\u65f6\u5c31\u9700\u8981\u4e3b\u673a\u5207\u6362\u5230\u65b0\u6253\u5f00\u7684\u7a97\u53e3\u4e0a\u8fdb\u884c\u64cd\u4f5c\u3002WebDriver\u63d0\u4f9b\u4e86switch_to.window()\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5b9e\u73b0\u5728\u4e0d\u540c\u7684\u7a97\u53e3\u4e4b\u95f4\u5207\u6362\u3002<\/p>\n<table width=\"545\">\n<tbody>\n<tr>\n<td width=\"251\">\u65b9\u6cd5<\/td>\n<td width=\"294\">\u8bf4\u660e<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">current_window_handle<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u83b7\u5f97\u5f53\u524d\u7a97\u53e3\u53e5\u67c4<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">window_handles<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u8fd4\u56de\u6240\u6709\u7a97\u53e3\u7684\u53e5\u67c4\u5230\u5f53\u524d\u4f1a\u8bdd<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">switch_to.window()<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u7528\u4e8e\u5207\u6362\u5230\u76f8\u5e94\u7684\u7a97\u53e3\uff0c\u4e0e\u4e0a\u4e00\u8282\u7684switch_to.frame()\u7c7b\u4f3c\uff0c\u524d\u8005\u7528\u4e8e\u4e0d\u540c\u7a97\u53e3\u7684\u5207\u6362\uff0c\u540e\u8005\u7528\u4e8e\u4e0d\u540c\u8868\u5355\u4e4b\u95f4\u7684\u5207\u6362\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5b9e\u4f8b\u6f14\u793a<\/p>\n<pre class=\"lang:default decode:true \">from selenium import webdriver\nimport time\ndriver = webdriver.Chrome(\"F:\\Chrome\\ChromeDriver\\chromedriver\")\ndriver.implicitly_wait(10)\ndriver.get(\"http:\/\/www.baidu.com\")\n\n#1.\u83b7\u5f97\u767e\u5ea6\u641c\u7d22\u7a97\u53e3\u53e5\u67c4\nsreach_windows = driver.current_window_handle\n\ndriver.find_element_by_link_text('\u767b\u5f55').click()\ndriver.find_element_by_link_text(\"\u7acb\u5373\u6ce8\u518c\").click()\n\n#1.\u83b7\u5f97\u5f53\u524d\u6240\u6709\u6253\u5f00\u7684\u7a97\u53e3\u7684\u53e5\u67c4\nall_handles = driver.window_handles\n\n#3.\u8fdb\u5165\u6ce8\u518c\u7a97\u53e3\nfor handle in all_handles:\n    if handle != sreach_windows:\n        driver.switch_to.window(handle)\n        print('\u8df3\u8f6c\u5230\u6ce8\u518c\u7a97\u53e3')\n        driver.find_element_by_name(\"account\").send_keys('123456789')\n        driver.find_element_by_name('password').send_keys('123456789')\n        time.sleep(2)\n    \ndriver.quit()<\/pre>\n<h3>9.\u8b66\u544a\u6846\u5904\u7406<\/h3>\n<p>\u5728WebDriver\u4e2d\u5904\u7406JavaScript\u6240\u751f\u6210\u7684alert\u3001confirm\u4ee5\u53caprompt\u5341\u5206\u7b80\u5355\uff0c\u5177\u4f53\u505a\u6cd5\u662f\u4f7f\u7528 switch_to.alert \u65b9\u6cd5\u5b9a\u4f4d\u5230 alert\/confirm\/prompt<\/p>\n<p>\u7136\u540e\u4f7f\u7528text\/accept\/dismiss\/ send_keys\u7b49\u65b9\u6cd5\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<table width=\"545\">\n<tbody>\n<tr>\n<td width=\"251\">\u65b9\u6cd5<\/td>\n<td width=\"294\">\u8bf4\u660e<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">text<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u8fd4\u56de&nbsp;alert\/confirm\/prompt&nbsp;\u4e2d\u7684\u6587\u5b57\u4fe1\u606f<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">accept()<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u63a5\u53d7\u73b0\u6709\u8b66\u544a\u6846<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">dismiss()<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u89e3\u6563\u73b0\u6709\u8b66\u544a\u6846<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">send_keys(keysToSend)<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u53d1\u9001\u6587\u672c\u81f3\u8b66\u544a\u6846\u3002keysToSend\uff1a\u5c06\u6587\u672c\u53d1\u9001\u81f3\u8b66\u544a\u6846\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5b9e\u4f8b\u6f14\u793a<\/p>\n<pre class=\"lang:default decode:true \">from selenium import webdriver\nfrom selenium.webdriver.common.action_chains import ActionChains\nimport time\n\ndriver = webdriver.Chrome(\"F:\\Chrome\\ChromeDriver\\chromedriver\")\ndriver.implicitly_wait(10)\ndriver.get('http:\/\/www.baidu.com')\n\n# \u9f20\u6807\u60ac\u505c\u81f3\u201c\u8bbe\u7f6e\u201d\u94fe\u63a5\nlink = driver.find_element_by_link_text('\u8bbe\u7f6e')\nActionChains(driver).move_to_element(link).perform()\n\n# \u6253\u5f00\u641c\u7d22\u8bbe\u7f6e\ndriver.find_element_by_link_text(\"\u641c\u7d22\u8bbe\u7f6e\").click()\n\n#\u5728\u6b64\u5904\u8bbe\u7f6e\u7b49\u5f852s\u5426\u5219\u53ef\u80fd\u62a5\u9519\ntime.sleep(2)\n# \u4fdd\u5b58\u8bbe\u7f6e\ndriver.find_element_by_class_name(\"prefpanelgo\").click()\ntime.sleep(2)\n\n# \u63a5\u53d7\u8b66\u544a\u6846\ndriver.switch_to.alert.accept()\n\ndriver.quit()<\/pre>\n<h3>10.\u4e0b\u62c9\u6846\u9009\u62e9\u64cd\u4f5c<\/h3>\n<p>\u5bfc\u5165\u9009\u62e9\u4e0b\u62c9\u6846Select\u7c7b\uff0c\u4f7f\u7528\u8be5\u7c7b\u5904\u7406\u4e0b\u62c9\u6846\u64cd\u4f5c\u3002<\/p>\n<pre class=\"lang:default decode:true \">from selenium.webdriver.support.select import Select<\/pre>\n<p>Select\u7c7b\u7684\u65b9\u6cd5<\/p>\n<table width=\"545\">\n<tbody>\n<tr>\n<td width=\"251\">\u65b9\u6cd5<\/td>\n<td width=\"294\">\u8bf4\u660e<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">select_by_value(\u201c\u9009\u62e9\u503c\u201d)<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u76f8\u5f53\u4e8e\u6211\u4eec\u4f7f\u7528\u9f20\u6807\u9009\u62e9\u4e0b\u62c9\u6846\u7684\u503c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6709\u65f6\u6211\u4eec\u4f1a\u78b0\u5230\u4e0b\u62c9\u6846\uff0cWebDriver\u63d0\u4f9b\u4e86Select\u7c7b\u6765\u5904\u7406\u4e0b\u62c9\u6846\u3002 \u5982\u767e\u5ea6\u641c\u7d22\u8bbe\u7f6e\u7684\u4e0b\u62c9\u6846\uff0c\u5982\u4e0b\u56fe\uff1a<\/p>\n<p><img title=\"Python \u722c\u866b\u57fa\u7840Selenium\u5e93\u7684\u4f7f\u7528 - \u7b2c2\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" alt=\"Python \u722c\u866b\u57fa\u7840Selenium\u5e93\u7684\u4f7f\u7528 - \u7b2c2\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" loading=\"lazy\" class=\"size-full wp-image-1839 alignnone\" src=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2018\/10\/select.png\" width=\"1065\" height=\"459\" srcset=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2018\/10\/select.png 1065w, http:\/\/www.u3d8.com\/wp-content\/uploads\/2018\/10\/select-300x129.png 300w, http:\/\/www.u3d8.com\/wp-content\/uploads\/2018\/10\/select-768x331.png 768w, http:\/\/www.u3d8.com\/wp-content\/uploads\/2018\/10\/select-1024x441.png 1024w\" sizes=\"(max-width: 1065px) 100vw, 1065px\" \/><\/p>\n<pre class=\"lang:default decode:true \">from selenium import webdriver\nfrom selenium.webdriver.support.select import Select\nfrom time import sleep\n\ndriver = webdriver.Chrome(\"F:\\Chrome\\ChromeDriver\\chromedriver\")\ndriver.implicitly_wait(10)\ndriver.get('http:\/\/www.baidu.com')\n\n#1.\u9f20\u6807\u60ac\u505c\u81f3\u201c\u8bbe\u7f6e\u201d\u94fe\u63a5\ndriver.find_element_by_link_text('\u8bbe\u7f6e').click()\nsleep(1)\n#2.\u6253\u5f00\u641c\u7d22\u8bbe\u7f6e\ndriver.find_element_by_link_text(\"\u641c\u7d22\u8bbe\u7f6e\").click()\nsleep(2)\n\n#3.\u641c\u7d22\u7ed3\u679c\u663e\u793a\u6761\u6570\nsel = driver.find_element_by_xpath(\"\/\/select[@id='nr']\")\nSelect(sel).select_by_value('50')  # \u663e\u793a50\u6761\n\nsleep(3)\ndriver.quit()<\/pre>\n<h3>11.\u6587\u4ef6\u4e0a\u4f20<\/h3>\n<p>\u5bf9\u4e8e\u901a\u8fc7input\u6807\u7b7e\u5b9e\u73b0\u7684\u4e0a\u4f20\u529f\u80fd\uff0c\u53ef\u4ee5\u5c06\u5176\u770b\u4f5c\u662f\u4e00\u4e2a\u8f93\u5165\u6846\uff0c\u5373\u901a\u8fc7send_keys()\u6307\u5b9a\u672c\u5730\u6587\u4ef6\u8def\u5f84\u7684\u65b9\u5f0f\u5b9e\u73b0\u6587\u4ef6\u4e0a\u4f20\u3002<\/p>\n<p>\u901a\u8fc7send_keys()\u65b9\u6cd5\u6765\u5b9e\u73b0\u6587\u4ef6\u4e0a\u4f20:<\/p>\n<pre class=\"lang:default decode:true \">from selenium import webdriver\nimport os\n\ndriver = webdriver.Firefox()\nfile_path = 'file:\/\/\/' + os.path.abspath('upfile.html')\ndriver.get(file_path)\n\n# \u5b9a\u4f4d\u4e0a\u4f20\u6309\u94ae\uff0c\u6dfb\u52a0\u672c\u5730\u6587\u4ef6\ndriver.find_element_by_name(\"file\").send_keys('D:\\\\upload_file.txt')\n\ndriver.quit()<\/pre>\n<h3>12.cookie\u64cd\u4f5c<\/h3>\n<p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u9a8c\u8bc1\u6d4f\u89c8\u5668\u4e2dcookie\u662f\u5426\u6b63\u786e\uff0c\u56e0\u4e3a\u57fa\u4e8e\u771f\u5b9ecookie\u7684\u6d4b\u8bd5\u662f\u65e0\u6cd5\u901a\u8fc7\u767d\u76d2\u548c\u96c6\u6210\u6d4b\u8bd5\u8fdb\u884c\u7684\u3002WebDriver\u63d0\u4f9b\u4e86\u64cd\u4f5cCookie\u7684\u76f8\u5173\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8bfb\u53d6\u3001\u6dfb\u52a0\u548c\u5220\u9664cookie\u4fe1\u606f\u3002<\/p>\n<p>WebDriver\u64cd\u4f5ccookie\u7684\u65b9\u6cd5:<\/p>\n<table width=\"545\">\n<tbody>\n<tr>\n<td width=\"251\">\u65b9\u6cd5<\/td>\n<td width=\"294\">\u8bf4\u660e<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">get_cookies()<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u83b7\u5f97\u6240\u6709cookie\u4fe1\u606f<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">get_cookie(name)<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u8fd4\u56de\u5b57\u5178\u7684key\u4e3a\u201cname\u201d\u7684cookie\u4fe1\u606f<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">add_cookie(cookie_dict)<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u6dfb\u52a0cookie\u3002\u201ccookie_dict\u201d\u6307\u5b57\u5178\u5bf9\u8c61\uff0c\u5fc5\u987b\u6709name&nbsp;\u548cvalue&nbsp;\u503c<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">delete_cookie(name,optionsString)<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u5220\u9664cookie\u4fe1\u606f\u3002\u201cname\u201d\u662f\u8981\u5220\u9664\u7684cookie\u7684\u540d\u79f0\uff0c\u201coptionsString\u201d\u662f\u8be5cookie\u7684\u9009\u9879\uff0c\u76ee\u524d\u652f\u6301\u7684\u9009\u9879\u5305\u62ec\u201c\u8def\u5f84\u201d\uff0c\u201c\u57df\u201d<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">delete_all_cookies()<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u5220\u9664\u6240\u6709cookie\u4fe1\u606f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5b9e\u4f8b\u6f14\u793a<\/p>\n<pre class=\"lang:default decode:true \">from selenium import webdriver\nimport time\nbrowser = webdriver.Chrome(\"F:\\Chrome\\ChromeDriver\\chromedriver\")\nbrowser.get(\"http:\/\/www.youdao.com\")\n\n#1.\u6253\u5370cookie\u4fe1\u606f\nprint('=====================================')\nprint(\"\u6253\u5370cookie\u4fe1\u606f\u4e3a\uff1a\")\nprint(browser.get_cookies)\n\n#2.\u6dfb\u52a0cookie\u4fe1\u606f\ndict={'name':\"name\",'value':'Kaina'}\nbrowser.add_cookie(dict)\n\nprint('=====================================')\nprint('\u6dfb\u52a0cookie\u4fe1\u606f\u4e3a\uff1a')\n#3.\u904d\u5386\u6253\u5370cookie\u4fe1\u606f\nfor cookie in browser.get_cookies():\n    print('%s----%s\\n' %(cookie['name'],cookie['value']))\n    \n#4.\u5220\u9664\u4e00\u4e2acookie\nbrowser.delete_cookie('name')\nprint('=====================================')\nprint('\u5220\u9664\u4e00\u4e2acookie')\nfor cookie in browser.get_cookies():\n    print('%s----%s\\n' %(cookie['name'],cookie['value']))\n\nprint('=====================================')\nprint('\u5220\u9664\u6240\u6709cookie\u540e\uff1a')\n#5.\u5220\u9664\u6240\u6709cookie,\u65e0\u9700\u4f20\u9012\u53c2\u6570\nbrowser.delete_all_cookies()\nfor cookie in browser.get_cookies():\n    print('%s----%s\\n' %(cookie['name'],cookie['value']))\n\ntime.sleep(3)\nbrowser.close()<\/pre>\n<h3>13.\u8c03\u7528JavaScript\u4ee3\u7801<\/h3>\n<p>\u867d\u7136WebDriver\u63d0\u4f9b\u4e86\u64cd\u4f5c\u6d4f\u89c8\u5668\u7684\u524d\u8fdb\u548c\u540e\u9000\u65b9\u6cd5\uff0c\u4f46\u5bf9\u4e8e\u6d4f\u89c8\u5668\u6eda\u52a8\u6761\u5e76\u6ca1\u6709\u63d0\u4f9b\u76f8\u5e94\u7684\u64cd\u4f5c\u65b9\u6cd5\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5c31\u53ef\u4ee5\u501f\u52a9JavaScript\u6765\u63a7\u5236\u6d4f\u89c8\u5668\u7684\u6eda\u52a8\u6761\u3002<\/p>\n<p>WebDriver\u63d0\u4f9b\u4e86execute_script()\u65b9\u6cd5\u6765\u6267\u884cJavaScript\u4ee3\u7801\u3002<\/p>\n<p>\u7528\u4e8e\u8c03\u6574\u6d4f\u89c8\u5668\u6eda\u52a8\u6761\u4f4d\u7f6e\u7684JavaScript\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true \">&lt;!-- window.scrollTo(\u5de6\u8fb9\u8ddd,\u4e0a\u8fb9\u8ddd); --&gt;\nwindow.scrollTo(0,450);<\/pre>\n<p>window.scrollTo()\u65b9\u6cd5\u7528\u4e8e\u8bbe\u7f6e\u6d4f\u89c8\u5668\u7a97\u53e3\u6eda\u52a8\u6761\u7684\u6c34\u5e73\u548c\u5782\u76f4\u4f4d\u7f6e\u3002\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u8868\u793a\u6c34\u5e73\u7684\u5de6\u95f4\u8ddd\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8868\u793a\u5782\u76f4\u7684\u4e0a\u8fb9\u8ddd\u3002\u5176\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:default decode:true \">from selenium import webdriver\nfrom time import sleep\n\n#1.\u8bbf\u95ee\u767e\u5ea6\ndriver=webdriver.Firefox(executable_path =\"F:\\GeckoDriver\\geckodriver\")\ndriver.get(\"http:\/\/www.baidu.com\")\n\n#2.\u641c\u7d22\ndriver.find_element_by_id(\"kw\").send_keys(\"selenium\")\ndriver.find_element_by_id(\"su\").click()\n\n#3.\u4f11\u77202s\u76ee\u7684\u662f\u83b7\u5f97\u670d\u52a1\u5668\u7684\u54cd\u5e94\u5185\u5bb9\uff0c\u5982\u679c\u4e0d\u4f7f\u7528\u4f11\u7720\u53ef\u80fd\u62a5\u9519\nsleep(2)\n\n#4.\u901a\u8fc7javascript\u8bbe\u7f6e\u6d4f\u89c8\u5668\u7a97\u53e3\u7684\u6eda\u52a8\u6761\u4f4d\u7f6e\njs=\"window.scrollTo(100,450);\"\ndriver.execute_script(js)\nsleep(3)\n\ndriver.close()<\/pre>\n<p>\u901a\u8fc7\u6d4f\u89c8\u5668\u6253\u5f00\u767e\u5ea6\u8fdb\u884c\u641c\u7d22\uff0c\u5e76\u4e14\u63d0\u524d\u901a\u8fc7set_window_size()\u65b9\u6cd5\u5c06\u6d4f\u89c8\u5668\u7a97\u53e3\u8bbe\u7f6e\u4e3a\u56fa\u5b9a\u5bbd\u9ad8\u663e\u793a\uff0c\u76ee\u7684\u662f\u8ba9\u7a97\u53e3\u51fa\u73b0\u6c34\u5e73\u548c\u5782\u76f4\u6eda\u52a8\u6761\u3002<\/p>\n<p>\u7136\u540e\u901a\u8fc7execute_script()\u65b9\u6cd5\u6267\u884cJavaScripts\u4ee3\u7801\u6765\u79fb\u52a8\u6eda\u52a8\u6761\u7684\u4f4d\u7f6e\u3002<\/p>\n<h3>14.\u7a97\u53e3\u622a\u56fe<\/h3>\n<p>\u81ea\u52a8\u5316\u7528\u4f8b\u662f\u7531\u7a0b\u5e8f\u53bb\u6267\u884c\u7684\uff0c\u56e0\u6b64\u6709\u65f6\u5019\u6253\u5370\u7684\u9519\u8bef\u4fe1\u606f\u5e76\u4e0d\u5341\u5206\u660e\u786e\u3002\u5982\u679c\u5728\u811a\u672c\u6267\u884c\u51fa\u9519\u7684\u65f6\u5019\u80fd\u5bf9\u5f53\u524d\u7a97\u53e3\u622a\u56fe\u4fdd\u5b58\uff0c\u90a3\u4e48\u901a\u8fc7\u56fe\u7247\u5c31\u53ef\u4ee5\u975e\u5e38\u76f4\u89c2\u5730\u770b\u51fa\u51fa\u9519\u7684\u539f\u56e0\u3002<\/p>\n<p>WebDriver\u63d0\u4f9b\u4e86\u622a\u56fe\u51fd\u6570get_screenshot_as_file()\u6765\u622a\u53d6\u5f53\u524d\u7a97\u53e3\u3002<\/p>\n<p>\u622a\u5c4f\u65b9\u6cd5\uff1a<\/p>\n<table width=\"545\">\n<tbody>\n<tr>\n<td width=\"251\">\u65b9\u6cd5<\/td>\n<td width=\"294\">\u8bf4\u660e<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">get_screenshot_as_file(self,&nbsp;filename)<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u7528\u4e8e\u622a\u53d6\u5f53\u524d\u7a97\u53e3\uff0c\u5e76\u628a\u56fe\u7247\u4fdd\u5b58\u5230\u672c\u5730<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre class=\"lang:default decode:true \">from selenium import webdriver\nfrom time import sleep\n\ndriver =webdriver.Firefox(executable_path =\"F:\\GeckoDriver\\geckodriver\")\ndriver.get('http:\/\/www.baidu.com')\n\ndriver.find_element_by_id('kw').send_keys('selenium')\ndriver.find_element_by_id('su').click()\nsleep(2)\n\n#1.\u622a\u53d6\u5f53\u524d\u7a97\u53e3\uff0c\u5e76\u6307\u5b9a\u622a\u56fe\u56fe\u7247\u7684\u4fdd\u5b58\u4f4d\u7f6e\ndriver.get_screenshot_as_file(\"D:\\\\baidu_img.jpg\")\n\ndriver.quit()<\/pre>\n<h3>15.\u5173\u95ed\u6d4f\u89c8\u5668<\/h3>\n<p>\u5728\u524d\u9762\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u4e00\u76f4\u4f7f\u7528quit()\u65b9\u6cd5\uff0c\u5176\u542b\u4e49\u4e3a\u9000\u51fa\u76f8\u5173\u7684\u9a71\u52a8\u7a0b\u5e8f\u548c\u5173\u95ed\u6240\u6709\u7a97\u53e3\u3002\u9664\u6b64\u4e4b\u5916\uff0cWebDriver\u8fd8\u63d0\u4f9b\u4e86close()\u65b9\u6cd5\uff0c\u7528\u6765\u5173\u95ed\u5f53\u524d\u7a97\u53e3\u3002<\/p>\n<p>\u4f8b\u591a\u7a97\u53e3\u7684\u5904\u7406\uff0c\u5728\u7528\u4f8b\u6267\u884c\u7684\u8fc7\u7a0b\u4e2d\u6253\u5f00\u4e86\u591a\u4e2a\u7a97\u53e3\uff0c\u6211\u4eec\u60f3\u8981\u5173\u95ed\u5176\u4e2d\u7684\u67d0\u4e2a\u7a97\u53e3\uff0c\u8fd9\u65f6\u5c31\u8981\u7528\u5230close()\u65b9\u6cd5\u8fdb\u884c\u5173\u95ed\u4e86\u3002<\/p>\n<table width=\"545\">\n<tbody>\n<tr>\n<td width=\"251\">\u65b9\u6cd5<\/td>\n<td width=\"294\">\u8bf4\u660e<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">close()<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u5173\u95ed\u5355\u4e2a\u7a97\u53e3<\/td>\n<\/tr>\n<tr>\n<td style=\"font-weight: 400;\" width=\"251\">quit()<\/td>\n<td style=\"font-weight: 400;\" width=\"294\">\u5173\u95ed\u6240\u6709\u7a97\u53e3<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u81ea\uff1ahttps:\/\/blog.csdn.net\/weixin_36279318 &hellip; <a href=\"http:\/\/www.u3d8.com\/?p=1833\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1644,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[308],"tags":[378],"_links":{"self":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1833"}],"collection":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1833"}],"version-history":[{"count":7,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1833\/revisions"}],"predecessor-version":[{"id":2079,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1833\/revisions\/2079"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/media\/1644"}],"wp:attachment":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1833"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}