{"id":23,"date":"2023-07-16T19:05:29","date_gmt":"2023-07-16T11:05:29","guid":{"rendered":"http:\/\/www.weigot.com\/?p=23"},"modified":"2023-07-16T19:05:29","modified_gmt":"2023-07-16T11:05:29","slug":"%e5%90%8c%e6%97%b6%e6%94%af%e6%8c%81%e6%98%be%e7%a4%ba%e7%ad%89%e5%be%85%e5%92%8c%e9%9a%90%e5%bc%8f%e7%ad%89%e5%be%85","status":"publish","type":"post","link":"http:\/\/www.weigot.com\/index.php\/2023\/07\/16\/%e5%90%8c%e6%97%b6%e6%94%af%e6%8c%81%e6%98%be%e7%a4%ba%e7%ad%89%e5%be%85%e5%92%8c%e9%9a%90%e5%bc%8f%e7%ad%89%e5%be%85\/","title":{"rendered":"\u540c\u65f6\u652f\u6301\u663e\u793a\u7b49\u5f85\u548c\u9690\u5f0f\u7b49\u5f85"},"content":{"rendered":"\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u7f16\u5199\u4e00\u4e2a\u901a\u7528\u65b9\u6cd5\uff0c\u540c\u65f6\u652f\u6301\u663e\u793a\u7b49\u5f85\u548c\u9690\u5f0f\u7b49\u5f85\uff1a<\/p>\n\n\n\n<p>pythonCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from selenium.webdriver.common.by import By\nfrom selenium.webdriver.support.ui import WebDriverWait\nfrom selenium.webdriver.support import expected_conditions as EC\nfrom selenium.common.exceptions import TimeoutException\n\ndef wait_and_click(driver, by, value, wait_type='implicit', timeout=10):\n    \"\"\"\n    \u7b49\u5f85\u5143\u7d20\u53ef\u70b9\u51fb\u5e76\u70b9\u51fb\u5b83\u3002\n\n    Args:\n        driver: WebDriver \u5bf9\u8c61\u3002\n        by: \u5143\u7d20\u5b9a\u4f4d\u65b9\u6cd5\uff0c\u5982 By.ID\u3001By.XPATH \u7b49\u3002\n        value: \u5143\u7d20\u5b9a\u4f4d\u8868\u8fbe\u5f0f\u3002\n        wait_type: \u7b49\u5f85\u7c7b\u578b\uff0c\u53ef\u4ee5\u662f 'implicit'\uff08\u9690\u5f0f\u7b49\u5f85\uff09\u6216 'explicit'\uff08\u663e\u793a\u7b49\u5f85\uff09\uff0c\u9ed8\u8ba4\u4e3a 'implicit'\u3002\n        timeout: \u7b49\u5f85\u8d85\u65f6\u65f6\u95f4\uff0c\u5355\u4f4d\u4e3a\u79d2\uff0c\u9ed8\u8ba4\u4e3a 10\u3002\n\n    Returns:\n        \u5982\u679c\u6210\u529f\u70b9\u51fb\u5143\u7d20\uff0c\u8fd4\u56de True\uff1b\u5982\u679c\u8d85\u65f6\u6216\u627e\u4e0d\u5230\u5143\u7d20\uff0c\u8fd4\u56de False\u3002\n    \"\"\"\n    try:\n        if wait_type == 'implicit':\n            driver.implicitly_wait(timeout)\n            element = driver.find_element(by, value)\n            element.click()\n            return True\n        elif wait_type == 'explicit':\n            wait = WebDriverWait(driver, timeout)\n            element = wait.until(EC.element_to_be_clickable((by, value)))\n            element.click()\n            return True\n        else:\n            raise ValueError(f\"Unsupported wait_type: {wait_type}\")\n    except TimeoutException:\n        return False\n    except Exception as e:\n        print(f\"Failed to wait and click element: {e}\")\n        return False\n<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a&nbsp;<code>wait_and_click<\/code>&nbsp;\u7684\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u56db\u4e2a\u53c2\u6570\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>driver<\/code>\uff1aWebDriver \u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><code>by<\/code>\uff1a\u5143\u7d20\u5b9a\u4f4d\u65b9\u6cd5\uff0c\u5982 By.ID\u3001By.XPATH \u7b49\u3002<\/li>\n\n\n\n<li><code>value<\/code>\uff1a\u5143\u7d20\u5b9a\u4f4d\u8868\u8fbe\u5f0f\u3002<\/li>\n\n\n\n<li><code>wait_type<\/code>\uff1a\u7b49\u5f85\u7c7b\u578b\uff0c\u53ef\u4ee5\u662f &#8216;implicit&#8217;\uff08\u9690\u5f0f\u7b49\u5f85\uff09\u6216 &#8216;explicit&#8217;\uff08\u663e\u793a\u7b49\u5f85\uff09\uff0c\u9ed8\u8ba4\u4e3a &#8216;implicit&#8217;\u3002<\/li>\n\n\n\n<li><code>timeout<\/code>\uff1a\u7b49\u5f85\u8d85\u65f6\u65f6\u95f4\uff0c\u5355\u4f4d\u4e3a\u79d2\uff0c\u9ed8\u8ba4\u4e3a 10\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8be5\u51fd\u6570\u9996\u5148\u68c0\u67e5&nbsp;<code>wait_type<\/code>&nbsp;\u53c2\u6570\uff0c\u5982\u679c\u662f &#8216;implicit&#8217;\uff0c\u5219\u8bbe\u7f6e\u9690\u5f0f\u7b49\u5f85\u65f6\u95f4\u4e3a&nbsp;<code>timeout<\/code>\uff0c\u7136\u540e\u67e5\u627e\u6307\u5b9a\u7684\u5143\u7d20\u5e76\u70b9\u51fb\u5b83\u3002\u5982\u679c\u662f &#8216;explicit&#8217;\uff0c\u5219\u521b\u5efa\u4e00\u4e2a\u663e\u793a\u7b49\u5f85\u5bf9\u8c61\u5e76\u7b49\u5f85\u5143\u7d20\u53ef\u70b9\u51fb\uff0c\u7136\u540e\u70b9\u51fb\u5b83\u3002\u5982\u679c&nbsp;<code>wait_type<\/code>&nbsp;\u53c2\u6570\u4e0d\u662f &#8216;implicit&#8217; \u6216 &#8216;explicit&#8217;\uff0c\u5219\u629b\u51fa&nbsp;<code>ValueError<\/code>&nbsp;\u5f02\u5e38\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u6210\u529f\u70b9\u51fb\u5143\u7d20\uff0c\u51fd\u6570\u8fd4\u56de True\uff1b\u5982\u679c\u8d85\u65f6\u6216\u627e\u4e0d\u5230\u5143\u7d20\uff0c\u51fd\u6570\u8fd4\u56de False\u3002<\/p>\n\n\n\n<p>\u793a\u4f8b\u7528\u6cd5\uff1a<\/p>\n\n\n\n<p>pythonCopy<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>driver = webdriver.Chrome()\n\n# \u4f7f\u7528\u9690\u5f0f\u7b49\u5f85\u7b49\u5f85\u5e76\u70b9\u51fb\u5143\u7d20\nresult = wait_and_click(driver, By.ID, 'my_button_id', wait_type='implicit', timeout=10)\nprint(f\"\u9690\u5f0f\u7b49\u5f85\u7ed3\u679c\uff1a{result}\")\n\n# \u4f7f\u7528\u663e\u793a\u7b49\u5f85\u7b49\u5f85\u5e76\u70b9\u51fb\u5143\u7d20\nresult = wait_and_click(driver, By.ID, 'my_button_id', wait_type='explicit', timeout=10)\nprint(f\"\u663e\u793a\u7b49\u5f85\u7ed3\u679c\uff1a{result}\")\n\ndriver.quit()\n<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a WebDriver \u5bf9\u8c61&nbsp;<code>driver<\/code>\uff0c\u7136\u540e\u5206\u522b\u4f7f\u7528\u9690\u5f0f\u7b49\u5f85\u548c\u663e\u793a\u7b49\u5f85\u7b49\u5f85\u5e76\u70b9\u51fb ID \u4e3a&nbsp;<code>my_button_id<\/code>&nbsp;\u7684\u5143\u7d20\uff0c\u5e76\u8f93\u51fa\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u7f16\u5199\u4e00\u4e2a\u901a\u7528\u65b9\u6cd5\uff0c\u540c\u65f6\u652f\u6301\u663e\u793a\u7b49\u5f85\u548c\u9690\u5f0f\u7b49\u5f85\uff1a pythonCopy \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-23","post","type-post","status-publish","format-standard","hentry","category-suibianjilu"],"_links":{"self":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/23","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/comments?post=23"}],"version-history":[{"count":1,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/23\/revisions"}],"predecessor-version":[{"id":24,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/23\/revisions\/24"}],"wp:attachment":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/media?parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/categories?post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/tags?post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}