首页 > 解决方案 > 告诉脚本等到按钮可点击?

问题描述

我正在用 Selenium (Python) 编写脚本,但我遇到了禁用单击按钮的问题。该按钮仅在表单完成后才变为活动状态。

<button type="submit" class="pm_button primary large humanVerification-completeSetup-create" ng-disabled="model.emailCodeVerification === '' &amp;&amp; model.captcha_token === false &amp;&amp; model.smsCodeVerification === ''" translate="" translate-context="Action" disabled="disabled">Complete setup</button>

这是按钮 HTML 部分。现在按钮看起来像这样:

按钮图片 http://prntscr.com/pl4u96

如您所见,它不可点击。仅当验证码或任何其他类型的验证完成时,该按钮才会激活。现在我唯一的要求是在 Button 变为活动状态时检测它。

<button type="submit" class="pm_button primary large humanVerification-completeSetup-create" ng-disabled="model.emailCodeVerification === '' &amp;&amp; model.captcha_token === false &amp;&amp; model.smsCodeVerification === ''" translate="" translate-context="Action">Complete setup</button>

这是验证码或其他类型验证完成后的代码。验证完成后,第一行代码中的部分disabled="disabled"被完全删除。我真的对如何检测它有 0 个想法,但我未能找到一个好的解决方案。

标签: pythonhtmlseleniumselenium-webdriver

解决方案


诱导WebDriverWait并等待element_to_be_clickable()

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[@class='pm_button primary large humanVerification-completeSetup-create'][text()='Complete setup']"))).click()

您需要导入以下内容。

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

推荐阅读