<p>@COLDSPEED的评论为我们指出了正确的方向,@VaibhavMule的答案近乎完美,但他们都没有从<strong><code>Selenium</code></strong>的角度考虑<code>click()</code>的影响。在</p>
<h2><strong>单击()</strong></h2>
<p><strong>click()</strong>将通过<strong><code>selenium.webdriver.remote.webelement</code></strong>界面与文档交互。在</p>
<p>因此<strong>click()</strong>方法调用将执行新鲜度检查,以确保元素引用仍然有效。这实际上决定了元素是否仍然附加到DOM。如果这个测试失败,那么抛出一个<strong><code>StaleElementReferenceException</code></strong>,并且以后对这个实例的所有调用都将失败。因此,在调用每个<code>click()</code>之前,我们必须将<code>expected_conditions</code>子句设置为<strong><code>element_to_be_clickable</code></strong>来归纳<strong><code>WebDriverWait</code></strong>。在</p>
<p>因此,对于<code>loop this id selections</code>到{<cd10>},可以使用以下代码块:</p>
<pre><code>from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver.find_element_by_id("Product").click()
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.ID, "id1")))
for id in range(1, 7):
my_id = "my_id{}".format(id)
WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.ID, "my_id"))).click()
WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.ID, "my_id"))).click()
</code></pre>