{"id":757,"date":"2023-02-26T16:08:43","date_gmt":"2023-02-26T08:08:43","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=757"},"modified":"2023-04-29T17:00:28","modified_gmt":"2023-04-29T09:00:28","slug":"python-selenium-set-element-waiting","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/python-selenium-set-element-waiting\/","title":{"rendered":"Python Selenium\u8bbe\u7f6e\u5143\u7d20\u7b49\u5f85"},"content":{"rendered":"<p>WebDriver\u63d0\u4f9b\u4e86\u4e24\u79cd\u7c7b\u578b\u7684\u7b49\u5f85\uff1a<strong>\u663e\u5f0f\u7b49\u5f85<\/strong>\u548c<strong>\u9690\u5f0f\u7b49\u5f85<\/strong>\u3002<\/p>\n<h2>\u663e\u5f0f\u7b49\u5f85<\/h2>\n<p>\u663e\u5f0f\u7b49\u5f85\u4f7fWebdDriver\u7b49\u5f85\u67d0\u4e2a\u6761\u4ef6\u6210\u7acb\u65f6\u7ee7\u7eed\u6267\u884c\uff0c\u5426\u5219\u5728\u8fbe\u5230\u6700\u5927\u65f6\u957f\u65f6\u629b\u51fa\u8d85\u65f6\u5f02\u5e38\uff08TimeoutException\uff09\u3002<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-python\">from selenium import webdriver\nfrom selenium.webdriver.common.by import By\nfrom selenium.webdriver.support.ui import WebDriverWait\nfrom selenium.webdriver.support import expected_conditions as EC\n\ndriver = webdriver.Firefox()\ndriver.get(&quot;http:\/\/www.baidu.com&quot;)\n\nelement = WebDriverWait(driver, 5, 0.5).until(\n                      EC.presence_of_element_located((By.ID, &quot;kw&quot;))\n                      )\nelement.send_keys(&#039;selenium&#039;)\ndriver.quit()<\/code><\/pre>\n<p>WebDriverWait\u7c7b\u662f\u7531WebDirver \u63d0\u4f9b\u7684\u7b49\u5f85\u65b9\u6cd5\u3002\u5728\u8bbe\u7f6e\u65f6\u95f4\u5185\uff0c\u9ed8\u8ba4\u6bcf\u9694\u4e00\u6bb5\u65f6\u95f4\u68c0\u6d4b\u4e00\u6b21\u5f53\u524d\u9875\u9762\u5143\u7d20\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u8d85\u8fc7\u8bbe\u7f6e\u65f6\u95f4\u68c0\u6d4b\u4e0d\u5230\u5219\u629b\u51fa\u5f02\u5e38\u3002\u5177\u4f53\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)<\/code><\/pre>\n<ul>\n<li>driver\uff1a\u6d4f\u89c8\u5668\u9a71\u52a8<\/li>\n<li>timeout\uff1a\u6700\u957f\u8d85\u65f6\u65f6\u95f4\uff0c\u9ed8\u8ba4\u4ee5\u79d2\u4e3a\u5355\u4f4d<\/li>\n<li>poll_frequency\uff1a\u68c0\u6d4b\u7684\u95f4\u9694\uff08\u6b65\u957f\uff09\u65f6\u95f4\uff0c\u9ed8\u8ba4\u4e3a0.5S<\/li>\n<li>ignored_exceptions\uff1a\u8d85\u65f6\u540e\u7684\u5f02\u5e38\u4fe1\u606f\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u629bNoSuchElementException\u5f02\u5e38<\/li>\n<\/ul>\n<p><code>WebDriverWait()<\/code>\u4e00\u822c\u7531<code>until()<\/code>\u6216<code>until_not()<\/code>\u65b9\u6cd5\u914d\u5408\u4f7f\u7528\uff1a<\/p>\n<ul>\n<li>until(method, message=&#8221;) \u8c03\u7528\u8be5\u65b9\u6cd5\u63d0\u4f9b\u7684\u9a71\u52a8\u7a0b\u5e8f\u4f5c\u4e3a\u4e00\u4e2a\u53c2\u6570\uff0c\u76f4\u5230\u8fd4\u56de\u503c\u4e3aTrue<\/li>\n<li>until_not(method, message=&#8221;) \u8c03\u7528\u8be5\u65b9\u6cd5\u63d0\u4f9b\u7684\u9a71\u52a8\u7a0b\u5e8f\u4f5c\u4e3a\u4e00\u4e2a\u53c2\u6570\uff0c\u76f4\u5230\u8fd4\u56de\u503c\u4e3aFalse<\/li>\n<\/ul>\n<p>\u5728\u672c\u4f8b\u4e2d\uff0c\u901a\u8fc7as\u5173\u952e\u5b57\u5c06<code>expected_conditions<\/code>\u91cd\u547d\u540d\u4e3a<code>EC<\/code>\uff0c\u5e76\u8c03\u7528<code>presence_of_element_located()<\/code>\u65b9\u6cd5\u5224\u65ad\u5143\u7d20\u662f\u5426\u5b58\u5728\u3002<\/p>\n<h2>\u9690\u5f0f\u7b49\u5f85<\/h2>\n<p>WebDriver\u63d0\u4f9b\u4e86<code>implicitly_wait()<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u9690\u5f0f\u7b49\u5f85\uff0c\u9ed8\u8ba4\u8bbe\u7f6e\u4e3a0\u3002\u5b83\u7684\u7528\u6cd5\u76f8\u5bf9\u6765\u8bf4\u8981\u7b80\u5355\u5f97\u591a\u3002<\/p>\n<pre><code class=\"language-python\">from selenium import webdriver\nfrom selenium.common.exceptions import NoSuchElementException\nfrom time import ctime\n\ndriver = webdriver.Firefox()\n\n# \u8bbe\u7f6e\u9690\u5f0f\u7b49\u5f85\u4e3a10\u79d2\ndriver.implicitly_wait(10)\ndriver.get(&quot;http:\/\/www.baidu.com&quot;)\n\ntry:\n    print(ctime())\n    driver.find_element_by_id(&quot;kw22&quot;).send_keys(&#039;selenium&#039;)\nexcept NoSuchElementException as e:\n    print(e)\nfinally:\n    print(ctime())\n    driver.quit()<\/code><\/pre>\n<p><code>implicitly_wait()<\/code>\u9ed8\u8ba4\u53c2\u6570\u7684\u5355\u4f4d\u4e3a\u79d2\uff0c\u672c\u4f8b\u4e2d\u8bbe\u7f6e\u7b49\u5f85\u65f6\u957f\u4e3a10\u79d2\u3002\u9996\u5148\u8fd910\u79d2\u5e76\u975e\u4e00\u4e2a\u56fa\u5b9a\u7684\u7b49\u5f85\u65f6\u95f4\uff0c\u5b83\u5e76\u4e0d\u5f71\u54cd\u811a\u672c\u7684\u6267\u884c\u901f\u5ea6\u3002\u5176\u6b21\uff0c\u5b83\u5e76\u4e0d\u9488\u5bf9\u9875\u9762\u4e0a\u7684\u67d0\u4e00\u5143\u7d20\u8fdb\u884c\u7b49\u5f85\u3002\u5f53\u811a\u672c\u6267\u884c\u5230\u67d0\u4e2a\u5143\u7d20\u5b9a\u4f4d\u65f6\uff0c\u5982\u679c\u5143\u7d20\u53ef\u4ee5\u5b9a\u4f4d\uff0c\u5219\u7ee7\u7eed\u6267\u884c\uff1b\u5982\u679c\u5143\u7d20\u5b9a\u4f4d\u4e0d\u5230\uff0c\u5219\u5b83\u5c06\u4ee5\u8f6e\u8be2\u7684\u65b9\u5f0f\u4e0d\u65ad\u5730\u5224\u65ad\u5143\u7d20\u662f\u5426\u88ab\u5b9a\u4f4d\u5230\u3002\u5047\u8bbe\u5728\u7b2c6\u79d2\u5b9a\u4f4d\u5230\u4e86\u5143\u7d20\u5219\u7ee7\u7eed\u6267\u884c\uff0c\u82e5\u76f4\u5230\u8d85\u51fa\u8bbe\u7f6e\u65f6\u957f\uff0810\u79d2\uff09\u8fd8\u6ca1\u6709\u5b9a\u4f4d\u5230\u5143\u7d20\uff0c\u5219\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WebDriver\u63d0\u4f9b\u4e86\u4e24\u79cd\u7c7b\u578b\u7684\u7b49\u5f85\uff1a\u663e\u5f0f\u7b49\u5f85\u548c\u9690\u5f0f\u7b49\u5f85\u3002 \u663e\u5f0f\u7b49\u5f85 \u663e\u5f0f\u7b49\u5f85\u4f7fWebdDriver\u7b49\u5f85\u67d0\u4e2a [&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-757","post","type-post","status-publish","format-standard","hentry","category-selenium"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/757","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=757"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/757\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}