1 回答

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
正如您所看到的,每個彈出窗口的代碼都是相同的,因此如果您愿意,可以循環。
添加回答
舉報