1 回答
![?](http://img1.sycdn.imooc.com/5333a2320001acdd02000200-100-100.jpg)
TA贡献1848条经验 获得超2个赞
有几件事:
WebDriverWait(driver, 1).until(EC.visibility_of_element_located((By.ID, 'spot')
不等待 1 秒...它实际上最多等待1 秒以使元素变得可见。它每 250 毫秒轮询一次 DOM 中的元素,直到元素变得可见或超时。我认为您遇到的问题是,在第二次调用时,弹出窗口当前已打开,因此它满足等待条件,因此再次打印相同的数字。第二个问题是您需要等待第一个弹出窗口消失,然后等待第二个弹出窗口出现。一种方法是等待第一个弹出窗口变得陈旧。陈旧元素是指不再附加到 DOM 的元素(它不再存在)。第一个弹出窗口将出现,然后当它消失时,它将变得陈旧或与 DOM 分离。为了等待一个元素变得过时,您必须获取对该元素的引用(将其存储在变量中),然后用于
WebDriverWait
等待它变得过时。
上代码...
# store the WebDriverWait instance in a variable for reuse
wait = WebDriverWait(driver, 3)
# wait for the first popup to appear
popup = wait.until(EC.visibility_of_element_located((By.ID, 'spot')))
# print the text
print(popup.text)
# wait for the first popup to disappear
wait.until(EC.staleness_of(popup))
# wait for the second popup to appear
popup = wait.until(EC.visibility_of_element_located((By.ID, 'spot')))
# print the text
print(popup.text)
# wait for the second popup to disappear
wait.until(EC.staleness_of(popup))
... and so on
正如您所看到的,每个弹出窗口的代码都是相同的,因此如果您愿意,可以循环。
添加回答
举报