<p>从HTML中可以很明显地看出,元素位于<em>模态对话框</em>中。因此,要<code>click()</code>在<strong><code>OK</code></strong>按钮上,您必须诱导<em>WebDriverWait</em>,才能单击<em>元素</em>,并且可以使用以下<a href="https://stackoverflow.com/questions/48369043/official-locator-strategies-for-the-webdriver/48376890#48376890">Locator Strategies</a>:</p>
<ul>
<li><p>使用<code>LINK_TEXT</code>:</p>
<pre><code>WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "OK"))).click()
</code></pre></li>
<li><p>使用<code>CSS_SELECTOR</code>:</p>
<pre><code>WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.button.small.reveal-modal-button.mr5#btn-confirm-yes"))).click()
</code></pre></li>
<li><p>使用<code>XPATH</code>:</p>
<pre><code>WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='button small reveal-modal-button mr5' and @id='btn-confirm-yes'][text()='OK']"))).click()
</code></pre></li>
<li><p><strong>注意</strong>:必须添加以下导入:</p>
<pre><code>from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
</code></pre></li>
</ul>